| Member.java |
1 // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2
3 package jminusminus;
4
5 /**
6 * This abstract base class provides a wrapper for class members (ie, fields, methods, and
7 * constructors).
8 */
9 abstract class Member {
10 /**
11 * Returns this member's (simple) name.
12 *
13 * @return this member's (simple) name.
14 */
15 public String name() {
16 return member().getName();
17 }
18
19 /**
20 * Returns the type in which this member was declared.
21 *
22 * @return the type in which this member was declared.
23 */
24 public Type declaringType() {
25 return Type.typeFor(member().getDeclaringClass());
26 }
27
28 /**
29 * Returns true if this member is static, and false otherwise.
30 *
31 * @return true if this member is static, and false otherwise.
32 */
33 public boolean isStatic() {
34 return java.lang.reflect.Modifier.isStatic(member().getModifiers());
35 }
36
37 /**
38 * Returns true if this member is public, and false otherwise.
39 *
40 * @return true if this member is public, and false otherwise.
41 */
42 public boolean isPublic() {
43 return java.lang.reflect.Modifier.isPublic(member().getModifiers());
44 }
45
46 /**
47 * Returns true if this member is protected, and false otherwise.
48 *
49 * @return true if this member is protected, and false otherwise.
50 */
51 public boolean isProtected() {
52 return java.lang.reflect.Modifier.isProtected(member().getModifiers());
53 }
54
55 /**
56 * Returns true if this member is private, and false otherwise.
57 *
58 * @return true if this member is private, and false otherwise.
59 */
60 public boolean isPrivate() {
61 return java.lang.reflect.Modifier.isPrivate(member().getModifiers());
62 }
63
64 /**
65 * Returns true if this member is abstract, and false otherwise.
66 *
67 * @return true if this member is abstract, and false otherwise.
68 */
69 public boolean isAbstract() {
70 return java.lang.reflect.Modifier.isAbstract(member().getModifiers());
71 }
72
73 /**
74 * Returns true if this member is final, and false otherwise.
75 *
76 * @return true if this member is final, and false otherwise.
77 */
78 public boolean isFinal() {
79 return java.lang.reflect.Modifier.isFinal(member().getModifiers());
80 }
81
82 /**
83 * Returns the JVM descriptor for this member.
84 *
85 * @return the JVM descriptor for this member.
86 */
87 public abstract String toDescriptor();
88
89 /**
90 * Returns this member's internal representation.
91 *
92 * @return this member's internal representation.
93 */
94 protected abstract java.lang.reflect.Member member();
95 }
96
97 /**
98 * This class provides a wrapper for constructors.
99 */
100 class Constructor extends Member {
101 // Internal representation of this constructor.
102 private java.lang.reflect.Constructor constructor;
103
104 /**
105 * Constructs a constructor given its internal representation.
106 *
107 * @param constructor internal representation.
108 */
109 public Constructor(java.lang.reflect.Constructor constructor) {
110 this.constructor = constructor;
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public String toDescriptor() {
117 String descriptor = "(";
118 for (Class paramType : constructor.getParameterTypes()) {
119 descriptor += Type.typeFor(paramType).toDescriptor();
120 }
121 descriptor += ")V";
122 return descriptor;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 protected java.lang.reflect.Member member() {
129 return constructor;
130 }
131 }
132
133 /**
134 * This class provides a wrapper for fields.
135 */
136 class Field extends Member {
137 // Internal representation of this field.
138 private java.lang.reflect.Field field;
139
140 /**
141 * Constructs a field given its internal representation.
142 *
143 * @param field internal representation.
144 */
145 public Field(java.lang.reflect.Field field) {
146 this.field = field;
147 }
148
149 /**
150 * Returns this field's type.
151 *
152 * @return this field's type.
153 */
154 public Type type() {
155 return Type.typeFor(field.getType());
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 public String toDescriptor() {
162 return type().toDescriptor();
163 }
164
165 /**
166 * {@inheritDoc}
167 */
168 protected java.lang.reflect.Member member() {
169 return field;
170 }
171 }
172
173 /**
174 * This class provides a wrapper for methods.
175 */
176 class Method extends Member {
177 // Internal representation of this method.
178 private java.lang.reflect.Method method;
179
180 /**
181 * Constructs a method given its internal representation.
182 *
183 * @param method the internal representation.
184 */
185 public Method(java.lang.reflect.Method method) {
186 this.method = method;
187 }
188
189 /**
190 * Returns this method's return type.
191 *
192 * @return this method's return type.
193 */
194 public Type returnType() {
195 return Type.typeFor(method.getReturnType());
196 }
197
198 /**
199 * {@inheritDoc}
200 */
201 public String toDescriptor() {
202 String descriptor = "(";
203 for (Class paramType : method.getParameterTypes()) {
204 descriptor += Type.typeFor(paramType).toDescriptor();
205 }
206 descriptor += ")" + Type.typeFor(method.getReturnType()).toDescriptor();
207 return descriptor;
208 }
209
210 /**
211 * {@inheritDoc}
212 */
213 protected java.lang.reflect.Member member() {
214 return method;
215 }
216 }