private ArrayList<CompilationUnitPair> parseCompilationUnits() { final ArrayList<CompilationUnitPair> pairs = new ArrayList<CompilationUnitPair>(_sourceFiles.length); FileASTRequestor requestor = new FileASTRequestor () { @Override public void acceptAST(String source, CompilationUnit ast) { pairs.add(new CompilationUnitPair(source, ast)); } }; _parser.setEnvironment(_classPathEntries, _sourcePathEntries, null, true); _parser.setResolveBindings(true); _parser.createASTs(_sourceFiles, null, new String[0], requestor, _progressMonitor); return pairs; }
/** * Will be called when the environment can not be derived from a context in {@link #createJavaParser(Object)} * {@link ASTParser#setEnvironment(String[], String[], String[], boolean)} */ protected void provideCustomEnvironment(final ASTParser parser) { final ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader(); final Function1<URL, String> _function = (URL it) -> { return it.getFile(); }; final Function1<String, Boolean> _function_1 = (String it) -> { return Boolean.valueOf(new File(it).exists()); }; final Iterable<String> cpEntries = IterableExtensions.<String>filter(ListExtensions.<URL, String>map(((List<URL>)Conversions.doWrapArray(((URLClassLoader) sysClassLoader).getURLs())), _function), _function_1); parser.setEnvironment(((String[])Conversions.unwrapArray(cpEntries, String.class)), null, null, true); } }
public CSCompilationUnit run() throws IOException { if (null == _writer || null == _source) { throw new IllegalStateException("source and writer must be set"); } File sourceFile = new File(_source); List<String> sourcefilePaths = new ArrayList<String>(); sourcefilePaths.add(sourceFile.getPath()); String []sourcefilearr = new String[sourcefilePaths.size()]; sourcefilePaths.toArray(sourcefilearr); _parser.setEnvironment(null,sourcefilearr, null, true); _parser.setSource(ReadFileToCharArray(_source)); _parser.setResolveBindings(true); CompilationUnit ast = (CompilationUnit) _parser.createAST(null); return run(ast); }
@Override public Void call() throws Exception { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setBindingsRecovery(false); parser.setResolveBindings(true); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); // these options seem to slightly reduce the number of times that JDT aborts on compilation errors options.put(JavaCore.CORE_INCOMPLETE_CLASSPATH, "warning"); options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, "warning"); options.put(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, "warning"); options.put(JavaCore.CORE_CIRCULAR_CLASSPATH, "warning"); options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, "warning"); options.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_VIOLATION, "warning"); options.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, "ignore"); options.put(JavaCore.COMPILER_PB_NULL_ANNOTATION_INFERENCE_CONFLICT, "warning"); options.put(JavaCore.CORE_OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE, "warning"); options.put(JavaCore.CORE_JAVA_BUILD_DUPLICATE_RESOURCE, "warning"); parser.setCompilerOptions(options); parser.setEnvironment(libraryPaths.toArray(new String[libraryPaths.size()]), sourcePaths.toArray(new String[sourcePaths.size()]), null, true); parser.createASTs(batch.toArray(new String[batch.size()]), encodings, bindingKeys, requestor, null); return null; } });
protected ASTParser constructASTParser(boolean resolveBindings, boolean errorRecovery, IString javaVersion, String[] sourcePath, String[] classPath) { @SuppressWarnings("deprecation") ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setResolveBindings(resolveBindings); parser.setBindingsRecovery(true); parser.setStatementsRecovery(errorRecovery); Hashtable<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_SOURCE, javaVersion.getValue()); options.put(JavaCore.COMPILER_COMPLIANCE, javaVersion.getValue()); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); parser.setEnvironment(classPath, sourcePath, null, true); return parser; }
/** * Primary method to trigger the importer after having defined the * (1) {@link JavaFiles} with files to be parsed, and * (2) {@link Classpath} with dependencies */ public void run(JavaFiles javaFiles, Classpath classpath) { ignoredRootPath = javaFiles.ignoredRootPath().replaceAll("\\\\", "/"); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); @SuppressWarnings("unchecked") Map<String, String> 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); parser.setEnvironment(classpath.paths(), new String[]{}, new String[]{}, true); String[] paths = javaFiles.paths(); String[] encodings = Arrays.stream(paths).map(path -> "UTF-8").toArray(String[]::new); parser.createASTs(paths, encodings, new String[0], getRequestor(javaFiles), null); }
public void execute(FileASTRequestor storage) { for(List<String> partition : partitions) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setBindingsRecovery(true); Map<?, ?> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setEnvironment(null, srcDirs, null, true); parser.createASTs(partition.toArray(new String[partition.size()]), null, new String[0], storage, null); } } }
private static CompilationUnit createCompilationUnit(String sourceFile, String unitName) { String source = readWithStringBuilder(sourceFile); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toCharArray()); // set source String[] classpathEntries = ...; String[] sourcepathEntries = ...; parser.setEnvironment(classpathEntries, sourcepathEntries, null, true); parser.setUnitName(unitName); parser.setResolveBindings(true); // optional // parser.setBindingsRecovery(true); return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse }
@Override public void loadAllSources(Set<ModuleEntry> modules) { List<String> sources = new LinkedList<>(); Map<String, ModuleEntry> sourceMap = HashMapFactory.make(); for(ModuleEntry m : modules) { if (m.isSourceFile()) { SourceFileModule s = (SourceFileModule)m; sourceMap.put(s.getAbsolutePath(), s); sources.add(s.getAbsolutePath()); } } String[] sourceFiles = sources.toArray(new String[0]); @SuppressWarnings("deprecation") final ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setEnvironment(libs, this.sources, null, false); Hashtable<String, String> options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_SOURCE, "1.8"); parser.setCompilerOptions(options); parser.createASTs(sourceFiles, null, new String[0], new ECJAstToIR(sourceMap), new NullProgressMonitor()); }
private static ASTParser buildAstParser(File srcFolder) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setResolveBindings(false); parser.setEnvironment(new String[0], new String[]{srcFolder.getPath()}, null, false); return parser; }
ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_COMPILATION_UNIT); // you must set compilation unit name, so I just name it as a path to source file parser.setUnitName(inputFilePath.toString()); //params classpathEntries, sourcepathEntries, encodings, IncludeRunningVMBootclasspath parser.setEnvironment(null, null, null, true); //fileContent is a char array of some source code parser.setSource(fileContent); parser.setResolveBindings(true); CompilationUnit unit = (CompilationUnit) parser.createAST(null);
String path ="C:\\Users\\pc\\workspace\\asttester\\src\\test\\Apple.java"; File file = new File(path); String str = Util.readFileToString(file); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setBindingsRecovery(true); Map options = JavaCore.getOptions(); parser.setCompilerOptions(options); String unitName = "Apple.java"; parser.setUnitName(unitName); String[] sources = { "C:\\Users\\pc\\workspace\\asttester\\src" }; String[] classpath = {"C:\\Program Files\\Java\\jre1.8.0_25\\lib\\rt.jar"}; // sources is the source dir of all your java files // classpath is the java jar containing all the needed classes parser.setEnvironment(classpath, sources, new String[] { "UTF-8"}, true); parser.setSource(str.toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null);
// creating ASTParser ASTParser parser = ASTParser.newParser(AST.JLS4); // you probably want to set the kind parser.setKind(ASTParser.K_COMPILATION_UNIT); // you must set compilation unit name, so I just name it as a path to source file parser.setUnitName(inputFilePath.toString()); // you must set environment params classpathEntries, sourcepathEntries, encodings, IncludeRunningVMBootclasspath parser.setEnvironment(null, null, null, true); // you probably want to set source parser.setSource(fileContent); // I can see that you actually want to do that :) parser.setResolveBindings(true); // creating compilation unit CompilationUnit unit = (CompilationUnit) parser.createAST(null);
public CKReport calculate(String path) { String[] srcDirs = FileUtils.getAllDirs(path); String[] javaFiles = FileUtils.getAllJavaFiles(path); log.info("Found " + javaFiles.length + " java files"); MetricsExecutor storage = new MetricsExecutor(() -> metrics()); List<List<String>> partitions = Lists.partition(Arrays.asList(javaFiles), MAX_AT_ONCE); log.info("Max partition size: " + MAX_AT_ONCE + ", total partitions=" + partitions.size()); for(List<String> partition : partitions) { log.info("Next partition"); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setBindingsRecovery(true); Map<?, ?> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setEnvironment(null, srcDirs, null, true); parser.createASTs(partition.toArray(new String[partition.size()]), null, new String[0], storage, null); } log.info("Finished parsing"); CKReport report = storage.getReport(); extras.update(report); return report; }
File file = new File(""); String srcCode = Files.toString(file, Charsets.UTF_8); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(srcCode.toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options); parser.setCompilerOptions(options); String[] classpath = {"", "", ""}; String[] sourcepath = {""}; parser.setEnvironment(classpath, sourcepath, null, true); parser.setUnitName(""); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setStatementsRecovery(true); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor2(cu));
ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setBindingsRecovery(true); Hashtable<String, String> options = JavaCore.getDefaultOptions(); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); parser.setCompilerOptions(options); parser.setEnvironment(classpath, sources, new String[] { "UTF-8", "UTF-8" }, true); parser.setSource(fileContents.toCharArray()); CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); IProblem[] problems = compilationUnit.getProblems(); if (problems != null && problems.length > 0) { logger.warn("Got {} problems compiling the source file: ", problems.length); for (IProblem problem : problems) { logger.warn("{}", problem); } } return compilationUnit;
parser.setResolveBindings(true); parser.setEnvironment( // apply classpath new String[] { "//home//user//Projects//SmartCopy//ASTParser_Test//bin" }, // null, null, true);
private static ASTParser buildAstParser(String[] sourceFolders) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setEnvironment(new String[0], sourceFolders, null, true); //parser.setEnvironment(new String[0], new String[]{"tmp\\refactoring-toy-example\\src"}, null, false); return parser; }
public parseJavaFile(String pathOfJavaFile, String[] sources, String[] classpath, String nameOfJavaFile) throws Exception { ASTParser parser = ASTParser.newParser(AST.JLS8);//choose source code analyzing strategy parser.setResolveBindings(true);// turn on binding strategy parser.setKind(ASTParser.K_COMPILATION_UNIT);// the source code is a file .java parser.setBindingsRecovery(true); parser.setCompilerOptions(JavaCore.getOptions()); parser.setUnitName(nameOfJavaFile); parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setSource(Utils.getFile(pathOfJavaFile).toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); if (cu.getAST().hasBindingsRecovery()) { // Now binding is activated. Do something else } }
private CompilationUnit createAST(final String content, String unitName) { Document doc = new Document(content); final ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setBindingsRecovery(true); parser.setStatementsRecovery(true); parser.setUnitName(unitName); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); String[] sources = { inputArgs.getSourceFolder() }; parser.setEnvironment(null, sources, null, true); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); if (!cu.getAST().hasBindingsRecovery()) { System.out.println("Binding not activated."); } return cu; }