2017-01-09 8 views
0

Reflection APIを使用して、クラスの求心性および遠心性結合を計算し、各クラスの安定性メトリックを計算します。次に、結果をHashMapに追加します。私はデバッガを実行すると、プログラムが正しく実行されるように見え、それはHashMapに正しい値を渡すようです。HashMapに不正な値を設定します。

私は(マップある)メソッドのreturn文をご確認キーが正しい(キーがクラスである)、値が(値が測定対象で)間違っています。この値は、常に1つのインタフェースの結果です。

地図計算後の戻ったとき、それは正しいキーを持っていますが、各キーに関連付けられている値が正しくありません。値は各キーで同じです

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

すべてのヘルプは素晴らしいでしょう。

+0

[mcve]を入力して、入力と予想と実際の出力を明確にすることはできますか? – assylias

+0

HashMapを使用している場所と予想される結果が得られない再現可能なテストの簡単な例を表示できますか? –

答えて

2

あなたはループのためにあなたの内側の

measure = new Measurement(); 

を配置する必要があります。

現在のところ、ループ内で測定と修正+複数回の追加のみを行っています。 すべてのキーが同じ測定オブジェクトを指しています(ループの最後の反復のデータを持っている可能性があります)。

+0

ありがとうございました。 –

関連する問題