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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

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));
    }/*from www.j a v a 2s. c o m*/
}

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.  j a  va 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());
    }
}

From source file:org.eclipse.ajdt.core.tests.model.AJModelTest.java

License:Open Source License

private void mappingTestForFile(IProject project, String filename, String[][] results) {
    IFile file = (IFile) project.findMember(filename);
    if (file == null)
        fail("Required file not found: " + filename); //$NON-NLS-1$

    String path = file.getRawLocation().toOSString();

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();//w ww.j a v a 2  s  . com
    Map annotationsMap = asm.getInlineAnnotations(path, true, true);

    assertNotNull("Didn't get annotations map for file: " + path, annotationsMap); //$NON-NLS-1$

    ICompilationUnit unit = AJCompilationUnitManager.INSTANCE.getAJCompilationUnit(file);
    if (unit == null) {
        unit = JavaCore.createCompilationUnitFrom(file);
    }

    assertNotNull("Didn't get a compilation unit from file: " + path, unit); //$NON-NLS-1$

    List toFind = new ArrayList();
    List toMatch = new ArrayList();
    for (int i = 0; i < results.length; i++) {
        toFind.add(results[i][0].intern());
        toMatch.add(results[i][1].intern());
    }

    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(project);
    Set keys = annotationsMap.keySet();
    for (Iterator it = keys.iterator(); it.hasNext();) {
        Object key = it.next();
        List annotations = (List) annotationsMap.get(key);
        for (Iterator it2 = annotations.iterator(); it2.hasNext();) {
            IProgramElement node = (IProgramElement) it2.next();
            String peName = node.toLabelString(false).intern();

            IJavaElement je = model.programElementToJavaElement(node);
            if (je == null) {
                System.out.println("je is null"); //$NON-NLS-1$
                continue;
            }
            String jaName = je.getElementName().intern();
            int index = toFind.indexOf(peName);
            if (index == -1) {
                fail("Unexpected additional IProgramElement name found: " + peName); //$NON-NLS-1$
            } else {
                String expected = (String) toMatch.get(index);
                if (expected.equals(jaName)) {
                    toFind.remove(index);
                    toMatch.remove(index);
                } else {
                    fail("Incorrect corresponding Java element. Found: " + jaName + " Expected: " + expected); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
        }
    }

    // check that we found everything we were looking for
    if (toFind.size() > 0) {
        String missing = ""; //$NON-NLS-1$
        for (int j = 0; j < toFind.size(); j++) {
            missing += System.getProperty("line.separator"); //$NON-NLS-1$
            missing += (String) toFind.get(j);
        }
        fail("Did not find all expected IProgramElement names. Missing: " + missing); //$NON-NLS-1$
    }
}

From source file:org.eclipse.ajdt.core.tests.model.AJModelTest2.java

License:Open Source License

/**
 * Tests for a injar/binary relationship for an element advised by advice in
 * another project (by adding that project's bin directory to the
 * aspectpath)//from  www. ja va 2 s  . c  o  m
 * 
 * @throws Exception
 */
public void testAspectPathDirWeaving() throws Exception {
    createPredefinedProject14("MyAspectLibrary"); //$NON-NLS-1$
    IProject weaveMeProject = createPredefinedProject("WeaveMe"); //$NON-NLS-1$
    this.waitForIndexes();
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(weaveMeProject);

    AJRelationshipType[] rels = new AJRelationshipType[] { AJRelationshipManager.ADVISED_BY };
    List<IRelationship> allRels = model.getRelationshipsForProject(rels);
    boolean gotBinaryAdvice = false;
    for (IRelationship rel : allRels) {
        IJavaElement source = model.programElementToJavaElement(rel.getSourceHandle());
        if (source.getElementName().equals("main")) { //$NON-NLS-1$
            for (String targetStr : rel.getTargets()) {
                IJavaElement target = model.programElementToJavaElement(targetStr);
                if (target.getElementName().indexOf("before") != -1) { //$NON-NLS-1$
                    gotBinaryAdvice = true;
                }
            }
        }
    }
    assertTrue("Didn't find main element advised by before advice", //$NON-NLS-1$
            gotBinaryAdvice);
}

From source file:org.eclipse.ajdt.core.tests.model.AJModelTest2.java

License:Open Source License

/**
 * Tests for the existence of a particular "advised by" relationship with a
 * runtime test, and one without.//  ww w .  j  a v a2 s  .  c om
 * 
 * @throws Exception
 */
public void testHasRuntimeTest() throws Exception {
    IProject project = createPredefinedProject("MarkersTest"); //$NON-NLS-1$
    AJRelationshipType[] rels = new AJRelationshipType[] { AJRelationshipManager.ADVISED_BY };
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(project);
    List<IRelationship> allRels = model.getRelationshipsForProject(rels);
    boolean gotBeforeAdviceWithoutRuntimeTest = false;
    boolean gotAroundAdviceWithRuntimeTest = false;
    for (IRelationship rel : allRels) {
        IJavaElement source = model.programElementToJavaElement(rel.getSourceHandle());
        if (source.getElementName().equals("bar")) { //$NON-NLS-1$
            for (String targetStr : rel.getTargets()) {
                IJavaElement target = model.programElementToJavaElement(targetStr);
                if (target.getElementName().equals("before") //$NON-NLS-1$
                        && !rel.hasRuntimeTest()) {
                    gotBeforeAdviceWithoutRuntimeTest = true;
                } else if (target.getElementName().equals("around") //$NON-NLS-1$
                        && rel.hasRuntimeTest()) {
                    gotAroundAdviceWithRuntimeTest = true;
                }
            }
        }
    }
    assertTrue("Didn't find \"bar\" element advised by before advice without a runtime test", //$NON-NLS-1$
            gotBeforeAdviceWithoutRuntimeTest);
    assertTrue("Didn't find \"bar\" element advised by around advice with a runtime test", //$NON-NLS-1$
            gotAroundAdviceWithRuntimeTest);
}

From source file:org.eclipse.ajdt.core.tests.model.AJModelTest3.java

License:Open Source License

private void mappingTestForFile(IProject project, String filename, String[][] results) {
    IFile file = (IFile) project.findMember(filename);
    if (file == null)
        fail("Required file not found: " + filename); //$NON-NLS-1$

    String path = file.getRawLocation().toOSString();

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();/*  w  w w  . ja  v a2  s  . c  o  m*/
    Map annotationsMap = asm.getInlineAnnotations(path, true, true);
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(project);

    assertNotNull("Didn't get annotations map for file: " + path, annotationsMap); //$NON-NLS-1$

    ICompilationUnit unit = AJCompilationUnitManager.INSTANCE.getAJCompilationUnit(file);
    if (unit == null) {
        unit = JavaCore.createCompilationUnitFrom(file);
    }

    assertNotNull("Didn't get a compilation unit from file: " + path, unit); //$NON-NLS-1$

    List toFind = new ArrayList();
    List toMatch = new ArrayList();
    for (int i = 0; i < results.length; i++) {
        toFind.add(results[i][0].intern());
        toMatch.add(results[i][1].intern());
    }

    Set keys = annotationsMap.keySet();
    for (Iterator it = keys.iterator(); it.hasNext();) {
        Object key = it.next();
        List annotations = (List) annotationsMap.get(key);
        for (Iterator it2 = annotations.iterator(); it2.hasNext();) {
            IProgramElement pe = (IProgramElement) it2.next();
            String peName = pe.toLabelString(false).intern();

            IJavaElement je = model.programElementToJavaElement(pe);
            if (je == null) {
                fail("je is null"); //$NON-NLS-1$
            }
            String jaName = je.getElementName();
            int index = toFind.indexOf(peName);
            if (index == -1) {
                fail("Unexpected additional IProgramElement name found: " + peName); //$NON-NLS-1$
            } else {
                String expected = (String) toMatch.get(index);
                if (expected.equals(jaName)) {
                    toFind.remove(index);
                    toMatch.remove(index);
                } else {
                    fail("Incorrect corresponding Java element. Found: " + jaName + " Expected: " + expected); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
        }
    }

    // check that we found everything we were looking for
    if (toFind.size() > 0) {
        String missing = ""; //$NON-NLS-1$
        for (int j = 0; j < toFind.size(); j++) {
            missing += System.getProperty("line.separator"); //$NON-NLS-1$
            missing += (String) toFind.get(j);
        }
        fail("Did not find all expected IProgramElement names. Missing: " + missing); //$NON-NLS-1$
    }
}

From source file:org.eclipse.ajdt.core.tests.model.BinaryWeavingSupportTest.java

License:Open Source License

public void testAspectPathDirWeaving() throws Exception {
    /*IProject libProject = */createPredefinedProject("MyAspectLibrary2"); //$NON-NLS-1$
    //      AJProjectModelFacade libModel = AJProjectModelFactory.getInstance().getModelForProject(libProject);

    IProject weaveMeProject = createPredefinedProject("WeaveMe2"); //$NON-NLS-1$
    AJProjectModelFacade weaveMeModel = AJProjectModelFactory.getInstance().getModelForProject(weaveMeProject);

    AJRelationshipType[] rels = new AJRelationshipType[] { AJRelationshipManager.ADVISED_BY };
    List allRels = weaveMeModel.getRelationshipsForProject(rels);
    IJavaElement mainEl = null;/*from  ww w.j  ava 2 s.  c  om*/
    for (Iterator iter = allRels.iterator(); (mainEl == null) && iter.hasNext();) {
        IRelationship rel = (IRelationship) iter.next();
        IJavaElement source = weaveMeModel.programElementToJavaElement(rel.getSourceHandle());
        if (source.getElementName().equals("main")) { //$NON-NLS-1$
            mainEl = source;
        }
    }
    assertNotNull("Didn't find element for advised main method", mainEl); //$NON-NLS-1$
    List related = weaveMeModel.getRelationshipsForElement(mainEl, AJRelationshipManager.ADVISED_BY);
    assertNotNull("getRelatedElements returned null", related); //$NON-NLS-1$
    boolean found1 = false;
    boolean found2 = false;
    boolean found3 = false;
    for (Iterator iter = related.iterator(); iter.hasNext();) {
        IJavaElement el = (IJavaElement) iter.next();
        String elName = el.getElementName();
        String resName = el.getResource().getName();
        if (elName.equals("before")) { //$NON-NLS-1$
            found1 = true;
            assertEquals("Found before element in wrong file", "MyBar.aj", resName); //$NON-NLS-1$ //$NON-NLS-2$
        } else if (elName.equals("afterReturning")) { //$NON-NLS-1$
            found2 = true;
            assertEquals("Found before afterReturning in wrong file", "MyBar2.aj", resName); //$NON-NLS-1$ //$NON-NLS-2$
        } else if (elName.equals("around")) { //$NON-NLS-1$
            found3 = true;
            assertEquals("Found before around in wrong file", "MyBar3.aj", resName); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
    assertTrue("Didn't find advised by before() relationship", found1); //$NON-NLS-1$
    assertTrue("Didn't find advised by afterReturning() relationship", found2); //$NON-NLS-1$
    assertTrue("Didn't find advised by around() relationship", found3); //$NON-NLS-1$

    // now look for added "advises" relationships in the aspect library
    // probject
    // XXX Not doing this any more
    //      rels = new AJRelationshipType[] { AJRelationshipManager.ADVISES };
    //      allRels = libModel.getRelationshipsForProject(rels);
    //      found1 = false;
    //      found2 = false;
    //      found3 = false;
    //      for (Iterator iter = allRels.iterator(); iter.hasNext();) {
    //         IRelationship rel = (IRelationship) iter.next();
    //         String sourceName = libModel.programElementToJavaElement(rel.getSourceHandle()).getElementName();
    //         for (Iterator targetIter = rel.getTargets().iterator(); targetIter.hasNext(); ) {
    //             String targetName = libModel.programElementToJavaElement((String) targetIter.next()).getElementName();
    //             if (sourceName.equals("before")) { //$NON-NLS-1$
    //                found1 = true;
    //                assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //             } else if (sourceName.equals("afterReturning")) { //$NON-NLS-1$
    //                found2 = true;
    //                assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //             } else if (sourceName.equals("around")) { //$NON-NLS-1$
    //                found3 = true;
    //                assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //             }
    //         }
    //      }
    //      assertTrue("Didn't find advises before() relationship", found1); //$NON-NLS-1$
    //      assertTrue("Didn't find advises afterReturning() relationship", found2); //$NON-NLS-1$
    //      assertTrue("Didn't find advises around() relationship", found3); //$NON-NLS-1$

    // there is no "getOtherProjectAllRelationships" any more
    //      List otherProjectRels = model.getOtherProjectAllRelationships(rels);
    //      found1 = false;
    //      found2 = false;
    //      found3 = false;
    //      for (Iterator iter = otherProjectRels.iterator(); iter.hasNext();) {
    //         AJRelationship rel = (AJRelationship) iter.next();
    //         String sourceName = rel.getSource().getElementName();
    //         String targetName = rel.getTarget().getElementName();
    //         if (sourceName.equals("before")) { //$NON-NLS-1$
    //            found1 = true;
    //            assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //         } else if (sourceName.equals("afterReturning")) { //$NON-NLS-1$
    //            found2 = true;
    //            assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //         } else if (sourceName.equals("around")) { //$NON-NLS-1$
    //            found3 = true;
    //            assertEquals("Incorrect target name", "main", targetName); //$NON-NLS-1$ //$NON-NLS-2$
    //         }
    //      }
    //      assertTrue("Didn't find advises before() relationship", found1); //$NON-NLS-1$
    //      assertTrue("Didn't find advises afterReturning() relationship", found2); //$NON-NLS-1$
    //      assertTrue("Didn't find advises around() relationship", found3); //$NON-NLS-1$
}

From source file:org.eclipse.ajdt.core.tests.model.BinaryWeavingSupportTest.java

License:Open Source License

public void testBug160236() throws Exception {
    IProject project = createPredefinedProject("bug160236"); //$NON-NLS-1$
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForProject(project);

    AJRelationshipType[] rels = new AJRelationshipType[] { AJRelationshipManager.ADVISED_BY };
    List allRels = model.getRelationshipsForProject(rels);
    boolean found1 = false;
    boolean found2 = false;
    boolean found3 = false;
    for (Iterator iter = allRels.iterator(); iter.hasNext();) {
        IRelationship rel = (IRelationship) iter.next();
        String sourceName = model.programElementToJavaElement(rel.getSourceHandle()).getElementName();
        if (sourceName.equals("Sample")) { //$NON-NLS-1$
            for (Iterator targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
                IJavaElement targetElt = model.programElementToJavaElement((String) targetIter.next());
                if ((targetElt.getElementName().equals("afterReturning")) //$NON-NLS-1$
                        && (targetElt.getParent().getElementName().equals("AbstractBeanConfigurerAspect"))) { //$NON-NLS-1$
                    found1 = true;//  w ww .  ja v a 2  s.co  m
                } else if ((targetElt.getElementName().equals("before")) //$NON-NLS-1$
                        && (targetElt.getParent().getElementName().equals("AbstractBeanConfigurerAspect"))) { //$NON-NLS-1$
                    found2 = true;
                } else {
                    fail("Unexpected target found: " + targetElt.getHandleIdentifier()); //$NON-NLS-1$
                }
            }
        } else if (sourceName.equals("main")) { //$NON-NLS-1$
            for (Iterator targetIter = rel.getTargets().iterator(); targetIter.hasNext();) {
                IJavaElement targetElt = model.programElementToJavaElement((String) targetIter.next());
                if ((targetElt.getElementName().equals("before")) //$NON-NLS-1$
                        && (targetElt.getParent().getElementName().equals("AnnotationBeanConfigurerAspect"))) { //$NON-NLS-1$
                    found3 = true;
                } else {
                    fail("Unexpected target found: " + targetElt.getHandleIdentifier()); //$NON-NLS-1$
                }
            }
        }
    }
    assertTrue("Didn't find Sample advised by afterReturning() binary aspect relationship", found1); //$NON-NLS-1$
    assertTrue("Didn't find Sample advised by before() binary aspect relationship", found2); //$NON-NLS-1$
    assertTrue("Didn't find main advised by before() binary aspect relationship", found3); //$NON-NLS-1$

}

From source file:org.eclipse.ajdt.core.tests.problemfinding.ITITProblemFinderTests.java

License:Apache License

protected void checkModel() throws JavaModelException {
    AJProjectModelFacade model = AJProjectModelFactory.getInstance().getModelForJavaElement(proj);
    IType cityType = proj.findType("p.City");
    List<IJavaElement> rels = model.getRelationshipsForElement(cityType,
            AJRelationshipManager.ASPECT_DECLARATIONS, false);
    assertEquals("Should have found exactly one relationship to target type\n" + rels, 1, rels.size());
    IJavaElement elt = rels.get(0);
    assertTrue("Relationship should be to a type " + elt, elt instanceof IType);
    assertTrue("Element should exist " + elt, elt.exists());
    assertEquals("wrong name for element", "Keys", elt.getElementName());

    rels = model.getRelationshipsForElement(elt, AJRelationshipManager.DECLARED_ON, false);
    assertEquals("Should have found exactly one relationship to target type\n" + rels, 1, rels.size());
    assertEquals(cityType, rels.get(0));

    assertEquals("Should have exactly 2 relationships in the entire project", 2,
            model.getRelationshipsForProject(AJRelationshipManager.getAllRelationshipTypes()).size());
}

From source file:org.eclipse.ajdt.core.text.ITDAwareSelectionRequestor.java

License:Open Source License

public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name,
        boolean isDeclaration, char[] uniqueKey, int start, int end) {
    try {// ww  w  . j a  v  a2s . c o  m
        IType targetType = findType(declaringTypePackageName, declaringTypeName);
        if (targetType == null) {
            // type couldn't be found.  this is really some kind of problem
            return;
        }
        List<IJavaElement> itds = ensureModel(targetType).getRelationshipsForElement(targetType,
                AJRelationshipManager.ASPECT_DECLARATIONS);
        for (IJavaElement elt : itds) {
            if (matchedField(elt, name)) {
                accepted.add(elt);
                return;
            }
        }

        // if we are selecting inside of an ITD and the field being matched is a regular field, we find it here.
        IntertypeElement itd = maybeGetITD(start);
        if (itd != null) {
            IField field = targetType.getField(String.valueOf(name));
            if (field.exists()) {
                accepted.add(field);
                return;
            }
        }

        // now check to see if we actually found a field in an ITIT
        IJavaElement parent = targetType.getParent();
        if (parent.getElementType() == IJavaElement.TYPE) {
            // definitely an inner type.  If the outer type does not match,
            // then we know that this was from an ITIT
            char[] enclosingName = (parent.getElementName() + ".").toCharArray();
            if (!CharOperation.prefixEquals(enclosingName, declaringTypeName)) {
                IField field = targetType.getField(String.valueOf(name));
                if (field.exists()) {
                    accepted.add(field);
                }
            }
        }
    } catch (JavaModelException e) {
    }
}