KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > ClassInfoMeasure


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.parser;
20
21 import java.util.List JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
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 /**
28  *
29  * @author Tomas Hurka
30  */

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     /** Creates a new instance of ClassMeasure */
41     private ClassInfoMeasure() {
42     }
43     
44     /** Compares JavaClass to ASTree object.
45      * @param refObject Must be JavaClass
46      * @param asTree
47      * @return distance
48      */

49     public int getDistance(Object JavaDoc refObject, Object JavaDoc 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 JavaDoc ifcRefs = refInfo.getInterfaceRefs();
63                 result += new ArrayMeasure(ClassNameMeasure.INSTANCE).getDistance(ifcRefs == null ? new Object JavaDoc[0] : ifcRefs.toArray(), astInfo.interfaces) * IMPLEMENTS_WEIGHT;
64                 String JavaDoc[] refMemberNames = InfoUtil.getElementNames(refInfo.getFeatures());
65                 String JavaDoc[] 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