Example usage for org.eclipse.jdt.core.dom AST JLS3

List of usage examples for org.eclipse.jdt.core.dom AST JLS3

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom AST JLS3.

Prototype

int JLS3

To view the source code for org.eclipse.jdt.core.dom AST JLS3.

Click Source Link

Document

Constant for indicating the AST API that handles JLS3.

Usage

From source file:ASTParser.ParseJavaFile.java

private void parse(final String str, File outputFile) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(str.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    StringBuffer allComments = new StringBuffer();

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    for (Comment comment : (List<Comment>) cu.getCommentList()) {
        CommentVisitor commentVisitor = new CommentVisitor(cu, str);
        comment.accept(commentVisitor);//from   ww w  .  j  a va 2  s.c o  m
        //            System.out.println(commentVisitor.getAllComments().toString());
        allComments.append(commentVisitor.getAllComments().toString());
    }

    allComments = ParseWords.parseAllWords(allComments);
    writeToFile(allComments.toString(), outputFile);
}

From source file:br.ufal.cideei.util.CachedICompilationUnitParser.java

License:Open Source License

public CompilationUnit parse(IFile aFile) {
    // MISS/*from  w w w  .ja  va  2  s.  co  m*/
    if (!aFile.equals(this.file)) {
        //         System.out.println("CachedICompilationUnitParser: miss");
        /*
         * Lazy initializes the member ASTParser
         */
        if (parser == null) {
            parser = ASTParser.newParser(AST.JLS3);
            parser.setKind(ASTParser.K_COMPILATION_UNIT);
            parser.setResolveBindings(true);
        }

        /*
         * Create a ASTNode (a CompilationUnit) by reusing the parser;
         */
        ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(aFile);
        parser.setSource(compilationUnit);
        CompilationUnit jdtCompilationUnit = (CompilationUnit) parser.createAST(null);

        /*
         * Caches the result
         */
        this.file = aFile;
        return this.cu = jdtCompilationUnit;
    } // HIT
    else {
        return this.cu;
    }
}

From source file:br.uff.ic.gems.resources.ast.ASTExtractor.java

public void parser() throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    File file = new File(filePath);

    String stringFile = FileUtils.readFileToString(file);
    parser.setSource(stringFile.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    //Setting options
    Map options;//from w w w. j  a v  a 2  s  .com
    options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
    parser.setCompilerOptions(options);

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    Visitor visitor = new Visitor(cu);
    cu.accept(visitor);

    List<Comment> commentList = cu.getCommentList();

    for (Comment comment : commentList) {
        comment.accept(visitor);
    }

    languageConstructs = visitor.getLanguageConstructs();

}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static CompilationUnit getCU(IFile file, PPAOptions options) {
    CompilationUnit cu = null;//from w  w w.j  a  v  a 2  s . c  om
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        PPATypeRegistry registry = new PPATypeRegistry(
                (JavaProject) JavaCore.create(icu.getUnderlyingResource().getProject()));
        ASTNode node = null;
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        node = parser2.createAST(null);
        PPAEngine ppaEngine = new PPAEngine(registry, options);

        cu = (CompilationUnit) node;

        ppaEngine.addUnitToProcess(cu);
        ppaEngine.doPPA();
        ppaEngine.reset();
    } catch (JavaModelException jme) {
        // Retry with the file version.
        logger.warn("Warning while getting CU from PPA");
        logger.debug("Exception", jme);
        cu = getCU(file.getLocation().toFile(), options);
    } catch (Exception e) {
        logger.error("Error while getting CU from PPA", e);
    }

    return cu;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static CompilationUnit getCUNoPPA(IFile file) {
    CompilationUnit cu = null;/*from   w  w w  .j a va2s.  c o  m*/
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        ASTNode node = null;
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        node = parser2.createAST(null);
        cu = (CompilationUnit) node;
    } catch (Exception e) {
        logger.error("Error while getting CU without PPA", e);
    }

    return cu;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

/**
 * <p>//  w  ww  . j a  v a 2 s. c o m
 * </p>
 * 
 * @param units
 * @return
 */
public List<CompilationUnit> getCUsWithOnePPAPass(List<ICompilationUnit> units) {

    if (units.size() == 0)
        return new ArrayList<CompilationUnit>();

    final List<CompilationUnit> astList = new ArrayList<CompilationUnit>();
    try {

        // FIXME a hack to get the current project.
        JavaProject jproject = (JavaProject) JavaCore.create(units.get(0).getUnderlyingResource().getProject());
        PPATypeRegistry registry = new PPATypeRegistry(jproject);
        final PPAEngine ppaEngine = new PPAEngine(registry, new PPAOptions());

        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setProject((IJavaProject) jproject);

        ASTRequestor requestor = new ASTRequestor() {

            @Override
            public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
                astList.add(ast);
                ppaEngine.addUnitToProcess(ast);
            }
        };

        parser2.createASTs(units.toArray(new ICompilationUnit[units.size()]), new String[0], requestor, null);

        ppaEngine.doPPA();
        ppaEngine.reset();

    } catch (JavaModelException jme) {
        jme.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return astList;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static ASTNode getNode(IFile file, PPAOptions options, int kind) {
    ASTNode node = null;/*from w  w w  .  ja  va 2 s  .c o m*/
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        PPATypeRegistry registry = new PPATypeRegistry(
                (JavaProject) JavaCore.create(icu.getUnderlyingResource().getProject()));
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        parser2.setKind(kind);
        node = parser2.createAST(null);
        PPAEngine ppaEngine = new PPAEngine(registry, options);

        ppaEngine.addUnitToProcess(node);
        ppaEngine.doPPA();
        ppaEngine.reset();
    } catch (Exception e) {
        logger.error("Error while getting CU from PPA", e);
    }

    return node;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static String getPackageFromFile(File file) throws IOException {
    String packageName = "";
    PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
    parser2.setStatementsRecovery(true);
    parser2.setResolveBindings(true);/*  w  ww.  ja v  a 2  s  .c  o m*/
    parser2.setSource(PPAResourceUtil.getContent(file).toCharArray());
    CompilationUnit cu = (CompilationUnit) parser2.createAST(null);
    PackageDeclaration pDec = cu.getPackage();
    if (pDec != null) {
        packageName = pDec.getName().getFullyQualifiedName();
    }
    return packageName;
}

From source file:cideplus.ui.astview.Binding.java

License:Open Source License

public static String getEscapedStringLiteral(String stringValue) {
    StringLiteral stringLiteral = AST.newAST(AST.JLS3).newStringLiteral();
    stringLiteral.setLiteralValue(stringValue);
    return stringLiteral.getEscapedValue();
}

From source file:cideplus.ui.astview.Binding.java

License:Open Source License

public static String getEscapedCharLiteral(char charValue) {
    CharacterLiteral charLiteral = AST.newAST(AST.JLS3).newCharacterLiteral();
    charLiteral.setCharValue(charValue);
    return charLiteral.getEscapedValue();
}