1 19 package org.netbeans.modules.javacore.parser; 20 21 import java.util.List ; 22 import java.lang.reflect.Modifier ; 23 import org.netbeans.jmi.javamodel.JavaClass; 24 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl; 25 import org.netbeans.modules.javacore.jmiimpl.javamodel.SemiPersistentElement; 26 27 31 class ClassInfoMeasure implements Measure { 32 public static final ClassInfoMeasure INSTANCE = new ClassInfoMeasure(); 33 34 private static final int TYPE_WEIGHT = 100; 35 private static final int NAME_WEIGHT = 60; 36 private static final int EXTENDS_WEIGHT = 10; 37 private static final int IMPLEMENTS_WEIGHT = 10; 38 private static final int MEMBERS_WEIGHT = 20; 39 40 41 private ClassInfoMeasure() { 42 } 43 44 49 public int getDistance(Object refObject, Object ast) { 50 if (ast instanceof ClassInfo) { 51 JavaClassImpl refInfo = (JavaClassImpl) refObject; 52 ClassInfo astInfo = (ClassInfo) ast; 53 int result = 0; 54 55 if (refInfo.isInterface() != Modifier.isInterface(astInfo.modifiers)) { 56 result = TYPE_WEIGHT ; 57 } 58 result += StringMeasure.INSTANCE.getDistance(refInfo.getName(), astInfo.name) * NAME_WEIGHT; 59 60 if (refInfo.isPersisted()) { 61 result += ClassNameMeasure.INSTANCE.getDistance(refInfo.getSuperclassRef(), astInfo.superclass) * EXTENDS_WEIGHT; 62 List ifcRefs = refInfo.getInterfaceRefs(); 63 result += new ArrayMeasure(ClassNameMeasure.INSTANCE).getDistance(ifcRefs == null ? new Object [0] : ifcRefs.toArray(), astInfo.interfaces) * IMPLEMENTS_WEIGHT; 64 String [] refMemberNames = InfoUtil.getElementNames(refInfo.getFeatures()); 65 String [] astMemberNames = InfoUtil.getElementNames(astInfo.features); 66 result += new ArrayMeasure(StringMeasure.INSTANCE).getDistance(refMemberNames, astMemberNames) * MEMBERS_WEIGHT; 67 } 68 result = result / 100; 69 return result > INFINITE_DISTANCE ? INFINITE_DISTANCE : result; 70 } else if (ast instanceof TypeRef) { 71 TypeRef tr = SemiPersistentElement.typeToTypeRef((JavaClass) refObject); 72 if (tr.equals(ast)) 73 return 0; 74 return INFINITE_DISTANCE; 75 } 76 return INFINITE_DISTANCE; 77 } 78 } 79 | Popular Tags |