private static void parseAST(String[] srcFiles, Charset srcCharset, String[] classPathEntries, FileASTRequestor requestor) { ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setEnvironment(classPathEntries, null, null, true); String[] srcEncodings = new String[srcFiles.length]; for (int i = 0; i < srcEncodings.length; i++) { srcEncodings[i] = srcCharset.name(); } parser.createASTs( srcFiles, srcEncodings, new String[]{}, requestor, null); }
public void parseFiles(List<InputFile> files, final Handler handler) { // We need the whole SourceFile to correctly handle a parsed ADT, so we keep track of it here. final Map<String, InputFile> reverseMap = new LinkedHashMap<String, InputFile>(); for (InputFile file: files) { reverseMap.put(file.getPath(), file); } ASTParser parser = newASTParser(); FileASTRequestor astRequestor = new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit ast) { logger.fine("acceptAST: " + sourceFilePath); int errors = ErrorUtil.errorCount(); checkCompilationErrors(sourceFilePath, ast); if (errors == ErrorUtil.errorCount()) { handler.handleParsedUnit(reverseMap.get(sourceFilePath), ast); } } }; // JDT fails to resolve all secondary bindings unless there are the same // number of "binding key" strings as source files. It doesn't appear to // matter what the binding key strings should be (as long as they're non- // null), so the paths array is reused. String[] paths = reverseMap.keySet().toArray(new String[reverseMap.size()]); parser.createASTs(paths, getEncodings(paths.length), paths, astRequestor, null); }
/** * Create the ASTParser with the source files to generate ASTs for, and set up the * environment using ASTParser.setEnvironment. * * NOTE: This MUST be called before anything else in JRSRepair. */ public void buildASTs() throws Exception{ /* setEnvironment( * String[] classpathEntries, * String[] sourcepathEntries, * String[] encodings, * boolean includeRunningVMBootclasspath) */ parser.setEnvironment(this.classpaths, this.sourcepaths, null, true); parser.setResolveBindings(true); /* Set up the AST handler. We need to create LineCoverage and Statements classes to store * and filter the statements from the ASTs. */ FileASTRequestor fileASTRequestor = new MutationASTRequestor(sourceFileContents, scope, faultyLineCoverage, seedLineCoverage, faultyStatements, seedStatements); /* createASTs( * String[] sourceFilePaths, * String[] encodings, - the source file encodings (e.g., "ASCII", "UTF8", "UTF-16"). Can be set to null if platform encoding is sufficient. * String[] bindingKeys, * FileASTRequestor requestor, * IProgressMonitor monitor) */ parser.createASTs(sourceFilesArray, null, new String[] {}, fileASTRequestor, null); }
public void analyze(File rootFolder, List<String> javaFiles, final SDModel model) { postProcessReferences = new HashMap<RastNode, List<String>>(); postProcessSupertypes = new HashMap<RastNode, List<String>>(); final String projectRoot = rootFolder.getPath(); 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); } final String[] sourceFolders = this.inferSourceFolders(filesArray); final ASTParser parser = buildAstParser(sourceFolders); FileASTRequestor fileASTRequestor = new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit ast) { String relativePath = sourceFilePath.substring(projectRoot.length() + 1).replaceAll(systemFileSeparator, "/"); // IProblem[] problems = ast.getProblems(); // if (problems.length > 0) { // System.out.println("problems"); // } // try { char[] charArray = Util.getFileCharContent(new File(sourceFilePath), null); processCompilationUnit(relativePath, charArray, ast, model); } catch (IOException e) { throw new RuntimeException(e); } // catch (DuplicateEntityException e) { // debug e; // } } }; parser.createASTs((String[]) filesArray, null, emptyArray, fileASTRequestor, null); postProcessReferences(model, postProcessReferences); postProcessReferences = null; postProcessSupertypes(model); postProcessSupertypes = null; }
private Set<Slice> generateExamples() { Set<Slice> slices = new HashSet<>(); File srcFile = new File(srcPath); String[] testPaths = getTestFiles(srcFile).parallelStream().map(File::getAbsolutePath).toArray(String[]::new); String[] folderPaths = getAllFiles(srcFile).parallelStream().map(File::getParentFile).map(File::getAbsolutePath).toArray(String[]::new); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setEnvironment(null, folderPaths, null, true); parser.setResolveBindings(true); Map<String, String> options = new Hashtable<>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); parser.setBindingsRecovery(true); parser.createASTs(testPaths, null, new String[]{}, new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit javaUnit) { APIMethodVisitor visitor = new APIMethodVisitor(); javaUnit.accept(visitor); slices.addAll(visitor.getSlices()); } }, null); return slices; }
private void createCompilationUnitsForJavaFiles(final File inputFolder) { // first process input folder to find java file processFolderToFindJavaClass(inputFolder); // now prepare ASTParser to process al java file found ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setStatementsRecovery(true); parser.setEnvironment(new String[] { inputFolder.getAbsolutePath() }, new String[] { inputFolder.getAbsolutePath() }, new String[] { "UTF-8" }, true); // add compiler compliance rules to convert enums @SuppressWarnings("unchecked") Hashtable<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); FileASTRequestor requestor = new CustomFileASTRequestor(getLog(), inputFolder.getAbsolutePath(), sourcePathEntry); parser.createASTs(sourcePathEntry.keySet().toArray(new String[0]), charsetEntry.toArray(new String[0]), new String[] { "" }, requestor, null); }
private void analyze(File rootFolder, List<String> javaFiles, final SDModel.Snapshot model) { postProcessReferences = new HashMap<SDEntity, List<String>>(); postProcessSupertypes = new HashMap<SDType, List<String>>(); postProcessClientCode = new HashMap<String, List<SourceRepresentation>>(); final String projectRoot = rootFolder.getPath(); 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); } final String[] sourceFolders = this.inferSourceFolders(filesArray); final ASTParser parser = buildAstParser(sourceFolders); FileASTRequestor fileASTRequestor = new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit ast) { String relativePath = sourceFilePath.substring(projectRoot.length() + 1).replaceAll(systemFileSeparator, "/"); // IProblem[] problems = ast.getProblems(); // if (problems.length > 0) { // System.out.println("problems"); // } // try { char[] charArray = Util.getFileCharContent(new File(sourceFilePath), null); processCompilationUnit(relativePath, charArray, ast, model); } catch (IOException e) { throw new RuntimeException(e); } // catch (DuplicateEntityException e) { // debug e; // } } }; parser.createASTs((String[]) filesArray, null, emptyArray, fileASTRequestor, null); postProcessReferences(model, postProcessReferences); postProcessReferences = null; postProcessSupertypes(model); postProcessSupertypes = null; postProcessClientCode(model); postProcessClientCode = null; }
public static ElementInfoPool parse(String srcDir) { ElementInfoPool elementInfoPool = new ElementInfoPool(srcDir); Collection<File> javaFiles = FileUtils.listFiles(new File(srcDir), new String[]{"java"}, true); Set<String> srcPathSet = new HashSet<>(); Set<String> srcFolderSet = new HashSet<>(); for (File javaFile : javaFiles) { String srcPath = javaFile.getAbsolutePath(); String srcFolderPath = javaFile.getParentFile().getAbsolutePath(); srcPathSet.add(srcPath); srcFolderSet.add(srcFolderPath); } String[] srcPaths = new String[srcPathSet.size()]; srcPathSet.toArray(srcPaths); NameResolver.setSrcPathSet(srcPathSet); String[] srcFolderPaths = new String[srcFolderSet.size()]; srcFolderSet.toArray(srcFolderPaths); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setEnvironment(null, srcFolderPaths, null, true); parser.setResolveBindings(true); Map<String, String> options = new Hashtable<>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); parser.setBindingsRecovery(true); System.out.println(javaFiles.size()); parser.createASTs(srcPaths, null, new String[]{}, new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit javaUnit) { try { javaUnit.accept(new JavaASTVisitor(elementInfoPool, FileUtils.readFileToString(new File(sourceFilePath)))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, null); return elementInfoPool; }
@Override protected FileASTRequestor getRequestor(JavaFiles allJavaFiles) { return new AstRequestor(this, allJavaFiles); }
protected abstract FileASTRequestor getRequestor(JavaFiles allJavaFiles);