我已经构建了一个程序,它接受提供的“.class”文件并使用 BCEL 解析它,我现在已经学会了如何计算 LCOM4 值。现在我想知道如何计算类文件的CBO(对象之间的耦合)值。我已经搜索了整个网络,试图找到关于它的正确教程,但到目前为止我还无法找到(我也阅读了有关 BCEL 的整个 javadoc,并且在 stackoverflow 上有一个类似的问题,但它已经已删除)。因此,我需要一些有关此问题的帮助,例如一些详细的教程或代码片段,它们可以帮助我了解如何做到这一点。
好的,这里您必须计算整个类集中的类的 CBO。该集合可以是目录、jar 文件或类路径中所有类的内容。
我将使用类名作为键及其引用的类来填充 Map> :
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)