Example usage for org.eclipse.jdt.core IJavaElement getHandleIdentifier

List of usage examples for org.eclipse.jdt.core IJavaElement getHandleIdentifier

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getHandleIdentifier.

Prototype

String getHandleIdentifier();

Source Link

Document

Returns a string representation of this element handle.

Usage

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String getNotBlankName(String currentName, IJavaElement element) {
    String l_return = currentName;
    if ("".equals(l_return)) {
        if (element instanceof IType) {
            IJavaElement parentType = element.getParent().getAncestor(IJavaElement.TYPE);
            String handle = element.getHandleIdentifier();
            int start = handle.lastIndexOf(parentType.getElementName());
            if (start != -1) {
                handle = handle.substring(start + parentType.getElementName().length());
            }// w  w w  . ja v a2 s .co  m
            l_return = "anonymous#" + handle;
        } else {
            l_return = "(default package)";
        }
    }
    return l_return;
}

From source file:net.sourceforge.metrics.internal.xml.XMLSourceTreeExporter.java

License:Open Source License

/**
 * @param monitor//ww  w  .ja v a 2s . c  o m
 */
public void export(IJavaElement element, File outputFile, IProgressMonitor monitor)
        throws InvocationTargetException {
    try {
        this.handle = element.getHandleIdentifier();
        FileOutputStream out = new FileOutputStream(outputFile);
        XMLPrintStream pOut = new XMLPrintStream(out);
        pOut.printXMLHeader();
        pOut.println("<Metrics>");
        monitor.beginTask("Exporting metrics to XML...", calculateTotalWork(handle));
        printDescriptions(pOut, monitor);
        AbstractMetricSource root = Cache.singleton.get(element);
        IXMLExporter exporter = root.getExporter();
        exporter.export(root, pOut, 1, monitor);
        monitor.worked(1);
        pOut.println("</Metrics>");
        pOut.close();
    } catch (Throwable e) {
        throw new InvocationTargetException(e);
    }
}

From source file:net.sourceforge.metrics.ui.layeredpackagegraph.LayeredPackageTableView.java

License:Open Source License

private boolean shouldBeBusy(IJavaElement current) {
    boolean busy = (selection != null) && (selection.equals(current));
    busy = busy || (selection != null)/*  w ww .  j a v  a2s .co m*/
            && (current.getHandleIdentifier().startsWith(selection.getHandleIdentifier()));
    return busy;
}

From source file:net.sourceforge.tagsea.mylyn.task.TaskUtils.java

License:Open Source License

