CompilationUnit buildCompilationUnit(BuildContext file) { IJavaProject javaProject = JavaCore.create(file.getFile().getProject()); ASTParser p = ASTParser.newParser(JavaConstants.AST_LEVEL); p.setProject(javaProject); p.setSource(file.getContents()); p.setResolveBindings(true); p.setKind(ASTParser.K_COMPILATION_UNIT); p.setUnitName(file.getFile().getName()); return (CompilationUnit) p.createAST(null); }
/** * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies * into account. (regression test for bug 39533 Working copy with no corresponding file not * considered by NameLookup) * * @deprecated using deprecated code */ public void testParseCompilationUnit1() throws CoreException { ICompilationUnit workingCopy1 = null; ICompilationUnit workingCopy2 = null; try { TestWorkingCopyOwner owner = new TestWorkingCopyOwner(); workingCopy1 = getCompilationUnit("P/X.java").getWorkingCopy(owner, null); workingCopy1.getBuffer().setContents("public class X implements I {\n" + "}"); workingCopy1.makeConsistent(null); workingCopy2 = getCompilationUnit("P/I.java").getWorkingCopy(owner, null); workingCopy2.getBuffer().setContents("public interface I {\n" + "}"); workingCopy2.makeConsistent(null); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(workingCopy1); parser.setResolveBindings(true); parser.setWorkingCopyOwner(owner); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List types = cu.types(); assertEquals("Unexpected number of types in AST", 1, types.size()); TypeDeclaration type = (TypeDeclaration) types.get(0); ITypeBinding typeBinding = type.resolveBinding(); assertTypeBindingsEqual("Unexpected interfaces", "I", typeBinding.getInterfaces()); } finally { if (workingCopy1 != null) { workingCopy1.discardWorkingCopy(); } if (workingCopy2 != null) { workingCopy2.discardWorkingCopy(); } } }
/** * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies * into account. * * @deprecated using deprecated code */ public void testParseCompilationUnit3() throws CoreException { try { createJavaProject("P1", new String[] {"src"}, new String[] {"JCL_LIB", "lib"}, "bin"); // create X.class in lib folder /* Evaluate the following in a scrapbook: org.eclipse.jdt.core.tests.model.ModifyingResourceTests.generateClassFile( "X", "public class X {\n" + "}") */ byte[] bytes = new byte[] { -54, -2, -70, -66, 0, 3, 0, 45, 0, 13, 1, 0, 1, 88, 7, 0, 1, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 7, 0, 3, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0, 5, 0, 6, 10, 0, 4, 0, 8, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 6, 88, 46, 106, 97, 118, 97, 0, 33, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 9, -79, 0, 0, 0, 1, 0, 10, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12, }; this.createFile("P1/lib/X.class", bytes); // create libsrc and attach source createFolder("P1/libsrc"); createFile("P1/libsrc/X.java", "public class X extends Y {\n" + "}"); IPackageFragmentRoot lib = getPackageFragmentRoot("P1/lib"); lib.attachSource(new Path("/P1/libsrc"), null, null); // create Y.java in src folder createFile("P1/src/Y.java", ""); // create working copy on Y.java TestWorkingCopyOwner owner = new TestWorkingCopyOwner(); this.workingCopy = getCompilationUnit("P1/src/Y.java").getWorkingCopy(owner, null); this.workingCopy.getBuffer().setContents("public class Y {\n" + "}"); this.workingCopy.makeConsistent(null); // parse and resolve class file IClassFile classFile = getClassFile("P1/lib/X.class"); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(classFile); parser.setResolveBindings(true); parser.setWorkingCopyOwner(owner); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List types = cu.types(); assertEquals("Unexpected number of types in AST", 1, types.size()); TypeDeclaration type = (TypeDeclaration) types.get(0); ITypeBinding typeBinding = type.resolveBinding(); ITypeBinding superType = typeBinding.getSuperclass(); assertEquals( "Unexpected super type", "Y", superType == null ? "<null>" : superType.getQualifiedName()); } finally { deleteProject("P1"); } }
private void assertProblems( String expectedProblems, String path, String source, WorkingCopyOwner owner) throws JavaModelException { this.workingCopy = getWorkingCopy(path, source); ASTParser parser = ASTParser.newParser(JLS3_INTERNAL); parser.setSource(this.workingCopy); parser.setResolveBindings(true); parser.setWorkingCopyOwner(owner); CompilationUnit cu = (CompilationUnit) parser.createAST(null); assertProblems("Unexpected problems", expectedProblems, cu.getProblems(), source.toCharArray()); }
/** * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies * into account. * * @deprecated using deprecated code */ public void testParseCompilationUnit2() throws CoreException { TestWorkingCopyOwner owner = new TestWorkingCopyOwner(); this.workingCopy = getCompilationUnit("P/Y.java").getWorkingCopy(owner, null); this.workingCopy.getBuffer().setContents("public class Y {\n" + "}"); this.workingCopy.makeConsistent(null); char[] source = ("public class Z extends Y {\n" + "}").toCharArray(); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(source); parser.setUnitName("Z.java"); parser.setProject(getJavaProject("P")); parser.setWorkingCopyOwner(owner); parser.setResolveBindings(true); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List types = cu.types(); assertEquals("Unexpected number of types in AST", 1, types.size()); TypeDeclaration type = (TypeDeclaration) types.get(0); ITypeBinding typeBinding = type.resolveBinding(); assertNotNull("No binding", typeBinding); assertEquals("Unexpected super type", "Y", typeBinding.getSuperclass().getQualifiedName()); }