Member.java |
1 // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas 2 3 package jminusminus; 4 5 /** 6 * A wrapper for members (eg Fields, Methods, Constructors) in the Java API. 7 * Members are used in message expressions, field selections, and new object 8 * construction operations. 9 */ 10 11 abstract class Member { 12 13 /** 14 * Return the member's (simple) name. 15 * 16 * @return the name. 17 */ 18 19 public String name() { 20 return member().getName(); 21 } 22 23 /** 24 * Return the type in which this member was declared. 25 * 26 * @return the declaring type. 27 */ 28 29 public Type declaringType() { 30 return Type.typeFor(member().getDeclaringClass()); 31 } 32 33 /** 34 * Has this member been declared with the static modifier? 35 * 36 * @return is the member static? 37 */ 38 39 public boolean isStatic() { 40 return java.lang.reflect.Modifier.isStatic(member().getModifiers()); 41 } 42 43 /** 44 * Has this member been declared with the public modifier? 45 * 46 * @return is the member public? 47 */ 48 49 public boolean isPublic() { 50 return java.lang.reflect.Modifier.isPublic(member().getModifiers()); 51 } 52 53 /** 54 * Has this member been declared with the protected modifier? 55 * 56 * @return is the member protected? 57 */ 58 59 public boolean isProtected() { 60 return java.lang.reflect.Modifier.isProtected(member().getModifiers()); 61 } 62 63 /** 64 * Has this member been declared with the private modifier? 65 * 66 * @return is the member private? 67 */ 68 69 public boolean isPrivate() { 70 return java.lang.reflect.Modifier.isPrivate(member().getModifiers()); 71 } 72 73 /** 74 * Has this member been declared with the abstract modifier? 75 * 76 * @return is the member abstract? 77 */ 78 79 public boolean isAbstract() { 80 return java.lang.reflect.Modifier.isAbstract(member().getModifiers()); 81 } 82 83 /** 84 * Has this member been declared with the final modifier? Note,we cannot 85 * declare anything final as it is not in our lexicon. But we may refer to 86 * names that are declared final in the java API and so we (may) want to 87 * honor those declarations. 88 * 89 * @return is the member final? 90 */ 91 92 public boolean isFinal() { 93 return java.lang.reflect.Modifier.isFinal(member().getModifiers()); 94 } 95 96 /** 97 * Return the member's internal representation. 98 * 99 * @return the internal representation. 100 */ 101 102 protected abstract java.lang.reflect.Member member(); 103 104 } 105 106 /** 107 * A Method knows its descriptor (its signature in JVM format), and its return 108 * type. 109 */ 110 111 class Method extends Member { 112 113 /** Internal representation of this method. */ 114 private java.lang.reflect.Method method; 115 116 /** 117 * Construct a Method is constructed from its internal representation. 118 * 119 * @param method 120 * a Java method in the relection API. 121 */ 122 123 public Method(java.lang.reflect.Method method) { 124 this.method = method; 125 } 126 127 /** 128 * Return the JVM descriptor for this method. 129 * 130 * @return the descriptor. 131 */ 132 133 public String toDescriptor() { 134 String descriptor = "("; 135 for (Class paramType : method.getParameterTypes()) { 136 descriptor += Type.typeFor(paramType).toDescriptor(); 137 } 138 descriptor += ")" + Type.typeFor(method.getReturnType()).toDescriptor(); 139 return descriptor; 140 } 141 142 /** 143 * Return the Java representation for this method. 144 * 145 * @return the descriptor. 146 */ 147 148 public String toString() { 149 String str = name() + "("; 150 for (Class paramType : method.getParameterTypes()) { 151 str += Type.typeFor(paramType).toString(); 152 } 153 str += ")"; 154 return str; 155 } 156 157 /** 158 * Return the method's return type. 159 * 160 * @return the return type. 161 */ 162 163 public Type returnType() { 164 return Type.typeFor(method.getReturnType()); 165 } 166 167 /** 168 * Method equality is defined HERE as having override-equivalent signatures. 169 * 170 * @param that 171 * the method we are comparing this to. 172 * @return true iff the methods are override-equivalent. 173 */ 174 175 public boolean equals(Method that) { 176 return Type.argTypesMatch(this.method.getParameterTypes(), that.method 177 .getParameterTypes()); 178 } 179 180 /** 181 * Return the member's internal representation. 182 * 183 * @return the internal representation. 184 */ 185 186 protected java.lang.reflect.Member member() { 187 return method; 188 } 189 190 } 191 192 /** 193 * A Field knows its type. 194 */ 195 196 class Field extends Member { 197 198 /** Internal representation of this field. */ 199 private java.lang.reflect.Field field; 200 201 /** 202 * Construct a Field is constructed from its internal representation. 203 * 204 * @param field 205 * a Java field in the relection API. 206 */ 207 208 public Field(java.lang.reflect.Field field) { 209 this.field = field; 210 } 211 212 /** 213 * Return the field's type. 214 * 215 * @return the field's type. 216 */ 217 218 public Type type() { 219 return Type.typeFor(field.getType()); 220 } 221 222 /** 223 * @inheritDoc 224 */ 225 226 protected java.lang.reflect.Member member() { 227 return field; 228 } 229 230 } 231 232 /** 233 * A Constructor knows its JVM descriptor. 234 */ 235 236 class Constructor extends Member { 237 238 /** Internal representation of this constructor. */ 239 java.lang.reflect.Constructor constructor; 240 241 /** 242 * Construct a Constructor from its internal representation. 243 * 244 * @param constructor 245 * a Java constructor in the relection API. 246 */ 247 248 public Constructor(java.lang.reflect.Constructor constructor) { 249 this.constructor = constructor; 250 } 251 252 /** 253 * Return the JVM descriptor for this constructor. 254 * 255 * @return the descriptor. 256 */ 257 258 public String toDescriptor() { 259 String descriptor = "("; 260 for (Class paramType : constructor.getParameterTypes()) { 261 descriptor += Type.typeFor(paramType).toDescriptor(); 262 } 263 descriptor += ")V"; 264 return descriptor; 265 } 266 267 /** 268 * Return the type declaring this constructor. 269 * 270 * @return the declaring type. 271 */ 272 273 public Type declaringType() { 274 return Type.typeFor(constructor.getDeclaringClass()); 275 } 276 277 /** 278 * @inheritDoc 279 */ 280 281 protected java.lang.reflect.Member member() { 282 return constructor; 283 } 284 285 } 286