使用不正确的值填充HashMap。

时间:2017-01-09 11:46:41

标签: java reflection hashmap

我使用Reflection API来计算类的传入和传出耦合,然后计算每个类的稳定性度量。然后将结果添加到HashMap。当我运行调试器时程序似乎运行正常,它似乎将正确的值传递给HashMap。

当我检查方法的返回语句(,即地图)时,密钥是正确的(密钥是一个类),值(值是度量对象)是不正确的。该值始终是其中一个接口的结果。

当地图在计算后返回时,它具有正确的密钥,但与每个密钥关联的值不正确。每个Key的值都相同

 public ClassMap getEfferent(ClassList list){
    map = new ClassMap();
    measure = new Measurement();

    //cycle through the list
    for (int i = 0; i < list.size(); i++) {

        //Get the class needed for efferent inspection
        Class cla = list.getMyClass(i);

        //get the interfaces from the class
        Class[] interfaces = cla.getInterfaces();

        //if the class implements any interfaces increment efferent
        for(Class inter : interfaces){

            //if the interface is part of the list
            if(list.contains(inter)){
                efferentCoupling++;
            }

        }//end interfaces

        Constructor[] cons = cla.getConstructors();
        Class[] conParams;

        for(Constructor c: cons){

            conParams = c.getParameterTypes();

            for(Class par: conParams){

                //if the paramater name is on the list of classes ++
                if(list.contains(par.getName())){
                    efferentCoupling ++;
                }

            }

        }//end Constructor params

        Field[] fields = cla.getFields();

        for(Field fie: fields ){

            //if the field name is on the list of classes ++
            if(list.contains(fie.getName()))
                efferentCoupling ++;


        }//fields

        //get the methods for the class
        Method[] classMethods = cla.getMethods();
        Class[] params;

        //
        for(Method meth: classMethods){

            Class returnTypes = meth.getReturnType();

            if(list.contains(meth.getReturnType().getName())){
                efferentCoupling ++;
            }

        }

        //pass in the list and the class name to check for afferent coupling
        //return the afferent score as an interger
        afferentCoupling = getAfferent(list, cla.getName());

        Name = cla.getName();

        //pass the the class name into setClassName
        measure.setClassName(Name);

        //pass in the efferentCoupling for the class
        measure.setEfferentCoupling(efferentCoupling);
        //pass in the afferentCoupling for the class
        measure.setAfferentCoupling(afferentCoupling);

        //System.out.println(measure.getStability());
        cla = list.getMyClass(i);

        //put the class(key) measure (value)
        map.put(cla, measure);

    }//end for


    //resets efferent coupling
    efferentCoupling = 0;

    return map;
}//getAfferent


//method passes in the ClassList and the class name to be checked
public int getAfferent(ClassList list, String name){
    int afferent = 0;

    for (int i = 0; i < list.size(); i++) {

        //Get the class needed for afferent inspection
        Class cla = list.getMyClass(i);

        Class[] interfaces = cla.getInterfaces();

        //if the class implements any interfaces increment efferent
        for(Class inter : interfaces){

            //if the interface name is same as inter.getName() then increment afferent
            if(inter.getName() == name){
                afferent ++;
            }

        }//end interfaces

        Constructor[] cons = cla.getConstructors();
        Class[] conParams;

        for(Constructor c: cons) {

            conParams = c.getParameterTypes();

            for (Class par : conParams) {

                //if constructor params == name then increment
                if (par.getName() == name) {
                    afferent++;
                }
            }
        }

        Field[] fields = cla.getFields();
        for(Field fie: fields ){

            if(fie.getName() == name)
                afferent++;
        }//fields

        Method[] classMethods = cla.getMethods();
        Class[] params;

        for(Method meth: classMethods){

            Class returnTypes = meth.getReturnType();

            if(meth.getReturnType().getName() == name){

                afferent ++;
            }

        }
    }

    return afferent;
}

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

你必须把

measure = new Measurement();
在你的for循环中

目前,您只需创建一个测量并在循环中修改+多次添加。 所以你的所有键都指向同一个Measurement对象(可能有你循环的最后一次迭代的数据。)