分析し、可能であれば、いくつかのクラスにいくつかのコードを追加するために、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
"です。