使用 BCEL 生成的解析字节码确定对象之间的传出耦合(CBO 指标)



>我构建了一个程序,它接收提供的".class"文件并使用BCEL对其进行解析,我现在学会了如何计算LCOM4值。现在我想知道如何计算类文件的CBO(对象之间的耦合(值。我已经搜索了整个网络,试图找到一个合适的教程,但到目前为止我无法(我也阅读了关于 BCEL 的整个 javadoc,并且有一个关于堆栈溢出的类似问题,但它已被删除(。因此,我需要有关此问题的一些帮助,例如一些详细的教程或代码片段,可以帮助我了解如何执行此操作。

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

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

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);
                }
            }

最新更新