Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/bcel/classfile/MethodDefinition.java


1   //Tested with BCEL-5.1
2   //http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3   
4   package com.puppycrawl.tools.checkstyle.bcel.classfile;
5   
6   import java.util.HashSet;
7   import java.util.Iterator;
8   import java.util.Set;
9   
10  import org.apache.bcel.Repository;
11  import org.apache.bcel.classfile.JavaClass;
12  import org.apache.bcel.classfile.Method;
13  import org.apache.bcel.generic.Type;
14  
15  import com.puppycrawl.tools.checkstyle.bcel.generic.InvokeReference;
16  import com.puppycrawl.tools.checkstyle.bcel.generic.Utils;
17  
18  /**
19   * Contains the definition of a Method and its references.
20   * @author Rick Giles
21   */
22  public class MethodDefinition
23      extends FieldOrMethodDefinition
24  {
25      /** the references to the Method */
26      private Set mReferences = new HashSet();
27  
28      /**
29       * Creates a <code>MethodDefinition</code> for a Method.
30       * @param aMethod the Method.
31       */
32      public MethodDefinition(Method aMethod)
33      {
34          super(aMethod);
35      }
36  
37      /**
38       * Gets the references to the Method.
39       * @return the references to the Method.
40       */
41      public Set getReferences()
42      {
43          return mReferences;
44      }
45  
46      /**
47       * Determines the number of references to the Method.
48       * @return the number of references to the Method.
49       */
50      public int getReferenceCount()
51      {
52          return mReferences.size();
53      }
54  
55      /**
56       * Returns the Method for this definition.
57       * @return the Method for this definition.
58       */
59      public Method getMethod()
60      {
61          return (Method) getFieldOrMethod();
62      }
63  
64      /**
65       * Adds a reference to the Method.
66       * @param aRef the reference.
67       */
68  
69      public void addReference(InvokeReference aRef)
70      {
71          mReferences.add(aRef);
72      }
73  
74      /**
75       * Gets the Types of the Method's arguments.
76       * @return the argument Types.
77       */
78      public Type[] getArgumentTypes()
79      {
80          return getMethod().getArgumentTypes();
81      }
82  
83      /**
84       * Determines whether a Method is compatible with the
85       * Method of this definition.
86       * @param aMethod the Method to check.
87       * @return true if aMethod is compatible with the Method
88       * of this definition.
89       */
90      public boolean isCompatible(Method aMethod)
91      {
92          return isCompatible(aMethod.getName(), aMethod.getArgumentTypes());
93      }
94  
95      /**
96       * Determines whether a MethodDefinition is compatible with the
97       * Method of this definition.
98       * @param aMethodDef the Method definition to check.
99       * @return true if aMethod is compatible with the Method
100      * of this definition.
101      */
102     public boolean isCompatible(MethodDefinition aMethodDef)
103     {
104         return isCompatible(aMethodDef.getMethod());
105     }
106 
107     /**
108      * Determines whether the Method of a MethodDefinition is as narrow
109      * as the method for this definition.
110      * Precondition: the method for this has the same name and the same
111      * number of arguments as the Method for the given MethodDefinition.
112      * @param aMethodDef the MethodDefinition to check.
113      * @return true if the Method of aMethodDef is as narrow
114      * as the method for this definition.
115      */
116     public boolean isAsNarrow(MethodDefinition aMethodDef)
117     {
118         return aMethodDef.isCompatible(this);
119 //        final Type[] types1 = getArgumentTypes();
120 //        final Type[] types2 = aMethodDef.getArgumentTypes();
121 //        for (int i = 0; i < types2.length; i++) {
122 //            if (!Utils.isCompatible(types1[i], types2[i])) {
123 //                return false;
124 //            }
125 //        }
126 //        return true;
127     }
128 
129     /**
130      * Determines whether a method is compatible with the Method of
131      * this definition.
132      * @param aMethodName the name of the method to check.
133      * @param aArgTypes the method argument types.
134      * @return true if the method is compatible with the Method of
135      * this definition.
136      */
137     public boolean isCompatible(String aMethodName, Type[] aArgTypes)
138     {
139         // same name?
140         if (!getName().equals(aMethodName)) {
141             return false;
142         }
143         // compatible argument types?
144         final Type[] methodTypes = getArgumentTypes();
145         if (methodTypes.length != aArgTypes.length) {
146             return false;
147         }
148         for (int i = 0; i < aArgTypes.length; i++) {
149             if (!Utils.isCompatible(aArgTypes[i], methodTypes[i])) {
150                 return false;
151             }
152         }
153         return true;
154     }
155 
156     /**
157      * Determine whether this method definition has a reference from a class or
158      * a superclass.
159      * @param aJavaClass the JavaClass to check against.
160      * @return true if there is a reference to this method definition from a
161      * aJavaClass or a superclass of aJavaClass.
162      */
163     public boolean hasReference(JavaClass aJavaClass)
164     {
165         final Iterator it = getReferences().iterator();
166         while (it.hasNext()) {
167             final InvokeReference invokeRef = (InvokeReference) it.next();
168             final String invokeClassName = invokeRef.getClassName();
169             if (Repository.instanceOf(aJavaClass, invokeClassName)) {
170                 return true;
171             }
172         }
173         return false;
174     }
175 }