1

我已经构建了一个程序,它接收提供的“.class”文件并使用 BCEL 解析它,我现在已经学会了如何计算 LCOM4 值。现在我想知道如何计算类文件的 CBO(对象之间的耦合)值。我已经搜索了整个网络,试图找到一个合适的教程,但到目前为止我一直无法(我也阅读了关于 BCEL 的整个 javadoc 并且在 stackoverflow 上有一个类似的问题,但它一直是删除)。所以我想在这个问题上得到一些帮助,比如一些详细的教程或代码片段,可以帮助我理解如何去做。

4

1 回答 1

0

好的,在这里你必须计算一整组类中的类的 CBO。该集合可以是目录、jar 文件或类路径中的所有类的内容。

我会用类名作为键填充 Map<String,Set<String>>,它引用的类:

private void addClassReferees(File file, Map<String, Set<String>> refMap)
        throws IOException {
    try (InputStream in = new FileInputStream(file)) {
        ClassParser parser = new ClassParser(in, file.getName());
        JavaClass clazz = parser.parse();
        String className = clazz.getClassName();
        Set<String> referees = new HashSet<>();
        ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
        for (Method method: clazz.getMethods()) {
            Code code = method.getCode();
            InstructionList instrs = new InstructionList(code.getCode());
            for (InstructionHandle ih: instrs) {
                Instruction instr = ih.getInstruction();
                if (instr instanceof FieldOrMethod) {
                    FieldOrMethod ref = (FieldInstruction)instr;
                    String cn = ref.getClassName(cp);
                    if (!cn.equals(className)) {
                        referees.add(cn);
                    }
                }
            }
        }
        refMap.put(className, referees);
    }
}

当您在地图中添加了所有类后,您需要过滤每个类的裁判,以将它们限制在所考虑的类集内,并添加反向链接:

            Set<String> classes = new TreeSet<>(refMap.keySet());
            for (String className: classes) {
                Set<String> others = refMap.get(className);
                others.retainAll(classes);
                for (String other: others) {
                    refMap.get(other).add(className);
                }
            }
于 2017-05-19T07:00:35.610 回答