0
我正在使用Reflection API來計算一個類的傳入和傳出耦合,並依次計算每個類的穩定性度量。然後將結果添加到HashMap。當我運行調試器時,程序似乎運行正常,並且它似乎將正確的值傳遞給HashMap。用不正確的值填充HashMap。
當我檢查方法的return語句(這是地圖)的關鍵是正確的(關鍵是類),值(值是度量對象)不正確。該值始終是其中一個接口的結果。
當該地圖計算後返回它具有正確的密鑰,但與每個鍵關聯的值不正確。每個密鑰的值是相同的
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;
}
任何幫助將是偉大的。
你能否提供一個[mcve]並闡明你的輸入和期望與實際輸出? – assylias
你可以告訴我們一個簡單的例子,你在哪裏使用HashMap和一個可重複的測試,它沒有給出預期的結果。 –