List of usage examples for org.eclipse.jdt.core.dom ASTParser setEnvironment
public void setEnvironment(String[] classpathEntries, String[] sourcepathEntries, String[] encodings, boolean includeRunningVMBootclasspath)
From source file:br.com.objectos.way.core.code.jdt.AstReaderResource.java
License:Apache License
@Override void setEnvironment(ASTParser parser) { String[] classpathEntries = FluentIterable.from(jarHints).transform(ToJar.INSTANCE).toArray(String.class); parser.setEnvironment(classpathEntries, null, null, true); }
From source file:br.uff.ic.mergeguider.javaparser.JavaParser.java
private Storage generateASTs(String path) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true);//from www . j a v a 2 s. co m parser.setBindingsRecovery(true); Map options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); parser.setCompilerOptions(options); String[] srcDirs = FileUtils.getAllDirs(path); String[] javaFiles = FileUtils.getAllJavaFiles(path); parser.setEnvironment(null, srcDirs, null, true); Storage storage = new Storage(); parser.createASTs(javaFiles, null, new String[0], storage, null); return storage; }
From source file:ch.acanda.eclipse.pmd.java.resolution.ASTQuickFixTestCase.java
License:Open Source License
private CompilationUnit createAST(final org.eclipse.jface.text.Document document, final ASTQuickFix<ASTNode> quickFix) { final ASTParser astParser = ASTParser.newParser(AST.JLS4); astParser.setSource(document.get().toCharArray()); astParser.setKind(ASTParser.K_COMPILATION_UNIT); astParser.setResolveBindings(quickFix.needsTypeResolution()); astParser.setEnvironment(new String[0], new String[0], new String[0], true); final String name = last(params.pmdReferenceId.or("QuickFixTest").split("/")); astParser.setUnitName(format("{0}.java", name)); final String version = last(params.language.or("java 1.7").split("\\s+")); astParser.setCompilerOptions(ImmutableMap.<String, String>builder().put(JavaCore.COMPILER_SOURCE, version) .put(JavaCore.COMPILER_COMPLIANCE, version).put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version) .build());/*from ww w.java2 s. c o m*/ final CompilationUnit ast = (CompilationUnit) astParser.createAST(null); ast.recordModifications(); return ast; }
From source file:com.facebook.nuclide.debugger.EvaluationManager.java
License:Open Source License
private CompilationUnit compileSource(String source, String sourceFilePath, String unitName, StringBuilder errors) throws Exception { if (!unitName.endsWith(".java")) { // The AST compiler is surprisingly insistent about this. unitName += ".java"; }//from w ww.j a v a 2 s.c o m final File sourceFile = new File(sourceFilePath); final String directoryPath = sourceFile.getAbsoluteFile().getParent(); Utils.logVerbose("compiling source for: " + directoryPath); final String[] sources = _contextManager.getSourceLocator().getSourceSearchPaths(); final String[] classpath = _contextManager.getSourceLocator().getBinaryJarPaths(); final ASTParser parser = ASTParser.newParser(AST.JLS8); final Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setUnitName(unitName); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setEnvironment(classpath, sources, null, true); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); String errorMsg = checkUnitForProblems(unit); if (errorMsg != null) { errors.append(errorMsg); } return unit; }
From source file:com.google.dart.java2dart.Context.java
License:Open Source License
/** * @return the Java AST of the given Java {@link File} in context of {@link #sourceFolders}. *//*from w ww.j av a 2 s . c om*/ private Map<File, CompilationUnit> parseJavaFiles(final List<File> javaFiles) throws Exception { String paths[] = new String[javaFiles.size()]; final Map<String, File> pathToFile = Maps.newHashMap(); for (int i = 0; i < javaFiles.size(); i++) { File javaFile = javaFiles.get(i); String javaPath = javaFile.getAbsolutePath(); paths[i] = javaPath; pathToFile.put(javaPath, javaFile); } // prepare Java parser ASTParser parser = ASTParser.newParser(AST.JLS4); { String[] classpathEntries = new String[classpathFiles.size()]; for (int i = 0; i < classpathFiles.size(); i++) { classpathEntries[i] = classpathFiles.get(i).getAbsolutePath(); } String[] sourceEntries = new String[sourceFolders.size()]; for (int i = 0; i < sourceFolders.size(); i++) { sourceEntries[i] = sourceFolders.get(i).getAbsolutePath(); } parser.setEnvironment(classpathEntries, sourceEntries, null, true); } parser.setResolveBindings(true); parser.setCompilerOptions(ImmutableMap.of(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5, JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED)); // do parse final Map<File, CompilationUnit> units = Maps.newLinkedHashMap(); parser.createASTs(paths, null, ArrayUtils.EMPTY_STRING_ARRAY, new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, org.eclipse.jdt.core.dom.CompilationUnit javaUnit) { File astFile = pathToFile.get(sourceFilePath); CompilationUnit dartUnit = SyntaxTranslator.translate(Context.this, javaUnit); units.put(astFile, dartUnit); } }, null); return units; }
From source file:com.google.devtools.j2cpp.GenerationTest.java
License:Open Source License
/** * Compiles Java source, as contained in a source file. * * @param name the name of the public type being declared * @param source the source code/*from ww w . j a v a 2s .c om*/ * @param assertErrors assert that no compilation errors were reported * @return the parsed compilation unit */ protected CompilationUnit compileType(String name, String source, boolean assertErrors) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setCompilerOptions(Options.getCompilerOptions()); parser.setSource(source.toCharArray()); parser.setResolveBindings(true); parser.setUnitName(name + ".java"); parser.setEnvironment(new String[] { getComGoogleDevtoolsJ2objcPath() }, new String[] { tempDir.getAbsolutePath() }, null, true); CompilationUnit unit = (CompilationUnit) parser.createAST(null); assertNoCompilationErrors(unit); return unit; }
From source file:com.google.devtools.j2cpp.J2ObjC.java
License:Open Source License
private static void setPaths(ASTParser parser) { // Add existing boot classpath after declared path, so that core files // referenced, but not being translated, are included. This only matters // when compiling the JRE emulation library sources. List<String> fullClasspath = Lists.newArrayList(); String[] classpathEntries = Options.getClassPathEntries(); for (int i = 0; i < classpathEntries.length; i++) { fullClasspath.add(classpathEntries[i]); }// www. j ava 2s . co m String bootclasspath = Options.getBootClasspath(); for (String path : bootclasspath.split(":")) { // JDT requires that all path elements exist and can hold class files. File f = new File(path); if (f.exists() && (f.isDirectory() || path.endsWith(".jar"))) { fullClasspath.add(path); } } parser.setEnvironment(fullClasspath.toArray(new String[0]), Options.getSourcePathEntries(), null, true); // Workaround for ASTParser.setEnvironment() bug, which ignores its // last parameter. This has been fixed in the Eclipse post-3.7 Java7 // branch. try { Field field = parser.getClass().getDeclaredField("bits"); field.setAccessible(true); int bits = field.getInt(parser); // Turn off CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH bits &= ~0x20; field.setInt(parser, bits); } catch (Exception e) { // should never happen, since only the one known class is manipulated e.printStackTrace(); System.exit(1); } }
From source file:com.google.devtools.j2objc.J2ObjC.java
License:Open Source License
private static void setPaths(ASTParser parser) { // Add existing boot classpath after declared path, so that core files // referenced, but not being translated, are included. This only matters // when compiling the JRE emulation library sources. List<String> fullClasspath = Lists.newArrayList(); String[] classpathEntries = Options.getClassPathEntries(); for (int i = 0; i < classpathEntries.length; i++) { fullClasspath.add(classpathEntries[i]); }//w w w. ja v a 2 s .c o m String bootclasspath = Options.getBootClasspath(); for (String path : bootclasspath.split(":")) { // JDT requires that all path elements exist and can hold class files. File f = new File(path); if (f.exists() && (f.isDirectory() || path.endsWith(".jar"))) { fullClasspath.add(path); } } parser.setEnvironment(fullClasspath.toArray(new String[0]), Options.getSourcePathEntries(), Options.getFileEncodings(), true); // Workaround for ASTParser.setEnvironment() bug, which ignores its // last parameter. This has been fixed in the Eclipse post-3.7 Java7 // branch. try { Field field = parser.getClass().getDeclaredField("bits"); field.setAccessible(true); int bits = field.getInt(parser); // Turn off CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH bits &= ~0x20; field.setInt(parser, bits); } catch (Exception e) { // should never happen, since only the one known class is manipulated e.printStackTrace(); System.exit(1); } }
From source file:com.google.devtools.j2objc.jdt.JdtParser.java
License:Apache License
@SuppressWarnings("deprecation") private ASTParser newASTParser(boolean resolveBindings, SourceVersion sourceVersion) { ASTParser parser; if (SourceVersion.java8Minimum(sourceVersion)) { parser = ASTParser.newParser(AST.JLS8); } else {//from w w w . j a v a 2 s . co m parser = ASTParser.newParser(AST.JLS4); // Java 7 } parser.setCompilerOptions(compilerOptions); parser.setResolveBindings(resolveBindings); parser.setEnvironment(toArray(classpathEntries), toArray(sourcepathEntries), getEncodings(sourcepathEntries.size()), includeRunningVMBootclasspath); return parser; }
From source file:com.google.devtools.j2objc.util.JdtParser.java
License:Apache License
private ASTParser newASTParser(boolean resolveBindings) { ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setCompilerOptions(compilerOptions); parser.setResolveBindings(resolveBindings); parser.setEnvironment(toArray(classpathEntries), toArray(sourcepathEntries), getEncodings(sourcepathEntries.size()), includeRunningVMBootclasspath); return parser; }