public UMLModelASTReader(File rootFolder, ASTParser parser, List<String> javaFiles) { this.umlModel = new UMLModel(rootFolder.getPath()); this.projectRoot = rootFolder.getPath(); this.parser = parser; final String[] emptyArray = new String[0]; String[] filesArray = new String[javaFiles.size()]; for (int i = 0; i < filesArray.length; i++) { filesArray[i] = rootFolder + File.separator + javaFiles.get(i).replaceAll("/", systemFileSeparator); } FileASTRequestor fileASTRequestor = new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit ast) { String relativePath = sourceFilePath.substring(projectRoot.length() + 1).replaceAll(systemFileSeparator, "/"); processCompilationUnit(relativePath, ast); } }; this.parser.createASTs((String[]) filesArray, null, emptyArray, fileASTRequestor, null); }
public void createASTs(ICompilationUnit[] units, String[] bindingKeys, CleanUpASTRequestor requestor, IProgressMonitor monitor) { if (monitor == null) monitor= new NullProgressMonitor(); try { monitor.beginTask("", units.length); //$NON-NLS-1$ List list= Arrays.asList(units); int end= 0; int cursor= 0; while (cursor < units.length) { end= Math.min(end + MAX_AT_ONCE, units.length); List toParse= list.subList(cursor, end); createParser().createASTs((ICompilationUnit[])toParse.toArray(new ICompilationUnit[toParse.size()]), bindingKeys, requestor, new SubProgressMonitor(monitor, toParse.size())); cursor= end; } } finally { monitor.done(); } }
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; }
@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; } });
public static ITypeBinding[] createTypeBindings(TType[] types, IJavaProject project) { final Map<String, Object> mapping= new HashMap<>(); List<String> keys= new ArrayList<>(); for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); mapping.put(bindingKey, type); keys.add(bindingKey); } ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setProject(project); parser.setResolveBindings(true); parser.createASTs(new ICompilationUnit[0], keys.toArray(new String[keys.size()]), new ASTRequestor() { @Override public void acceptBinding(String bindingKey, IBinding binding) { mapping.put(bindingKey, binding); } }, null); ITypeBinding[] result= new ITypeBinding[types.length]; for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); Object value= mapping.get(bindingKey); if (value instanceof ITypeBinding) { result[i]= (ITypeBinding)value; } } return result; }
public static ITypeBinding[] createTypeBindings(TType[] types, IJavaProject project) { final Map<String, Object> mapping= new HashMap<>(); List<String> keys= new ArrayList<>(); for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); mapping.put(bindingKey, type); keys.add(bindingKey); } ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setProject(project); parser.setResolveBindings(true); parser.createASTs(new ICompilationUnit[0], keys.toArray(new String[keys.size()]), new ASTRequestor() { @Override public void acceptBinding(String bindingKey, IBinding binding) { mapping.put(bindingKey, binding); } }, null); ITypeBinding[] result= new ITypeBinding[types.length]; for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); Object value= mapping.get(bindingKey); if (value instanceof ITypeBinding) { result[i]= (ITypeBinding)value; } } return result; }
public static ITypeBinding[] createTypeBindings(TType[] types, IJavaProject project) { final Map mapping= new HashMap(); List keys= new ArrayList(); for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); mapping.put(bindingKey, type); keys.add(bindingKey); } ASTParser parser= ASTParser.newParser(AST.JLS3); parser.setProject(project); parser.setResolveBindings(true); parser.createASTs(new ICompilationUnit[0], (String[])keys.toArray(new String[keys.size()]), new ASTRequestor() { public void acceptBinding(String bindingKey, IBinding binding) { mapping.put(bindingKey, binding); } }, null); ITypeBinding[] result= new ITypeBinding[types.length]; for (int i= 0; i < types.length; i++) { TType type= types[i]; String bindingKey= type.getBindingKey(); Object value= mapping.get(bindingKey); if (value instanceof ITypeBinding) { result[i]= (ITypeBinding)value; } } return result; }
@Override public void loadAllSources(Set<ModuleEntry> modules) { // TODO: we might need one AST (-> "Object" class) for all files. // TODO: group by project and send 'em in // sort files into projects Map<IProject, Map<ICompilationUnit,EclipseSourceFileModule>> projectsFiles = new HashMap<>(); for (ModuleEntry m : modules) { assert m instanceof EclipseSourceFileModule : "Expecing EclipseSourceFileModule, not " + m.getClass(); EclipseSourceFileModule entry = (EclipseSourceFileModule) m; IProject proj = entry.getIFile().getProject(); if (!projectsFiles.containsKey(proj)) { projectsFiles.put(proj, new HashMap<ICompilationUnit,EclipseSourceFileModule>()); } projectsFiles.get(proj).put(JavaCore.createCompilationUnitFrom(entry.getIFile()), entry); } @SuppressWarnings("deprecation") final ASTParser parser = ASTParser.newParser(AST.JLS8); for (final Map.Entry<IProject,Map<ICompilationUnit,EclipseSourceFileModule>> proj : projectsFiles.entrySet()) { parser.setProject(JavaCore.create(proj.getKey())); parser.setResolveBindings(true); Set<ICompilationUnit> units = proj.getValue().keySet(); parser.createASTs(units.toArray(new ICompilationUnit[0]), new String[0], new JdtAstToIR(proj), null); } }
private ITypeBinding getExpectedTypeForGenericParameters() { char[][] chKeys= context.getExpectedTypesKeys(); if (chKeys == null || chKeys.length == 0) { return null; } String[] keys= new String[chKeys.length]; for (int i= 0; i < keys.length; i++) { keys[i]= String.valueOf(chKeys[0]); } final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setProject(compilationUnit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); final Map<String, IBinding> bindings= new HashMap<>(); ASTRequestor requestor= new ASTRequestor() { @Override public void acceptBinding(String bindingKey, IBinding binding) { bindings.put(bindingKey, binding); } }; parser.createASTs(new ICompilationUnit[0], keys, requestor, null); if (bindings.size() > 0) { return (ITypeBinding) bindings.get(keys[0]); } return null; }
/** * Returns the type binding of the expected type as it is contained in the * code completion context. * * @return the binding of the expected type */ private ITypeBinding getExpectedType() { char[][] chKeys= fInvocationContext.getCoreContext().getExpectedTypesKeys(); if (chKeys == null || chKeys.length == 0) return null; String[] keys= new String[chKeys.length]; for (int i= 0; i < keys.length; i++) { keys[i]= String.valueOf(chKeys[0]); } final ASTParser parser= ASTParser.newParser(AST.JLS3); parser.setProject(fCompilationUnit.getJavaProject()); parser.setResolveBindings(true); final Map bindings= new HashMap(); ASTRequestor requestor= new ASTRequestor() { public void acceptBinding(String bindingKey, IBinding binding) { bindings.put(bindingKey, binding); } }; parser.createASTs(new ICompilationUnit[0], keys, requestor, null); if (bindings.size() > 0) return (ITypeBinding) bindings.get(keys[0]); return null; }
/** * 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); } } }
@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()); }
/** * Returns the type binding of the expected type as it is contained in the * code completion context. * * @return the binding of the expected type */ private ITypeBinding getExpectedType() { char[][] chKeys= fInvocationContext.getCoreContext().getExpectedTypesKeys(); if (chKeys == null || chKeys.length == 0) return null; String[] keys= new String[chKeys.length]; for (int i= 0; i < keys.length; i++) { keys[i]= String.valueOf(chKeys[0]); } final ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setProject(fCompilationUnit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); final Map<String, IBinding> bindings= new HashMap<>(); ASTRequestor requestor= new ASTRequestor() { @Override public void acceptBinding(String bindingKey, IBinding binding) { bindings.put(bindingKey, binding); } }; parser.createASTs(new ICompilationUnit[0], keys, requestor, null); if (bindings.size() > 0) return (ITypeBinding) bindings.get(keys[0]); return null; }
parser.createASTs(new ICompilationUnit[0], keys, requestor, null);
/** * Returns the type binding of the expected type as it is contained in the * code completion context. * * @return the binding of the expected type */ private ITypeBinding getExpectedType() { char[][] chKeys= fInvocationContext.getCoreContext().getExpectedTypesKeys(); if (chKeys == null || chKeys.length == 0) return null; String[] keys= new String[chKeys.length]; for (int i= 0; i < keys.length; i++) { keys[i]= String.valueOf(chKeys[0]); } final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setProject(fCompilationUnit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); final Map<String, IBinding> bindings= new HashMap<>(); ASTRequestor requestor= new ASTRequestor() { @Override public void acceptBinding(String bindingKey, IBinding binding) { bindings.put(bindingKey, binding); } }; parser.createASTs(new ICompilationUnit[0], keys, requestor, null); if (bindings.size() > 0) return (ITypeBinding) bindings.get(keys[0]); return null; }
parser.createASTs(new ICompilationUnit[0], keys, requestor, 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; }
parser.setProject(project); parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project)); parser.createASTs(new ICompilationUnit[] { copy}, new String[0], new ASTRequestor() {
parser.createASTs(absolutePaths, encodings, new String[0], new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit ast) {