Java 用不正确的值填充HashMap。
我使用反射API来计算一个类的传入和传出耦合,进而计算每个类的稳定性度量。然后将结果添加到HashMap。当我运行调试器时,程序似乎运行正常,并且它似乎将正确的值传递给HashMap 当我检查方法的返回语句(是映射)时,键是正确的(键是类),值(值是度量对象)是不正确的。该值始终是其中一个接口的结果 当地图在计算后返回时,它有正确的键,但与每个键关联的值不正确。每个键的值都相同Java 用不正确的值填充HashMap。,java,reflection,hashmap,Java,Reflection,Hashmap,我使用反射API来计算一个类的传入和传出耦合,进而计算每个类的稳定性度量。然后将结果添加到HashMap。当我运行调试器时,程序似乎运行正常,并且它似乎将正确的值传递给HashMap 当我检查方法的返回语句(是映射)时,键是正确的(键是类),值(值是度量对象)是不正确的。该值始终是其中一个接口的结果 当地图在计算后返回时,它有正确的键,但与每个键关联的值不正确。每个键的值都相同 public ClassMap getEfferent(ClassList list){ map = new
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;
}
public类映射getEfferent(类列表){
map=newclassmap();
测量=新测量();
//循环浏览列表
对于(int i=0;i
任何帮助都会很好。你必须
measure = new Measurement();
在for循环中
目前,您只创建一个度量值,并在循环中多次修改和添加它。
因此,所有关键点都将指向相同的测量对象(可能具有循环最后一次迭代的数据)。您必须将
measure = new Measurement();
在for循环中
目前,您只创建一个度量值,并在循环中多次修改和添加它。
因此,所有关键点都将指向相同的测量对象(可能具有循环最后一次迭代的数据)您能否提供一个简单的示例,说明您在哪里使用HashMap,以及在哪里使用HashMap没有给出预期结果的可复制测试。您能否提供一个简单的示例,说明您的输入和预期与实际输出?您能否给我们一个简单的示例,说明您在哪里使用HashMapHashMap和一个可复制的测试没有给出预期的结果。谢谢,就是这样。谢谢,就是这样。