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和一个可重复的测试,它没有给出预期的结果。 –