Search in sources :

Example 46 with ASTParser

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;
}
Also used : ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 47 with ASTParser

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);
}
Also used : ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 48 with ASTParser

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Statement(org.eclipse.jdt.core.dom.Statement) Expression(org.eclipse.jdt.core.dom.Expression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 49 with ASTParser

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);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 50 with ASTParser

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaProjectOptions(org.autorefactor.refactoring.JavaProjectOptions) ASTParser(org.eclipse.jdt.core.dom.ASTParser) File(java.io.File) Test(org.junit.Test)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)54 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)43 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)6 IProblem (org.eclipse.jdt.core.compiler.IProblem)6 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 File (java.io.File)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3