2

このコードでは、prosseek.B#bar() メソッドが prosseek.SuperA#foo() を呼び出します。

package prosseek;

public class SuperA {
    int i = 0;
    public void foo()
    {
        System.out.println(i);
    }
}

public class B {
    public void bar()
    {
        SuperA a = new SuperA();
        a.foo();
    }
}

bar() で呼び出された foo() のタイプを検出する必要があります。ASTVisitor を使用して MethodInvocation コード (a.foo()) を検出していますが、そこから型を取得するにはどうすればよいかわかりませんSuperA

ICompilationUnit icu = type.getCompilationUnit();
final ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(icu);
parser.setResolveBindings(true); // we need bindings later on
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

unit.accept(new ASTVisitor() {
    public boolean visit(MethodInvocation methodInvocation)
    {
        // ???

        return false;
    }
}

追加した

JDTの基本チュートリアルからヒントを得ました。

ここに画像の説明を入力

次のコードを試しました:

IBinding binding = methodInvocation.resolveTypeBinding();
IType type = (IType)binding.getJavaElement();
if (type == null)
    return false;

しかし、 binding.getJavaElement(); の戻り値に null を取得しました。

4

3 に答える 3

7

MethodInvocationの型を取得する代わりに、 Expressionから型を取得する必要がある場合があります。私はこれをテストしていませんが、これが役立つかもしれません。

public boolean visit(MethodInvocation node) {
    Expression exp = node.getExpression();
    ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
    System.out.println("Type: " + typeBinding.toString());
}
于 2013-01-21T05:58:46.573 に答える
1

おそらく以下から null 値を取得しています。

methodInvocation.resolveTypeBinding();

パーサーを正しく設定していないためです。resolveTypeBinding の JDT ドキュメントによると:

「この式の型のバインディングを解決して返します。AST の構築時に要求されない限り、バインディングは通常利用できないことに注意してください。」( http://goo.gl/IAUtR8 )

バインディングを有効にするには、パーサーでこのメソッドを呼び出す必要があります。

parser.setBindingsRecovery(true);

于 2014-02-18T03:35:15.313 に答える
0

このコードを使用して、ITypeを取得できます。メソッドの検索、ICompilationUnitの検索、メソッドを含むクラスの検索。

コードはかなり馬鹿げていると思います。IMethodからITypeを返すメソッドを提供するメソッドがあるかもしれません。

public boolean visit(MethodInvocation methodInvocation)
{
    //IBinding binding = methodInvocation.resolveTypeBinding();
    IBinding binding = methodInvocation.resolveMethodBinding();
    IMethod method = (IMethod)binding.getJavaElement();
    ICompilationUnit cu = method.getCompilationUnit();
    IType type = null;
    try {
        IType[] types = cu.getTypes();
        if (types.length == 1) {
            type = types[0];
        }
        else {
            for (IType t : types)
            {
                IMethod[] methods = t.getMethods();
                for (IMethod m : methods)
                {
                    if (m == method) {
                        type = t;
                        break;
                    }
                }
                if (type != null)
                    break;
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

追加した

このコードは私には問題なく機能します。

public boolean visit(MethodInvocation node)
{
    String methodName = node.getName().toString();
    ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
    IType type = (IType)typeBinding.getJavaElement();

    System.out.printf("Type %s (method %s) calls %s\n", typeName, methodName, type.getFullyQualifiedName());
    return false;
}
于 2013-01-20T19:49:49.463 に答える