List of usage examples for org.eclipse.jdt.core.dom CompilationUnit setProperty
public final void setProperty(String propertyName, Object data)
null to clear it. From source file:com.motorolamobility.preflighting.core.internal.utils.ProjectUtils.java
License:Apache License
private static void visitFolderToIdentifyClasses(File sourceFile, List<CompilationUnit> list, IPath projectPath, List<File> classPathFiles) throws PreflightingToolException { try {// w w w. ja v a 2 s. com if (sourceFile.isFile()) { // is a java file if (sourceFile.getName().endsWith(".java")) { FileReader reader = null; CharBuffer cb = null; try { reader = new FileReader(sourceFile); cb = CharBuffer.allocate((int) sourceFile.length()); int count = reader.read(cb); cb.flip(); // verify if all bytes were read if (count == sourceFile.length()) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(cb.array()); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options); parser.setCompilerOptions(options); List<String> classPathList = new ArrayList<String>(classPathFiles.size()); for (File file : classPathFiles) { classPathList.add(file.getAbsolutePath()); } String[] classpathEntries = classPathList.toArray(new String[classPathFiles.size()]); File srcFolder = new File(projectPath.toFile(), "src"); File genFolder = new File(projectPath.toFile(), "gen"); int sourcepathEntriesSize = 0; if (srcFolder.exists()) { sourcepathEntriesSize++; } if (genFolder.exists()) { sourcepathEntriesSize++; } String[] sourcepathEntries = new String[sourcepathEntriesSize]; if (sourcepathEntriesSize == 1) { sourcepathEntries[0] = srcFolder.getAbsolutePath(); } if (sourcepathEntriesSize == 2) { sourcepathEntries[0] = srcFolder.getAbsolutePath(); sourcepathEntries[1] = genFolder.getAbsolutePath(); } parser.setEnvironment(classpathEntries, sourcepathEntries, null, true); parser.setUnitName(computeRelativePath(projectPath, sourceFile.getAbsolutePath())); parser.setResolveBindings(true); ASTNode nodes = parser.createAST(null); if (nodes.getNodeType() == ASTNode.COMPILATION_UNIT) { CompilationUnit cu = (CompilationUnit) nodes; cu.setProperty(JAVA_FILE_PROPERTY, sourceFile); list.add(cu); } } else { DebugVerboseOutputter.printVerboseMessage( PreflightingCoreNLS.ProjectUtils_ErrorReadingSourceFile + sourceFile.getName(), VerboseLevel.v1); } } // syntax error catch (Exception syntaxException) { DebugVerboseOutputter.printVerboseMessage( PreflightingCoreNLS.ProjectUtils_ErrorReadingSourceFile + sourceFile.getName(), VerboseLevel.v1); } finally { if (cb != null) { cb.clear(); } if (reader != null) { reader.close(); } } } } else if (sourceFile.isDirectory()) { File[] subDirs = sourceFile.listFiles(); for (int i = 0; i < subDirs.length; i++) { visitFolderToIdentifyClasses(subDirs[i], list, projectPath, classPathFiles); } } } catch (Exception e) { throw new PreflightingToolException(PreflightingCoreNLS.ProjectUtils_ErrorReadingSourceFile, e); } }
From source file:nl.han.ica.core.parser.ASTRequestor.java
@Override public void acceptAST(String sourceFilePath, CompilationUnit ast) { super.acceptAST(sourceFilePath, ast); compilationUnits.add(ast);//w ww. j a v a 2 s . co m for (SourceFile sourceFile : sourceFiles) { if (sourceFile.getFile().getAbsolutePath().equals(sourceFilePath)) { sourceFile.setCompilationUnit(ast); ast.setProperty(SourceFile.SOURCE_FILE_PROPERTY, sourceFile); } } }
From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTFacadeHelper.java
License:Open Source License
@Override public ASTJCompilationUnit createCompilationUnit(String name, String contents) { // set source char[] contentAsCharArray = contents.toCharArray(); ASTParser astParser = createASTParser(); astParser.setSource(contentAsCharArray); // parse/* ww w . ja v a 2s .c om*/ CompilationUnit astCompilationUnit = (CompilationUnit) astParser.createAST(null); Diagnostic diagnostic = analyzeCompilationUnit(astCompilationUnit, contents); if (diagnostic != Diagnostic.OK_INSTANCE) { StringBuilder message = new StringBuilder(diagnostic.getMessage()); for (Diagnostic childDiagnostic : diagnostic.getChildren()) { message.append("\n\t").append(childDiagnostic.getMessage()); } message.append(contents); CodeGenPlugin.INSTANCE.log(message.toString()); if (diagnostic.getSeverity() == Diagnostic.ERROR) { throw new WrappedException(new DiagnosticException(diagnostic)); } } // create rewriter to record changes ASTRewrite rewriter = ASTRewrite.create(astCompilationUnit.getAST()); // keep comments between nodes when removing or moving nodes rewriter.setTargetSourceRangeComputer(new CommentAwareSourceRangeComputer(astCompilationUnit, contents)); // set properties astCompilationUnit.setProperty(ASTJCompilationUnit.NAME_PROPERTY, name); // create JNode and set properties ASTJCompilationUnit compilationUnit = (ASTJCompilationUnit) convertToNode(astCompilationUnit); compilationUnit.setOriginalContents(contentAsCharArray); compilationUnit.setRewriter(rewriter); return compilationUnit; }