【问题标题】:Determining the Efferent coupling between objects (CBO Metric) using the parsed byte-code generated by BCEL使用 BCEL 生成的已解析字节码确定对象之间的传出耦合(CBO 度量)
【发布时间】:2017-10-19 02:23:27
【问题描述】:

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

【问题讨论】:

    标签: java bytecode bcel cbo


    【解决方案1】:

    好的,这里你必须计算一整组类中的类的 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);
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多