【问题标题】:Calculating Efferent Coupling in Java在 Java 中计算传出耦合
【发布时间】:2014-03-07 07:32:32
【问题描述】:

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

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

我不能使用任何指标插件。

感谢您的帮助。

【问题讨论】:

    标签: java object eclipse-jdt coupling cbo


    【解决方案1】:

    您可以使用ASTVisitor 来检查 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()]);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      相关资源
      最近更新 更多