private static String getHandle(IMarker todoMarker) {
    try {/* ww w.  ja  v a  2 s. c o m*/
        IResource resource = todoMarker.getResource();
        IJavaElement element = JavaCore.create(resource);
        if (element instanceof ICompilationUnit) {
            ICompilationUnit unit = (ICompilationUnit) element;
            int lineNum = Integer.parseInt(todoMarker.getAttribute(IMarker.CHAR_START).toString());
            IJavaElement unitElement = unit.getElementAt(lineNum);
            if (unitElement != null) {
                // unitElement is a method or field
                return unitElement.getHandleIdentifier();
            } else { // use the enclosing class
                return unit.getHandleIdentifier();
            }
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:net.sourceforge.tagsea.mylyn.waypoints.WaypointsUtils.java

License:Open Source License

public static String getHandle(IWaypoint waypoint) {
    // @tag tagsea.mylyn.redesign -author=John -date="enCA:20/03/08" :
    // Ideally this should all be delegated to the specific waypoint plugin
    // @tag tagsea.mylyn.clone -author=John -date="enCA:20/03/08" :
    // Synchronize changes with TaskUtils.getHandle(IMarker)
    if (waypoint.getType().equals(ParsedWaypointPlugin.WAYPOINT_TYPE)) {
        String domain = waypoint.getStringValue(IParsedWaypointAttributes.ATTR_DOMAIN, "");
        return domain;
    }/*from   w  w w  . j  a  v a2s.c  o  m*/
    if (waypoint.getType().equals(TaskWaypointPlugin.WAYPOINT_ID)) {
        try {
            IMarker marker = TaskWaypointUtils.getTaskForWaypoint(waypoint);
            IResource resource = marker.getResource();
            IJavaElement element = JavaCore.create(resource);
            if (element instanceof ICompilationUnit) {
                ICompilationUnit unit = (ICompilationUnit) element;
                int charStart = Integer.parseInt(marker.getAttribute(IMarker.CHAR_START).toString());
                IJavaElement unitElement = unit.getElementAt(charStart);
                if (unitElement != null) {
                    // unitElement is a method or field
                    return unitElement.getHandleIdentifier();
                } else { // use the enclosing class
                    return unit.getHandleIdentifier();
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (JavaModelException e) {
            e.printStackTrace();
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:org.eclipse.ajdt.core.model.AJModel.java

License:Open Source License

/**
 * @param proj the project whose relationships we are looking for
 * @param relTypes the relationship types we are looking for
 * @return all DECLARED_ON relationships for the given java element
 * //from w w w.j  a  v a  2 s .com
 * @deprecated see bug 253245
 */
public List/*AJRelationship*/ getAllRelationships(IProject proj, AJRelationshipType[] relTypes) {
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(proj);
    if (model.hasModel()) { // returns false if project is not built
        List/*IRelationship*/ allRels = model.getRelationshipsForProject(relTypes);
        List/*AJRelationship*/ ajRels = new ArrayList(allRels.size());
        for (Iterator relIter = allRels.iterator(); relIter.hasNext();) {
            IRelationship rel = (IRelationship) relIter.next();
            IJavaElement source = model.programElementToJavaElement(rel.getSourceHandle());
            if (source instanceof IntertypeElement) {
                for (Iterator targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
                    String target = (String) targetIter.next();
                    // ensure a Java handle is used here.
                    // because the things being compared to are 
                    // Java handles.
                    // This is avoiding problems when Type is in a .aj file
                    IJavaElement elt = model.programElementToJavaElement(target);
                    elt = JavaCore.create(AspectJCore.convertToJavaCUHandle(elt.getHandleIdentifier(), elt));
                    if (elt != null) {
                        // will be null if type is an aspect type or contained in an aspect type
                        AJRelationship ajRel = new AJRelationship(source,
                                AJRelationshipManager.toRelationshipType(rel.getName()), elt,
                                rel.hasRuntimeTest());
                        ajRels.add(ajRel);
                    }
                }
            }
        }
        return ajRels;
    } else {
        return Collections.EMPTY_LIST;
    }
}

From source file:org.eclipse.ajdt.core.model.AJProjectModelFacade.java

License:Open Source License

/**
 * @return a program element that corresponds to the given java element.
 *///from w  w  w.  ja  v a 2s  . c  o m
public IProgramElement javaElementToProgramElement(IJavaElement je) {
    if (!isInitialized) {
        return IHierarchy.NO_STRUCTURE;
    }
    String ajHandle = je.getHandleIdentifier();

    boolean isBinary = false;
    if (isBinaryHandle(ajHandle) || je.isReadOnly()) {
        ajHandle = convertToAspectJBinaryHandle(ajHandle, false);
        isBinary = true;
    } else if (isFromExternalProject(je)) {
        ajHandle = convertToAspectJBinaryHandle(ajHandle, true);
        isBinary = true;
    }

    // check to see if we need to replace { (compilation unit) with * (aj compilation unit)
    // if using cuprovider, then aj compilation units have {, but needs to change to *
    ICompilationUnit cu = null;
    if (je instanceof IMember) {
        cu = ((IMember) je).getCompilationUnit();
    } else if (je instanceof IPackageDeclaration) {
        IJavaElement parent = ((IPackageDeclaration) je).getParent();
        if (parent instanceof ICompilationUnit) {
            cu = (ICompilationUnit) parent;
        }
    } else if (je instanceof AJCodeElement) {
        cu = ((AJCodeElement) je).getCompilationUnit();
        // get the occurence count 
        int count = ((AJCodeElement) je).occurrenceCount;
        // need the first bang after the last close paren
        int lastParen = ajHandle.lastIndexOf(')');
        int firstBang = ajHandle.indexOf(JavaElement.JEM_COUNT, lastParen);
        if (firstBang > -1) {
            ajHandle = ajHandle.substring(0, firstBang);
            if (count > 1) {
                // there is more than one element
                // with this name
                ajHandle += "" + JavaElement.JEM_COUNT + count;
            }
        }

    } else if (je instanceof ILocalVariable) {
        IOpenable openable = ((ILocalVariable) je).getOpenable();
        cu = openable instanceof ICompilationUnit ? (ICompilationUnit) openable : null;
    } else if (je instanceof ImportDeclaration) {
        cu = ((ImportDeclaration) je).getCompilationUnit();
    } else if (je instanceof ImportContainer) {
        cu = ((ImportContainer) je).getCompilationUnit();
    } else if (je instanceof ICompilationUnit) {
        cu = (ICompilationUnit) je;
    }
    if (cu != null) {
        IResource resource = cu.getResource();
        if (resource != null && resource.exists()
                && CoreUtils.ASPECTJ_SOURCE_ONLY_FILTER.accept(resource.getName())) {
            ajHandle = ajHandle.replaceFirst("" + JavaElement.JEM_ESCAPE + JavaElement.JEM_COMPILATIONUNIT,
                    Character.toString(AspectElement.JEM_ASPECT_CU));
        }
    }

    IProgramElement ipe = structureModel.findElementForHandleOrCreate(ajHandle, false);
    if (ipe == null) {
        if (isBinary) {
            // might be an aspect in a class file.  JDT doesn't know it is an aspect
            // try looking for handle again, but use an Aspect token
            // problem will be if this is an aspect contained in a class or vice versa
            ajHandle = ajHandle.replace(JavaElement.JEM_TYPE, AspectElement.JEM_ASPECT_TYPE);
            ipe = structureModel.findElementForHandleOrCreate(ajHandle, false);
        }
        if (ipe == null) {
            // occurs when the handles are not working properly
            return IHierarchy.NO_STRUCTURE;
        }
    }
    return ipe;
}

From source file:org.eclipse.ajdt.core.tests.AJCoreTest.java

License:Open Source License

private void compareWithHandles(String[][] testHandles) {
    for (int i = 0; i < testHandles.length; i++) {
        IJavaElement el = AspectJCore.create(testHandles[i][0]);
        assertEquals("Handle identifier of created element doesn't match original", //$NON-NLS-1$
                testHandles[i][0], el.getHandleIdentifier());
        assertEquals("Name of created element doesn't match expected", //$NON-NLS-1$
                testHandles[i][1], el.getElementName());
        assertEquals("Name of created element resource doesn't match expected", //$NON-NLS-1$
                testHandles[i][2], el.getResource().getName());
        assertEquals("Created element is not of the expected class type", //$NON-NLS-1$
                testHandles[i][3], getSimpleClassName(el));
    }//w  ww  .  j  a va 2 s .c o  m
}

From source file:org.eclipse.ajdt.core.tests.builder.LinkedFoldersTestBug270202.java

License:Open Source License

public void testHandlesInsideLinkedFolders() throws Exception {
    IJavaElement ije = AspectJCore.create(linkedFile);
    assertTrue("Compilation unit should exist " + ije.getHandleIdentifier(), ije.exists());
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(project);
    IProgramElement ipe = model.javaElementToProgramElement(ije);
    assertNotSame("IProgramElement should exist", IHierarchy.NO_STRUCTURE, ipe);
    IJavaElement recreated = model.programElementToJavaElement(ipe);
    assertTrue("Compilation unit should exist " + recreated.getHandleIdentifier(), recreated.exists());
}

From source file:org.eclipse.ajdt.core.tests.codeselect.AbstractITDAwareCodeSelectionTests.java

License:Open Source License

protected void validateCodeSelect(ICompilationUnit unit, IRegion region, String expected,
        boolean expectingProblems, int numParams) throws Exception {
    if (!expectingProblems) {
        this.assertNoProblems(unit.getJavaProject().getProject());
    }// w w w .ja v a 2s .c  om
    performDummySearch(unit.getJavaProject());
    IJavaElement[] result = unit.codeSelect(region.getOffset(), region.getLength());
    assertEquals("Should have found exactly one hyperlink", 1, result.length);
    IJavaElement elt = result[0];
    assertTrue("Java element " + elt.getHandleIdentifier() + " should exist", elt.exists());
    assertEquals(expected, elt.getElementName());

    if (numParams >= 0 && elt instanceof IMethod) {
        assertEquals("Wrong number of parameters for " + elt, numParams,
                ((IMethod) elt).getNumberOfParameters());
    }
}