2

分析し、可能であれば、いくつかのクラスにいくつかのコードを追加するために、EclipseASTParserを使用しようとしています。必要な情報の1つはバインディングが必要ですが、これはスタンドアロンプ​​ロジェクトであるため(最終的な目標は、Eclipseから独立したコマンドラインツールです)、バインディングを作成することはできません(requireBinding()returns null)。

たくさんの投稿を読んだ後、私が行くことができるのは、使用するためにこの例FileASTRequestorを使用することですが、ASTツリーを生成する前にバインドするKEYを与える必要があるように思われるので、それは行く方法ではありません。

スタンドアロンのJavaアプリケーションでバインディングを使用するためにASTParser.setEnvironmentメソッドを使用できる場所をどこかで見つけましたが、正しく実行しているとは思いません。以下のコードの何が問題になっていますか?

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);
            }

出力は常に" ERROR: bind is null"です。

4

1 に答える 1

3

私はすでにそれを解決しました、コードはここにあります:http: //pasteit.com/19433

私はASTVisitorモデルを好みますが、これは利用可能なすべてのバインディングを提供します。

そして、興味のある方のために、この問題についての議論があります:https ://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

編集:これが最善の解決策であるかどうかはわかりません。何か提案があれば教えてください

于 2012-11-19T14:50:33.170 に答える