KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > JavaClassAndMethod


1 /*
2  * Bytecode analysis framework
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import org.apache.bcel.Constants;
23 import org.apache.bcel.Repository;
24 import org.apache.bcel.classfile.JavaClass;
25 import org.apache.bcel.classfile.Method;
26
27 import edu.umd.cs.findbugs.classfile.MethodDescriptor;
28
29 /**
30  * A JavaClass and a Method belonging to the class.
31  * This is useful for answering a method lookup query
32  * which must concretely identify both the class and the method.
33  *
34  * @author David Hovemeyer
35  */

36 public class JavaClassAndMethod {
37     private final JavaClass javaClass;
38     private final Method method;
39     
40     /**
41      * Constructor.
42      *
43      * @param javaClass the JavaClass
44      * @param method a Method belonging to the JavaClass
45      */

46     public JavaClassAndMethod(JavaClass javaClass, Method method) {
47         this.javaClass = javaClass;
48         this.method = method;
49     }
50     
51     /**
52      * Constructor.
53      *
54      * @param method an XMethod specifying a specific method in a specific class
55      * @throws ClassNotFoundException
56      */

57     public JavaClassAndMethod(XMethod method) throws ClassNotFoundException JavaDoc {
58
59         this.javaClass = Repository.lookupClass(method.getClassName());
60         for(Method m : javaClass.getMethods())
61             if (m.getName().equals(method.getName())
62                     && m.getSignature().equals(method.getSignature())
63                     && m.getAccessFlags() == method.getAccessFlags()) {
64                 this.method = m;
65                 return;
66             }
67         throw new IllegalArgumentException JavaDoc("Can't find " + method);
68     }
69
70     /**
71      * Get the JavaClass.
72      */

73     public JavaClass getJavaClass() {
74         return javaClass;
75     }
76
77     /**
78      * Get the Method.
79      */

80     public Method getMethod() {
81         return method;
82     }
83     
84     /**
85      * Convert to an XMethod.
86      */

87     public XMethod toXMethod() {
88         return XFactory.createXMethod(javaClass, method);
89     }
90     
91     /**
92      * Get the MethodDescriptor that (hopefully) uniqely names
93      * this method.
94      *
95      * @return the MethodDescriptor uniquely naming this method
96      */

97     public MethodDescriptor toMethodDescriptor() {
98         return new MethodDescriptor(
99                 getSlashedClassName(),
100                 method.getName(),
101                 method.getSignature(),
102                 method.isStatic());
103     }
104
105     private String JavaDoc getSlashedClassName() {
106         return javaClass.getConstantPool().getConstantString(javaClass.getClassNameIndex(), Constants.CONSTANT_Class);
107     }
108     
109     @Override JavaDoc
110     public int hashCode() {
111         return javaClass.hashCode() + method.hashCode();
112     }
113     
114     @Override JavaDoc
115     public boolean equals(Object JavaDoc obj) {
116         if (obj == null || obj.getClass() != this.getClass())
117             return false;
118         JavaClassAndMethod other = (JavaClassAndMethod) obj;
119         return javaClass.equals(other.javaClass) && method.equals(other.method);
120     }
121     
122     @Override JavaDoc
123     public String JavaDoc toString() {
124         return SignatureConverter.convertMethodSignature(javaClass, method);
125     }
126 }
127
Popular Tags