简体   繁体   中英

Eclipse AST variable binding on standalone java application

I'm trying to use Eclipse ASTParser in order to analyse and, if possible, add some code to some classes. One of the information I need requires to have bindings, but because this is a standalone project (the final goal it's a command line tool, independent from eclipse) I can't have them ( requireBinding() returns null ).

After reading a lot of posts, the far that I can go is using this examples in order to use FileASTRequestor but that's not the way to go since it seems to me that we have to give the KEY to bind before generating the AST tree.

I've found somewhere that we can use ASTParser.setEnvironment method in order to use the bindings in a standalone java application, but I don't think I'm doing it correctly. What's wrong with the code below?

private static final String rootDir = "D:\\workspace\\stateless\\";
    private static final String[] classpath = java.lang.System.getProperty( "java.class.path" ).split(";");

    private static final String source = 
            "package de.siemens.tools.stateless.test.examples; " +
            "public class ClassWithFinalMemberVariables {" +
            "private final int _memberIntVariable = 0;" +
            "public void method() {" +
            "int localVariable = 0;" +
            "System.out.println(_memberIntVariable + localVariable);" +
            "}" +
            "}";

    public static void main(String[] args) throws CoreException {

        Document document = new Document(source);
        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setEnvironment(classpath, new String[] { rootDir }, 
new String[] { "UTF8" }, true);  
        parser.setSource(document.get().toCharArray());
        parser.setResolveBindings(true);
        parser.setBindingsRecovery(true);
        CompilationUnit unit = (CompilationUnit)parser.createAST(null);

        unit.recordModifications();

        unit.accept(new ASTVisitor() {

@Override
            public void endVisit(VariableDeclarationFragment node) {

                IVariableBinding bind = node.resolveBinding();

                if(bind == null) 
                   System.out.println("ERROR: bind is null");

                super.endVisit(node);
            }

Output is always " ERROR: bind is null ".

I've already solved it, the code is here: http://pasteit.com/19433

Even though I prefer the ASTVisitor model, this one gives me every binding available.

And here is the discussion about the problem, for those of you who are curious: https://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

EDIT: I don't have any idea if this is the best solution or not, if you have any suggestion please let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM