KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > util > MemberFinder


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.util;
22
23 import proguard.classfile.*;
24 import proguard.classfile.visitor.*;
25
26 /**
27  * This class provides methods to find class members in a given class or in its
28  * hierarchy.
29  *
30  * @author Eric Lafortune
31  */

32 public class MemberFinder
33 extends SimplifiedVisitor
34 implements MemberVisitor
35 {
36     private static class MemberFoundException extends RuntimeException JavaDoc {};
37     private static final MemberFoundException MEMBER_FOUND = new MemberFoundException();
38
39     private Clazz clazz;
40     private Member member;
41
42
43     /**
44      * Finds the field with the given name and descriptor in the given
45      * class or its hierarchy.
46      */

47     public Field findField(Clazz referencingClass,
48                            Clazz clazz,
49                            String JavaDoc name,
50                            String JavaDoc descriptor)
51     {
52         return (Field)findMember(referencingClass, clazz, name, descriptor, true);
53     }
54
55
56     /**
57      * Finds the method with the given name and descriptor in the given
58      * class or its hierarchy.
59      */

60     public Method findMethod(Clazz referencingClass,
61                              Clazz clazz,
62                              String JavaDoc name,
63                              String JavaDoc descriptor)
64     {
65         return (Method)findMember(referencingClass, clazz, name, descriptor, false);
66     }
67
68
69     /**
70      * Finds the class member with the given name and descriptor in the given
71      * class or its hierarchy.
72      */

73     public Member findMember(Clazz referencingClass,
74                              Clazz clazz,
75                              String JavaDoc name,
76                              String JavaDoc descriptor,
77                              boolean isField)
78     {
79         // Organize a search in the hierarchy of superclasses and interfaces.
80
// The class member may be in a different class, if the code was
81
// compiled with "-target 1.2" or higher (the default in JDK 1.4).
82
try
83         {
84             this.clazz = null;
85             this.member = null;
86             clazz.hierarchyAccept(true, true, true, false, isField ?
87                 (ClassVisitor)new NamedFieldVisitor(name, descriptor,
88                               new MemberClassAccessFilter(referencingClass, this)) :
89                 (ClassVisitor)new NamedMethodVisitor(name, descriptor,
90                               new MemberClassAccessFilter(referencingClass, this)));
91         }
92         catch (MemberFoundException ex)
93         {
94         }
95
96         return member;
97     }
98
99
100     /**
101      * Returns the corresponding class of the most recently found class
102      * member.
103      */

104     public Clazz correspondingClass()
105     {
106         return clazz;
107     }
108
109
110     /**
111      * Returns whether the given method is overridden anywhere down the class
112      * hierarchy.
113      */

114     public boolean isOverriden(Clazz clazz,
115                                Method method)
116     {
117         String JavaDoc name = method.getName(clazz);
118         String JavaDoc descriptor = method.getDescriptor(clazz);
119
120         // Go looking for the method down the class hierarchy.
121
try
122         {
123             this.clazz = null;
124             this.member = null;
125
126             clazz.hierarchyAccept(false, false, false, true,
127                 new NamedMethodVisitor(name, descriptor,
128                 new MemberAccessFilter(0, ClassConstants.INTERNAL_ACC_PRIVATE, this)));
129         }
130         catch (MemberFoundException ex)
131         {
132             // We've found an overriding method.
133
return true;
134         }
135
136         return false;
137     }
138
139
140     /**
141      * Returns whether the given field is shadowed anywhere down the class
142      * hierarchy.
143      */

144     public boolean isShadowed(Clazz clazz,
145                               Field field)
146     {
147         String JavaDoc name = field.getName(clazz);
148         String JavaDoc descriptor = field.getDescriptor(clazz);
149
150         // Go looking for the field down the class hierarchy.
151
try
152         {
153             this.clazz = null;
154             this.member = null;
155             clazz.hierarchyAccept(false, false, false, true,
156                 new NamedFieldVisitor(name, descriptor,
157                 new MemberAccessFilter(0, ClassConstants.INTERNAL_ACC_PRIVATE, this)));
158         }
159         catch (MemberFoundException ex)
160         {
161             // We've found a shadowing field.
162
return true;
163         }
164
165         return false;
166     }
167
168
169 // // Implementations for ClassVisitor.
170
//
171
// private void visitAnyClass(Clazz clazz)
172
// {
173
// if (member == null)
174
// {
175
// member = isField ?
176
// (Member)clazz.findField(name, descriptor) :
177
// (Member)clazz.findMethod(name, descriptor);
178
//
179
// if (member != null)
180
// {
181
// this.clazz = clazz;
182
// }
183
// }
184
// }
185

186
187     // Implementations for MemberVisitor.
188

189     public void visitAnyMember(Clazz clazz, Member member)
190     {
191         this.clazz = clazz;
192         this.member = member;
193
194         throw MEMBER_FOUND;
195     }
196 }
197
Popular Tags