Example usage for org.aspectj.asm AsmManager getInlineAnnotations

List of usage examples for org.aspectj.asm AsmManager getInlineAnnotations

Introduction

In this page you can find the example usage for org.aspectj.asm AsmManager getInlineAnnotations.

Prototype

public HashMap<Integer, List<IProgramElement>> getInlineAnnotations(String sourceFile, boolean showSubMember,
        boolean showMemberAndType) 

Source Link

Document

Constructs map each time it's called.

Usage

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

License:Open Source License

protected void setUp() throws Exception {
    super.setUp();
    project = createPredefinedProject("AJProject83082"); //$NON-NLS-1$

    model = AJProjectModelFactory.getInstance().getModelForProject(project);

    IFolder src = project.getFolder("src"); //$NON-NLS-1$
    IFolder wpstest = src.getFolder("wpstest"); //$NON-NLS-1$
    IFolder aspectjPackage = wpstest.getFolder("aspectj"); //$NON-NLS-1$
    IFile main = aspectjPackage.getFile("Main.java"); //$NON-NLS-1$

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();/*from  w  w w  .j a  v  a 2 s.  c o  m*/
    Map annotationsMap = asm.getInlineAnnotations(main.getRawLocation().toOSString(), true, true);
    ajCodeElements = createAJCodeElements(annotationsMap);

}

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

License:Open Source License

public void testCompareTwoAJCodeElements() {

    // get the class file and create the map for the file (the underlying one)      
    IFile main = aspectjPackage.getFile("Main.java");

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();/*from w  ww  . j  a va 2s.com*/
    Map annotationsMap = asm.getInlineAnnotations(main.getRawLocation().toOSString(), true, true);
    assertNotNull("annotation map should not be null for Main.java", annotationsMap);
    // for the two IProgramElements which correspond to the two calls
    // in main - create the IJavaElements (or AJCodeElements)
    AJCodeElement ajce1 = null;
    AJCodeElement ajce2 = null;
    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();
            ISourceLocation sl = node.getSourceLocation();
            if (node.toLinkLabelString(false)
                    .equals("Main: method-call(void java.io.PrintStream.println(java.lang.String))")
                    && (sl.getLine() == 23)) {

                IJavaElement ije = model.programElementToJavaElement(node);
                if (ije instanceof AJCodeElement) {
                    ajce1 = (AJCodeElement) ije;
                }
            } else if (node.toLinkLabelString(false)
                    .equals("Main: method-call(void java.io.PrintStream.println(java.lang.String))")
                    && (sl.getLine() == 24)) {

                IJavaElement ije = model.programElementToJavaElement(node);
                if (ije instanceof AJCodeElement) {
                    ajce2 = (AJCodeElement) ije;
                }
            }
        }
    }
    assertNotNull("AJCodeElement shouldn't be null", ajce1);
    assertNotNull("AJCodeElement shouldn't be null", ajce2);

    // check that when call compare on them, that the one with
    // the lowest line number is first in the list
    AJComparator comp = new AJComparator();
    assertTrue("ajce1 should be less than ajce2", comp.compare(ajce1, ajce2) < 0);
    assertTrue("ajce2 should be greater than ajce1", comp.compare(ajce2, ajce1) > 0);
    assertTrue("ajce1 should be equal to ajce1", comp.compare(ajce1, ajce1) == 0);
}

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

License:Open Source License

public void testCompareTwoIJavaElements() {

    // get the aspect and create the map for the file (the underlying one)
    IFile aspect = aspectjPackage.getFile("A.aj");

    // for the two IProgramElements which correspond to the two calls
    // in main - create the IJavaElements

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();//from   w  w  w  .j  a  v  a2 s  . c o m
    Map annotationsMap = asm.getInlineAnnotations(aspect.getRawLocation().toOSString(), true, true);
    assertNotNull("annotation map should not be null for Main.java", annotationsMap);
    // for the two IProgramElements which correspond to the two pieces
    // of advice (before and after) in A.aj - create the IJavaElements 
    IJavaElement ije1 = null;
    IJavaElement ije2 = null;
    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();
            ISourceLocation sl = node.getSourceLocation();
            if (node.toLinkLabelString(false).equals("A.after(String): tracedPrint..")
                    && (sl.getLine() == 30)) {

                IJavaElement ije = model.programElementToJavaElement(node);
                if (!(ije instanceof AJCodeElement)) {
                    ije1 = ije;
                }
            } else if (node.toLinkLabelString(false).equals("A.before(String): tracedPrint..")
                    && (sl.getLine() == 26)) {

                IJavaElement ije = model.programElementToJavaElement(node);
                if (!(ije instanceof AJCodeElement)) {
                    ije2 = ije;
                }
            }
        }
    }
    assertNotNull("IJavaElement shouldn't be null", ije1);
    assertNotNull("IJavaElement shouldn't be null", ije2);

    // check that when call compare on them, that the one that comes first
    // alphabetically is the first in the list
    AJComparator comp = new AJComparator();
    assertTrue("ije1 should be less than ije2", comp.compare(ije1, ije2) < 0);
    assertTrue("ije2 should be greater than ije1", comp.compare(ije2, ije1) > 0);
    assertTrue("ije1 should be equal to ije1", comp.compare(ije1, ije1) == 0);

}

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

