use of org.eclipse.jdt.core.dom.ASTParser in project xtext-xtend by eclipse.
the class ASTParserFactory method createDefaultJavaParser.
protected final ASTParser createDefaultJavaParser(final String javaVersion) {
ASTParser parser = null;
final Hashtable<String, String> options = JavaCore.getOptions();
try {
parser = ASTParser.newParser(ASTParserFactory.asJLS(javaVersion));
JavaCore.setComplianceOptions(javaVersion, options);
} catch (final Throwable _t) {
if (_t instanceof IllegalArgumentException) {
parser = ASTParser.newParser(ASTParserFactory.asJLS(this.minParserApiLevel));
JavaCore.setComplianceOptions(this.minParserApiLevel, options);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(options);
parser.setStatementsRecovery(true);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
return parser;
}
use of org.eclipse.jdt.core.dom.ASTParser in project xtext-xtend by eclipse.
the class ASTParserFactory method createJavaParser.
/**
* @param context Contextual object from where to get the classpath entries (e.g. IProject or Module or <code>null</code>)
*/
public ASTParserFactory.ASTParserWrapper createJavaParser(final Object context) {
String targetJavaVersion = System.getProperty("java.specification.version");
if ((targetJavaVersion == null)) {
targetJavaVersion = this.minParserApiLevel;
}
final ASTParser parser = this.createDefaultJavaParser(targetJavaVersion);
this.provideCustomEnvironment(parser);
return new ASTParserFactory.ASTParserWrapper(targetJavaVersion, parser);
}
use of org.eclipse.jdt.core.dom.ASTParser in project xtext-xtend by eclipse.
the class JavaCodeAnalyzer method determinateJavaType.
public JavaCodeAnalyzer.JavaParseResult<? extends ASTNode> determinateJavaType(final String javaCode) {
ASTParser parser = this.parserFactory.createDefaultJavaParser(this.parserFactory.minParserApiLevel);
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(true);
ASTNode _createAST = parser.createAST(null);
CompilationUnit unit = ((CompilationUnit) _createAST);
int _size = unit.types().size();
boolean _greaterThan = (_size > 0);
if (_greaterThan) {
List<CompilationUnit> _singletonList = Collections.<CompilationUnit>singletonList(unit);
return new JavaCodeAnalyzer.JavaParseResult<CompilationUnit>(javaCode, ASTParser.K_COMPILATION_UNIT, _singletonList);
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
ASTNode root = parser.createAST(null);
if ((root instanceof TypeDeclaration)) {
List<BodyDeclaration> bodyDeclarations = ((TypeDeclaration) root).bodyDeclarations();
int _size_1 = bodyDeclarations.size();
boolean _greaterThan_1 = (_size_1 > 0);
if (_greaterThan_1) {
return new JavaCodeAnalyzer.JavaParseResult<BodyDeclaration>(javaCode, ASTParser.K_CLASS_BODY_DECLARATIONS, bodyDeclarations);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_STATEMENTS);
root = parser.createAST(null);
if ((root instanceof Block)) {
List<Statement> statements = ((Block) root).statements();
int _size_2 = statements.size();
boolean _greaterThan_2 = (_size_2 > 0);
if (_greaterThan_2) {
return new JavaCodeAnalyzer.JavaParseResult<Statement>(javaCode, ASTParser.K_STATEMENTS, statements);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_EXPRESSION);
root = parser.createAST(null);
if ((root instanceof Expression)) {
List<Expression> _singletonList_1 = Collections.<Expression>singletonList(((Expression) root));
return new JavaCodeAnalyzer.JavaParseResult<Expression>(javaCode, ASTParser.K_EXPRESSION, _singletonList_1);
}
return null;
}
use of org.eclipse.jdt.core.dom.ASTParser in project xtext-xtend by eclipse.
the class EclipseASTParserFactory method createJavaParser.
@Override
public ASTParserFactory.ASTParserWrapper createJavaParser(final Object context) {
if ((context instanceof IJavaElement)) {
final IJavaProject project = ((IJavaElement) context).getJavaProject();
final String projlevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
final ASTParser parser = this.createDefaultJavaParser(projlevel);
if ((context instanceof IJavaProject)) {
parser.setProject(project);
this.tweakOptions(parser, project);
} else {
if ((context instanceof ICompilationUnit)) {
parser.setSource(((ICompilationUnit) context));
this.tweakOptions(parser, ((ICompilationUnit) context).getJavaProject());
}
}
return new ASTParserFactory.ASTParserWrapper(projlevel, parser);
}
return super.createJavaParser(context);
}
use of org.eclipse.jdt.core.dom.ASTParser in project AutoRefactor by JnRouvignac.
the class CFGBuilderTest method testCFGBuilder.
@Test
public void testCFGBuilder() throws Exception {
final String sampleName = testName + ".java";
final File javaFile = new File("src/test/java/org/autorefactor/cfg", sampleName);
assertTrue(testName + ": sample in java file " + javaFile + " should exist", javaFile.exists());
final File dotFile = new File("src/test/resources/org/autorefactor/cfg", testName + ".dot");
assertTrue(testName + ": sample out dot file " + dotFile + " should exist", dotFile.exists());
final String dotSource = readAll(dotFile).trim();
final String javaSource = readAll(javaFile);
final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment("org.autorefactor.cfg");
final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, javaSource, true, null);
cu.getBuffer().setContents(javaSource);
cu.save(null, true);
final JavaProjectOptions options = newJavaProjectOptions(Release.javaSE("1.8"), 4);
final ASTParser parser = ASTParser.newParser(AST.JLS8);
autoRefactorHandlerResetParser(cu, parser, options);
final CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
final CFGBuilder builder = new CFGBuilder(javaSource, options);
final List<CFGBasicBlock> blocks = builder.buildCFG(astRoot);
final CFGBasicBlock block = blocks.get(methodDeclarationNb);
final String actual = new CFGDotPrinter().toDot(block).trim();
final File dotFileOut = new File("src/test/resources/org/autorefactor/cfg", testName + "_out.dot");
writeAll(dotFileOut, actual);
assertEquals(testName + ": wrong output;", dotSource, actual);
}
Aggregations