0

我需要从源文件中计算 Java 程序的传出耦合(对象之间的耦合)。

我已经在 Eclipse 中使用 jdt 提取抽象语法树,但我不确定是否可以直接从另一个类中提取类依赖项。

我不能使用任何公制插件。

谢谢你的帮助。

4

1 回答 1

1

您可以使用 anASTVisitor检查 AST 中的相关节点。然后,您可以使用resolveBinding()resolveTypeBinding()提取依赖项。(为此,您需要在解析时打开“resolveBindings”。)

我没有对此进行测试,但这个例子应该给你一个想法:

public static IType[] findDependencies(ASTNode node) {
    final Set<IType> result = new HashSet<IType>();
    node.accept(new ASTVisitor() {
        @Override
        public boolean visit(SimpleName node) {
            ITypeBinding typeBinding = node.resolveTypeBinding();
            if (typeBinding == null)
                return false;
            IJavaElement element = typeBinding.getJavaElement();
            if (element != null && element instanceof IType) {
                result.add((IType)element);
            }
            return false;
        }
    });
    return result.toArray(new IType[result.size()]);
}
于 2014-02-11T10:16:08.453 回答