简体   繁体   English

独立 Java 应用程序上的 Eclipse AST 变量绑定

[英]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.我正在尝试使用 Eclipse ASTParser 进行分析,并在可能的情况下向某些类添加一些代码。 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 ).我需要绑定的信息之一,但因为这是一个独立的项目(最终目标是一个命令行工具,独立于 eclipse)我不能拥有它们( requireBinding()返回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.在阅读了很多帖子后,我可以使用这个示例来使用FileASTRequestor但这不是要走的路,因为在我看来,我们必须在生成 AST 树之前提供要绑定的 KEY。

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.我发现我们可以在某个地方使用ASTParser.setEnvironment方法来在独立的 Java 应用程序中使用绑定,但我认为我做得不对。 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 ".输出总是“ ERROR: bind is null ”。

I've already solved it, the code is here: http://pasteit.com/19433我已经解决了,代码在这里: http : //pasteit.com/19433

Even though I prefer the ASTVisitor model, this one gives me every binding available.尽管我更喜欢 ASTVisitor 模型,但这个模型为我提供了所有可用的绑定。

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这里是关于这个问题的讨论,对于那些好奇的人: 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编辑:我不知道这是否是最好的解决方案,如果您有任何建议,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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