Java Code Examples for com.sun.tools.javac.code.Symtab#getClass()

The following examples show how to use com.sun.tools.javac.code.Symtab#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnonymousNumberingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCorrectAnonymousIndicesForMethodInvocations() throws Exception {
    String code = "package test;\n" +
                  "public class Test {\n" +
                  "    public Test main(Object o) {\n" +
                  "        return new Test().main(new Runnable() {\n" +
                  "            public void run() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        }).main(new Iterable() {\n" +
                  "            public java.util.Iterator iterator() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        });\n" +
                  "    }\n" +
                  "}";

    JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
    
    Iterable<? extends CompilationUnitTree> cuts = ct.parse();
    Iterable<? extends Element> analyze = ct.analyze();
    
    Symtab symTab = Symtab.instance(ct.getContext());
    Modules modules = Modules.instance(ct.getContext());
    TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
    TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));

    assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
}
 
Example 2
Source File: AnonymousNumberingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCorrectAnonymousIndicesForMultipleMethods() throws IOException {
    String code = "package test;\n" +
                  "public class Test {\n" +
                  "    public Test main1(Object o) {\n" +
                  "        new Runnable() {\n" +
                  "            public void run() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        };" +
                  "    }" +
                  "    public Test main2(Object o) {\n" +
                  "        new Iterable() {\n" +
                  "            public java.util.Iterator iterator() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        };\n" +
                  "    }\n" +
                  "    public Test main3(Object o) {\n" +
                  "        new java.util.ArrayList() {};\n" +
                  "    }\n" +
                  "}";

    JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));

    ct.analyze();

    Symtab symTab = Symtab.instance(ct.getContext());
    Modules modules = Modules.instance(ct.getContext());
    TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
    TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));
    TypeElement third = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$3"));

    assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.util.ArrayList", ((TypeElement) ((DeclaredType) third.getSuperclass()).asElement()).getQualifiedName().toString());
}
 
Example 3
Source File: AnonymousNumberingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCorrectNameForAnonymous() throws IOException {
    String code = "package test;\n" +
                  "public class Test {\n" +
                  "    public Test main1(Object o) {\n" +
                  "        new Runnable() {\n" +
                  "            public void run() {\n" +
                  "                throw new UnsupportedOperationException();\n" +
                  "            }\n" +
                  "        };" +
                  "        new Iterable() {\n" +
                  "            public java.util.Iterator iterator() {\n" +
                  "                new java.util.ArrayList() {};\n" +
                  "            }\n" +
                  "        };\n" +
                  "    }\n" +
                  "}";

    JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
    
    ct.analyze();

    Symtab symTab = Symtab.instance(ct.getContext());
    Modules modules = Modules.instance(ct.getContext());
    TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
    TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));
    TypeElement third = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2$1"));

    assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
    assertEquals("java.util.ArrayList", ((TypeElement) ((DeclaredType) third.getSuperclass()).asElement()).getQualifiedName().toString());
}