use of net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter in project pmd-eclipse-plugin by pmd.
the class PMDGenerateASTAction method generateAST.
/**
* Generate a AST for a file
*
* @param file
* a file
*/
private void generateAST(IFile file) {
LOG.info("Generating AST for file " + file.getName());
ByteArrayOutputStream byteArrayOutputStream = null;
ByteArrayInputStream astInputStream = null;
try {
JavaParser parser = new JavaParser(new JavaCharStream(file.getContents()));
parser.setJdkVersion(Integer.MAX_VALUE);
ASTCompilationUnit compilationUnit = parser.CompilationUnit();
byteArrayOutputStream = new ByteArrayOutputStream();
IAstWriter astWriter = PMDPlugin.getDefault().getAstWriter();
astWriter.write(byteArrayOutputStream, compilationUnit);
byteArrayOutputStream.flush();
IFile astFile = createASTFile(file);
if (astFile != null) {
if (astFile.exists()) {
astFile.delete(false, null);
}
astInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
astFile.create(astInputStream, false, null);
}
} catch (CoreException e) {
showErrorById(StringKeys.ERROR_CORE_EXCEPTION, e);
} catch (ParseException e) {
showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
} catch (WriterException e) {
showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
} catch (IOException e) {
showErrorById(StringKeys.ERROR_IO_EXCEPTION, e);
} finally {
IOUtil.closeQuietly(byteArrayOutputStream);
IOUtil.closeQuietly(astInputStream);
}
}
Aggregations