License:Open Source License

public void testCompareAJCodeElementAndIJavaElement() {
    AJCodeElement ajce = null;/*from  w w w .  j  a  v a 2s  .c  o m*/
    IJavaElement ije = null;

    // get the aspect and create the map for the file (the underlying one)
    IFile main = aspectjPackage.getFile("Main.java");

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();
    Map annotationsMap = asm.getInlineAnnotations(main.getRawLocation().toOSString(), true, true);
    assertNotNull("annotation map should not be null for Main.java", annotationsMap);
    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();
            ISourceLocation sl = node.getSourceLocation();
            if (node.toLinkLabelString(false)
                    .equals("Main: method-call(void java.io.PrintStream.println(java.lang.String))")
                    && (sl.getLine() == 23)) {

                IJavaElement je = model.programElementToJavaElement(node);
                if (je instanceof AJCodeElement) {
                    ajce = (AJCodeElement) je;
                    break;
                }
            }
        }
    }

    IFile aspect = aspectjPackage.getFile("A.aj");

    asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();
    Map annotationsMap2 = asm.getInlineAnnotations(aspect.getRawLocation().toOSString(), true, true);
    assertNotNull("annotation map should not be null for Main.java", annotationsMap2);

    Set keys2 = annotationsMap2.keySet();
    for (Iterator it = keys2.iterator(); it.hasNext();) {
        Object key = it.next();
        List annotations = (List) annotationsMap2.get(key);
        for (Iterator it2 = annotations.iterator(); it2.hasNext();) {
            IProgramElement node = (IProgramElement) it2.next();
            ISourceLocation sl = node.getSourceLocation();
            if (node.toLinkLabelString(false).equals("A.after(String): tracedPrint..")
                    && (sl.getLine() == 30)) {

                IJavaElement je = model.programElementToJavaElement(node);
                if (!(je instanceof AJCodeElement)) {
                    ije = je;
                    break;
                }
            }
        }
    }

    assertNotNull("AJCodeElement shouldn't be null", ajce);
    assertNotNull("IJavaElement shouldn't be null", ije);

    // check that when call compare on them, that the one that comes first
    // alphabetically is the first in the list
    AJComparator comp = new AJComparator();
    assertTrue("ije should be less than ajce", comp.compare(ije, ajce) < 0);
    assertTrue("ajce should be greater than ije", comp.compare(ajce, ije) > 0);

}

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();//from w  w  w.j  a va2s . c  o  m
    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.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();//from  w  w  w . j  a  v a  2  s.  co  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.AJProjectModelTest.java

License:Open Source License

protected void setUp() throws Exception {
    super.setUp();

    project = createPredefinedProject("AJProject83082"); //$NON-NLS-1$
    model = AJProjectModelFactory.getInstance().getModelForProject(project);

    IFolder src = project.getFolder("src"); //$NON-NLS-1$
    IFolder wpstest = src.getFolder("wpstest"); //$NON-NLS-1$
    IFolder aspectjPackage = wpstest.getFolder("aspectj"); //$NON-NLS-1$
    IFile main = aspectjPackage.getFile("Main.java"); //$NON-NLS-1$

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();/*w  w w. j  av a 2  s .c  o m*/
    Map annotationsMap = asm.getInlineAnnotations(main.getRawLocation().toOSString(), true, true);
    ajCodeElements = createAJCodeElements(annotationsMap);
}

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

License:Open Source License

protected void setUp() throws Exception {
    super.setUp();

    project = createPredefinedProject("AJProject83082Second"); //$NON-NLS-1$
    model = AJProjectModelFactory.getInstance().getModelForProject(project);

    IFolder src = project.getFolder("src"); //$NON-NLS-1$
    IFolder wpstest = src.getFolder("wpstest"); //$NON-NLS-1$
    IFolder aspectjPackage = wpstest.getFolder("aspectj"); //$NON-NLS-1$
    IFile main = aspectjPackage.getFile("Main.java"); //$NON-NLS-1$

    AsmManager asm = AspectJPlugin.getDefault().getCompilerFactory().getCompilerForProject(project.getProject())
            .getModel();//from   www.j  a  v a  2s  . c  om
    Map annotationsMap = asm.getInlineAnnotations(main.getRawLocation().toOSString(), true, true);
    ajCodeElements = createAJCodeElements(annotationsMap);
}