Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.model.nav; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Field; |
aoqi@0 | 29 | import java.lang.reflect.Method; |
aoqi@0 | 30 | import java.lang.reflect.Proxy; |
aoqi@0 | 31 | import java.lang.reflect.Type; |
aoqi@0 | 32 | import java.util.Collection; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import com.sun.xml.internal.bind.v2.runtime.Location; |
aoqi@0 | 35 | |
aoqi@0 | 36 | /** |
aoqi@0 | 37 | * Provides unified view of the underlying reflection library, |
aoqi@0 | 38 | * such as {@code java.lang.reflect} and/or Annotation Processing. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * <p> |
aoqi@0 | 41 | * This interface provides navigation over the reflection model |
aoqi@0 | 42 | * to decouple the caller from any particular implementation. |
aoqi@0 | 43 | * This allows the JAXB RI to reuse much of the code between |
aoqi@0 | 44 | * the compile time (which works on top of Annotation Processing) and the run-time |
aoqi@0 | 45 | * (which works on top of {@code java.lang.reflect}) |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * <p> |
aoqi@0 | 48 | * {@link Navigator} instances are stateless and immutable. |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <h2>Parameterization</h2> |
aoqi@0 | 52 | * <h3>C</h3> |
aoqi@0 | 53 | * <p> |
aoqi@0 | 54 | * A Java class declaration (not an interface, a class and an enum.) |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <h3>T</h3> |
aoqi@0 | 57 | * <p> |
aoqi@0 | 58 | * A Java type. This includs declaration, but also includes such |
aoqi@0 | 59 | * things like arrays, primitive types, parameterized types, and etc. |
aoqi@0 | 60 | * |
aoqi@0 | 61 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | public interface Navigator<T,C,F,M> { |
aoqi@0 | 64 | /** |
aoqi@0 | 65 | * Gets the base class of the specified class. |
aoqi@0 | 66 | * |
aoqi@0 | 67 | * @return |
aoqi@0 | 68 | * null if the parameter represents {@link Object}. |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | C getSuperClass(C clazz); |
aoqi@0 | 71 | |
aoqi@0 | 72 | /** |
aoqi@0 | 73 | * Gets the parameterization of the given base type. |
aoqi@0 | 74 | * |
aoqi@0 | 75 | * <p> |
aoqi@0 | 76 | * For example, given the following |
aoqi@0 | 77 | * <pre><xmp> |
aoqi@0 | 78 | * interface Foo<T> extends List<List<T>> {} |
aoqi@0 | 79 | * interface Bar extends Foo<String> {} |
aoqi@0 | 80 | * </xmp></pre> |
aoqi@0 | 81 | * This method works like this: |
aoqi@0 | 82 | * <pre><xmp> |
aoqi@0 | 83 | * getBaseClass( Bar, List ) = List<List<String> |
aoqi@0 | 84 | * getBaseClass( Bar, Foo ) = Foo<String> |
aoqi@0 | 85 | * getBaseClass( Foo<? extends Number>, Collection ) = Collection<List<? extends Number>> |
aoqi@0 | 86 | * getBaseClass( ArrayList<? extends BigInteger>, List ) = List<? extends BigInteger> |
aoqi@0 | 87 | * </xmp></pre> |
aoqi@0 | 88 | * |
aoqi@0 | 89 | * @param type |
aoqi@0 | 90 | * The type that derives from {@code baseType} |
aoqi@0 | 91 | * @param baseType |
aoqi@0 | 92 | * The class whose parameterization we are interested in. |
aoqi@0 | 93 | * @return |
aoqi@0 | 94 | * The use of {@code baseType} in {@code type}. |
aoqi@0 | 95 | * or null if the type is not assignable to the base type. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | T getBaseClass(T type, C baseType); |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * Gets the fully-qualified name of the class. |
aoqi@0 | 101 | * ("java.lang.Object" for {@link Object}) |
aoqi@0 | 102 | */ |
aoqi@0 | 103 | String getClassName(C clazz); |
aoqi@0 | 104 | |
aoqi@0 | 105 | /** |
aoqi@0 | 106 | * Gets the display name of the type object |
aoqi@0 | 107 | * |
aoqi@0 | 108 | * @return |
aoqi@0 | 109 | * a human-readable name that the type represents. |
aoqi@0 | 110 | */ |
aoqi@0 | 111 | String getTypeName(T rawType); |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Gets the short name of the class ("Object" for {@link Object}.) |
aoqi@0 | 115 | * |
aoqi@0 | 116 | * For nested classes, this method should just return the inner name. |
aoqi@0 | 117 | * (for example "Inner" for "com.acme.Outer$Inner". |
aoqi@0 | 118 | */ |
aoqi@0 | 119 | String getClassShortName(C clazz); |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Gets all the declared fields of the given class. |
aoqi@0 | 123 | */ |
aoqi@0 | 124 | Collection<? extends F> getDeclaredFields(C clazz); |
aoqi@0 | 125 | |
aoqi@0 | 126 | /** |
aoqi@0 | 127 | * Gets the named field declared on the given class. |
aoqi@0 | 128 | * |
aoqi@0 | 129 | * This method doesn't visit ancestors, but does recognize |
aoqi@0 | 130 | * non-public fields. |
aoqi@0 | 131 | * |
aoqi@0 | 132 | * @return |
aoqi@0 | 133 | * null if not found |
aoqi@0 | 134 | */ |
aoqi@0 | 135 | F getDeclaredField(C clazz, String fieldName); |
aoqi@0 | 136 | |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Gets all the declared methods of the given class |
aoqi@0 | 139 | * (regardless of their access modifiers, regardless |
aoqi@0 | 140 | * of whether they override methods of the base classes.) |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * <p> |
aoqi@0 | 143 | * Note that this method does not list methods declared on base classes. |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * @return |
aoqi@0 | 146 | * can be empty but always non-null. |
aoqi@0 | 147 | */ |
aoqi@0 | 148 | Collection<? extends M> getDeclaredMethods(C clazz); |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * Gets the class that declares the given field. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | C getDeclaringClassForField(F field); |
aoqi@0 | 154 | |
aoqi@0 | 155 | /** |
aoqi@0 | 156 | * Gets the class that declares the given method. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | C getDeclaringClassForMethod(M method); |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Gets the type of the field. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | T getFieldType(F f); |
aoqi@0 | 164 | |
aoqi@0 | 165 | /** |
aoqi@0 | 166 | * Gets the name of the field. |
aoqi@0 | 167 | */ |
aoqi@0 | 168 | String getFieldName(F field); |
aoqi@0 | 169 | |
aoqi@0 | 170 | /** |
aoqi@0 | 171 | * Gets the name of the method, such as "toString" or "equals". |
aoqi@0 | 172 | */ |
aoqi@0 | 173 | String getMethodName(M m); |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * Gets the return type of a method. |
aoqi@0 | 177 | */ |
aoqi@0 | 178 | T getReturnType(M m); |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** |
aoqi@0 | 181 | * Returns the list of parameters to the method. |
aoqi@0 | 182 | */ |
aoqi@0 | 183 | T[] getMethodParameters(M method); |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** |
aoqi@0 | 186 | * Returns true if the method is static. |
aoqi@0 | 187 | */ |
aoqi@0 | 188 | boolean isStaticMethod(M method); |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** |
aoqi@0 | 191 | * Checks if {@code sub} is a sub-type of {@code sup}. |
aoqi@0 | 192 | * |
aoqi@0 | 193 | * TODO: should this method take T or C? |
aoqi@0 | 194 | */ |
aoqi@0 | 195 | boolean isSubClassOf(T sub, T sup); |
aoqi@0 | 196 | |
aoqi@0 | 197 | /** |
aoqi@0 | 198 | * Gets the representation of the given Java type in {@code T}. |
aoqi@0 | 199 | * |
aoqi@0 | 200 | * @param c |
aoqi@0 | 201 | * can be a primitive, array, class, or anything. |
aoqi@0 | 202 | * (therefore the return type has to be T, not C) |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | T ref(Class c); |
aoqi@0 | 205 | |
aoqi@0 | 206 | /** |
aoqi@0 | 207 | * Gets the T for the given C. |
aoqi@0 | 208 | */ |
aoqi@0 | 209 | T use(C c); |
aoqi@0 | 210 | |
aoqi@0 | 211 | /** |
aoqi@0 | 212 | * If the given type is an use of class declaration, |
aoqi@0 | 213 | * returns the type casted as {@code C}. |
aoqi@0 | 214 | * Otherwise null. |
aoqi@0 | 215 | * |
aoqi@0 | 216 | * <p> |
aoqi@0 | 217 | * TODO: define the exact semantics. |
aoqi@0 | 218 | */ |
aoqi@0 | 219 | C asDecl(T type); |
aoqi@0 | 220 | |
aoqi@0 | 221 | /** |
aoqi@0 | 222 | * Gets the {@code C} representation for the given class. |
aoqi@0 | 223 | * |
aoqi@0 | 224 | * The behavior is undefined if the class object represents |
aoqi@0 | 225 | * primitives, arrays, and other types that are not class declaration. |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | C asDecl(Class c); |
aoqi@0 | 228 | |
aoqi@0 | 229 | /** |
aoqi@0 | 230 | * Checks if the type is an array type. |
aoqi@0 | 231 | */ |
aoqi@0 | 232 | boolean isArray(T t); |
aoqi@0 | 233 | |
aoqi@0 | 234 | /** |
aoqi@0 | 235 | * Checks if the type is an array type but not byte[]. |
aoqi@0 | 236 | */ |
aoqi@0 | 237 | boolean isArrayButNotByteArray(T t); |
aoqi@0 | 238 | |
aoqi@0 | 239 | /** |
aoqi@0 | 240 | * Gets the component type of the array. |
aoqi@0 | 241 | * |
aoqi@0 | 242 | * @param t |
aoqi@0 | 243 | * must be an array. |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | T getComponentType(T t); |
aoqi@0 | 246 | |
aoqi@0 | 247 | /** |
aoqi@0 | 248 | * Gets the i-th type argument from a parameterized type. |
aoqi@0 | 249 | * |
aoqi@0 | 250 | * For example, {@code getTypeArgument([Map<Integer,String>],0)=Integer} |
aoqi@0 | 251 | * |
aoqi@0 | 252 | * @throws IllegalArgumentException |
aoqi@0 | 253 | * If t is not a parameterized type |
aoqi@0 | 254 | * @throws IndexOutOfBoundsException |
aoqi@0 | 255 | * If i is out of range. |
aoqi@0 | 256 | * |
aoqi@0 | 257 | * @see #isParameterizedType(Object) |
aoqi@0 | 258 | */ |
aoqi@0 | 259 | T getTypeArgument(T t, int i); |
aoqi@0 | 260 | |
aoqi@0 | 261 | /** |
aoqi@0 | 262 | * Returns true if t is a parameterized type. |
aoqi@0 | 263 | */ |
aoqi@0 | 264 | boolean isParameterizedType(T t); |
aoqi@0 | 265 | |
aoqi@0 | 266 | /** |
aoqi@0 | 267 | * Checks if the given type is a primitive type. |
aoqi@0 | 268 | */ |
aoqi@0 | 269 | boolean isPrimitive(T t); |
aoqi@0 | 270 | |
aoqi@0 | 271 | /** |
aoqi@0 | 272 | * Returns the representation for the given primitive type. |
aoqi@0 | 273 | * |
aoqi@0 | 274 | * @param primitiveType |
aoqi@0 | 275 | * must be Class objects like {@link Integer#TYPE}. |
aoqi@0 | 276 | */ |
aoqi@0 | 277 | T getPrimitive(Class primitiveType); |
aoqi@0 | 278 | |
aoqi@0 | 279 | /** |
aoqi@0 | 280 | * Returns a location of the specified class. |
aoqi@0 | 281 | */ |
aoqi@0 | 282 | Location getClassLocation(C clazz); |
aoqi@0 | 283 | |
aoqi@0 | 284 | Location getFieldLocation(F field); |
aoqi@0 | 285 | |
aoqi@0 | 286 | Location getMethodLocation(M getter); |
aoqi@0 | 287 | |
aoqi@0 | 288 | /** |
aoqi@0 | 289 | * Returns true if the given class has a no-arg default constructor. |
aoqi@0 | 290 | * The constructor does not need to be public. |
aoqi@0 | 291 | */ |
aoqi@0 | 292 | boolean hasDefaultConstructor(C clazz); |
aoqi@0 | 293 | |
aoqi@0 | 294 | /** |
aoqi@0 | 295 | * Returns true if the field is static. |
aoqi@0 | 296 | */ |
aoqi@0 | 297 | boolean isStaticField(F field); |
aoqi@0 | 298 | |
aoqi@0 | 299 | /** |
aoqi@0 | 300 | * Returns true if the method is public. |
aoqi@0 | 301 | */ |
aoqi@0 | 302 | boolean isPublicMethod(M method); |
aoqi@0 | 303 | |
aoqi@0 | 304 | /** |
aoqi@0 | 305 | * Returns true if the method is final. |
aoqi@0 | 306 | */ |
aoqi@0 | 307 | boolean isFinalMethod(M method); |
aoqi@0 | 308 | |
aoqi@0 | 309 | /** |
aoqi@0 | 310 | * Returns true if the field is public. |
aoqi@0 | 311 | */ |
aoqi@0 | 312 | boolean isPublicField(F field); |
aoqi@0 | 313 | |
aoqi@0 | 314 | /** |
aoqi@0 | 315 | * Returns true if this is an enum class. |
aoqi@0 | 316 | */ |
aoqi@0 | 317 | boolean isEnum(C clazz); |
aoqi@0 | 318 | |
aoqi@0 | 319 | /** |
aoqi@0 | 320 | * Computes the erasure |
aoqi@0 | 321 | */ |
aoqi@0 | 322 | <P> T erasure(T contentInMemoryType); |
aoqi@0 | 323 | // This unused P is necessary to make ReflectionNavigator.erasure work nicely |
aoqi@0 | 324 | |
aoqi@0 | 325 | /** |
aoqi@0 | 326 | * Returns true if this is an abstract class. |
aoqi@0 | 327 | */ |
aoqi@0 | 328 | boolean isAbstract(C clazz); |
aoqi@0 | 329 | |
aoqi@0 | 330 | /** |
aoqi@0 | 331 | * Returns true if this is a final class. |
aoqi@0 | 332 | */ |
aoqi@0 | 333 | boolean isFinal(C clazz); |
aoqi@0 | 334 | |
aoqi@0 | 335 | /** |
aoqi@0 | 336 | * Gets the enumeration constants from an enum class. |
aoqi@0 | 337 | * |
aoqi@0 | 338 | * @param clazz |
aoqi@0 | 339 | * must derive from {@link Enum}. |
aoqi@0 | 340 | * |
aoqi@0 | 341 | * @return |
aoqi@0 | 342 | * can be empty but never null. |
aoqi@0 | 343 | */ |
aoqi@0 | 344 | F[] getEnumConstants(C clazz); |
aoqi@0 | 345 | |
aoqi@0 | 346 | /** |
aoqi@0 | 347 | * Gets the representation of the primitive "void" type. |
aoqi@0 | 348 | */ |
aoqi@0 | 349 | T getVoidType(); |
aoqi@0 | 350 | |
aoqi@0 | 351 | /** |
aoqi@0 | 352 | * Gets the package name of the given class. |
aoqi@0 | 353 | * |
aoqi@0 | 354 | * @return |
aoqi@0 | 355 | * i.e. "", "java.lang" but not null. |
aoqi@0 | 356 | */ |
aoqi@0 | 357 | String getPackageName(C clazz); |
aoqi@0 | 358 | |
aoqi@0 | 359 | /** |
aoqi@0 | 360 | * Finds ObjectFactory for the given referencePoint. |
aoqi@0 | 361 | * |
aoqi@0 | 362 | * @param referencePoint |
aoqi@0 | 363 | * The class that refers to the specified class. |
aoqi@0 | 364 | * @return |
aoqi@0 | 365 | * null if not found. |
aoqi@0 | 366 | */ |
aoqi@0 | 367 | C loadObjectFactory(C referencePoint, String packageName); |
aoqi@0 | 368 | |
aoqi@0 | 369 | /** |
aoqi@0 | 370 | * Returns true if this method is a bridge method as defined in JLS. |
aoqi@0 | 371 | */ |
aoqi@0 | 372 | boolean isBridgeMethod(M method); |
aoqi@0 | 373 | |
aoqi@0 | 374 | /** |
aoqi@0 | 375 | * Returns true if the given method is overriding another one |
aoqi@0 | 376 | * defined in the base class 'base' or its ancestors. |
aoqi@0 | 377 | */ |
aoqi@0 | 378 | boolean isOverriding(M method, C base); |
aoqi@0 | 379 | |
aoqi@0 | 380 | /** |
aoqi@0 | 381 | * Returns true if 'clazz' is an interface. |
aoqi@0 | 382 | */ |
aoqi@0 | 383 | boolean isInterface(C clazz); |
aoqi@0 | 384 | |
aoqi@0 | 385 | /** |
aoqi@0 | 386 | * Returns true if the field is transient. |
aoqi@0 | 387 | */ |
aoqi@0 | 388 | boolean isTransient(F f); |
aoqi@0 | 389 | |
aoqi@0 | 390 | /** |
aoqi@0 | 391 | * Returns true if the given class is an inner class. |
aoqi@0 | 392 | * |
aoqi@0 | 393 | * This is only used to improve the error diagnostics, so |
aoqi@0 | 394 | * it's OK to fail to detect some inner classes as such. |
aoqi@0 | 395 | * |
aoqi@0 | 396 | * Note that this method should return false for nested classes |
aoqi@0 | 397 | * (static classes.) |
aoqi@0 | 398 | */ |
aoqi@0 | 399 | boolean isInnerClass(C clazz); |
aoqi@0 | 400 | |
aoqi@0 | 401 | /** |
aoqi@0 | 402 | * Checks if types are the same |
aoqi@0 | 403 | * @param t1 type |
aoqi@0 | 404 | * @param t2 type |
aoqi@0 | 405 | * @return true if types are the same |
aoqi@0 | 406 | */ |
aoqi@0 | 407 | boolean isSameType(T t1, T t2); |
aoqi@0 | 408 | } |