001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.reflect; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.collect.ImmutableList; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.lang.annotation.Annotation; 022import java.lang.reflect.AccessibleObject; 023import java.lang.reflect.AnnotatedElement; 024import java.lang.reflect.AnnotatedType; 025import java.lang.reflect.Constructor; 026import java.lang.reflect.InvocationTargetException; 027import java.lang.reflect.Member; 028import java.lang.reflect.Method; 029import java.lang.reflect.Modifier; 030import java.lang.reflect.Type; 031import java.lang.reflect.TypeVariable; 032import java.util.Arrays; 033import org.jspecify.annotations.Nullable; 034 035/** 036 * Wrapper around either a {@link Method} or a {@link Constructor}. Convenience API is provided to 037 * make common reflective operation easier to deal with, such as {@link #isPublic}, {@link 038 * #getParameters} etc. 039 * 040 * <p>In addition to convenience methods, {@link TypeToken#method} and {@link TypeToken#constructor} 041 * will resolve the type parameters of the method or constructor in the context of the owner type, 042 * which may be a subtype of the declaring class. For example: 043 * 044 * <pre>{@code 045 * Method getMethod = List.class.getMethod("get", int.class); 046 * Invokable<List<String>, ?> invokable = new TypeToken<List<String>>() {}.method(getMethod); 047 * assertEquals(TypeToken.of(String.class), invokable.getReturnType()); // Not Object.class! 048 * assertEquals(new TypeToken<List<String>>() {}, invokable.getOwnerType()); 049 * }</pre> 050 * 051 * <p><b>Note:</b> earlier versions of this class inherited from {@link 052 * java.lang.reflect.AccessibleObject AccessibleObject} and {@link 053 * java.lang.reflect.GenericDeclaration GenericDeclaration}. Since version 31.0 that is no longer 054 * the case. However, most methods from those types are present with the same signature in this 055 * class. 056 * 057 * @param <T> the type that owns this method or constructor. 058 * @param <R> the return type of (or supertype thereof) the method or the declaring type of the 059 * constructor. 060 * @author Ben Yu 061 * @since 14.0 (no longer implements {@link AccessibleObject} or {@code GenericDeclaration} since 062 * 31.0) 063 */ 064public abstract class Invokable<T, R> implements AnnotatedElement, Member { 065 private final AccessibleObject accessibleObject; 066 private final Member member; 067 068 <M extends AccessibleObject & Member> Invokable(M member) { 069 checkNotNull(member); 070 this.accessibleObject = member; 071 this.member = member; 072 } 073 074 /** Returns {@link Invokable} of {@code method}. */ 075 public static Invokable<?, Object> from(Method method) { 076 return new MethodInvokable<>(method); 077 } 078 079 /** Returns {@link Invokable} of {@code constructor}. */ 080 public static <T> Invokable<T, T> from(Constructor<T> constructor) { 081 return new ConstructorInvokable<T>(constructor); 082 } 083 084 @Override 085 public final boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 086 return accessibleObject.isAnnotationPresent(annotationClass); 087 } 088 089 @Override 090 public final <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationClass) { 091 return accessibleObject.getAnnotation(annotationClass); 092 } 093 094 @Override 095 public final Annotation[] getAnnotations() { 096 return accessibleObject.getAnnotations(); 097 } 098 099 @Override 100 public final Annotation[] getDeclaredAnnotations() { 101 return accessibleObject.getDeclaredAnnotations(); 102 } 103 104 // We ought to be able to implement GenericDeclaration instead its parent AnnotatedElement. 105 // That would give us this method declaration. But for some reason, implementing 106 // GenericDeclaration leads to weird errors in Android tests: 107 // IncompatibleClassChangeError: interface not implemented 108 /** See {@link java.lang.reflect.GenericDeclaration#getTypeParameters()}. */ 109 public abstract TypeVariable<?>[] getTypeParameters(); 110 111 /** See {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)}. */ 112 public final void setAccessible(boolean flag) { 113 accessibleObject.setAccessible(flag); 114 } 115 116 /** See {@link java.lang.reflect.AccessibleObject#trySetAccessible()}. */ 117 @SuppressWarnings("CatchingUnchecked") // sneaky checked exception 118 public final boolean trySetAccessible() { 119 // We can't call accessibleObject.trySetAccessible since that was added in Java 9 and this code 120 // should work on Java 8. So we emulate it this way. 121 try { 122 accessibleObject.setAccessible(true); 123 return true; 124 } catch (Exception e) { // sneaky checked exception 125 return false; 126 } 127 } 128 129 /** See {@link java.lang.reflect.AccessibleObject#isAccessible()}. */ 130 public final boolean isAccessible() { 131 return accessibleObject.isAccessible(); 132 } 133 134 @Override 135 public final String getName() { 136 return member.getName(); 137 } 138 139 @Override 140 public final int getModifiers() { 141 return member.getModifiers(); 142 } 143 144 @Override 145 public final boolean isSynthetic() { 146 return member.isSynthetic(); 147 } 148 149 /** Returns true if the element is public. */ 150 public final boolean isPublic() { 151 return Modifier.isPublic(getModifiers()); 152 } 153 154 /** Returns true if the element is protected. */ 155 public final boolean isProtected() { 156 return Modifier.isProtected(getModifiers()); 157 } 158 159 /** Returns true if the element is package-private. */ 160 public final boolean isPackagePrivate() { 161 return !isPrivate() && !isPublic() && !isProtected(); 162 } 163 164 /** Returns true if the element is private. */ 165 public final boolean isPrivate() { 166 return Modifier.isPrivate(getModifiers()); 167 } 168 169 /** Returns true if the element is static. */ 170 public final boolean isStatic() { 171 return Modifier.isStatic(getModifiers()); 172 } 173 174 /** 175 * Returns {@code true} if this method is final, per {@code Modifier.isFinal(getModifiers())}. 176 * 177 * <p>Note that a method may still be effectively "final", or non-overridable when it has no 178 * {@code final} keyword. For example, it could be private, or it could be declared by a final 179 * class. To tell whether a method is overridable, use {@link Invokable#isOverridable}. 180 */ 181 public final boolean isFinal() { 182 return Modifier.isFinal(getModifiers()); 183 } 184 185 /** Returns true if the method is abstract. */ 186 public final boolean isAbstract() { 187 return Modifier.isAbstract(getModifiers()); 188 } 189 190 /** Returns true if the element is native. */ 191 public final boolean isNative() { 192 return Modifier.isNative(getModifiers()); 193 } 194 195 /** Returns true if the method is synchronized. */ 196 public final boolean isSynchronized() { 197 return Modifier.isSynchronized(getModifiers()); 198 } 199 200 /** Returns true if the field is volatile. */ 201 final boolean isVolatile() { 202 return Modifier.isVolatile(getModifiers()); 203 } 204 205 /** Returns true if the field is transient. */ 206 final boolean isTransient() { 207 return Modifier.isTransient(getModifiers()); 208 } 209 210 @Override 211 public boolean equals(@Nullable Object obj) { 212 if (obj instanceof Invokable) { 213 Invokable<?, ?> that = (Invokable<?, ?>) obj; 214 return getOwnerType().equals(that.getOwnerType()) && member.equals(that.member); 215 } 216 return false; 217 } 218 219 @Override 220 public int hashCode() { 221 return member.hashCode(); 222 } 223 224 @Override 225 public String toString() { 226 return member.toString(); 227 } 228 229 /** 230 * Returns {@code true} if this is an overridable method. Constructors, private, static or final 231 * methods, or methods declared by final classes are not overridable. 232 */ 233 public abstract boolean isOverridable(); 234 235 /** Returns {@code true} if this was declared to take a variable number of arguments. */ 236 public abstract boolean isVarArgs(); 237 238 /** 239 * Invokes with {@code receiver} as 'this' and {@code args} passed to the underlying method and 240 * returns the return value; or calls the underlying constructor with {@code args} and returns the 241 * constructed instance. 242 * 243 * @throws IllegalAccessException if this {@code Constructor} object enforces Java language access 244 * control and the underlying method or constructor is inaccessible. 245 * @throws IllegalArgumentException if the number of actual and formal parameters differ; if an 246 * unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a 247 * parameter value cannot be converted to the corresponding formal parameter type by a method 248 * invocation conversion. 249 * @throws InvocationTargetException if the underlying method or constructor throws an exception. 250 */ 251 // All subclasses are owned by us and we'll make sure to get the R type right, including nullness. 252 @SuppressWarnings({"unchecked", "nullness"}) 253 @CanIgnoreReturnValue 254 public final @Nullable R invoke(@Nullable T receiver, @Nullable Object... args) 255 throws InvocationTargetException, IllegalAccessException { 256 return (R) invokeInternal(receiver, checkNotNull(args)); 257 } 258 259 /** Returns the return type of this {@code Invokable}. */ 260 // All subclasses are owned by us and we'll make sure to get the R type right. 261 @SuppressWarnings("unchecked") 262 public final TypeToken<? extends R> getReturnType() { 263 return (TypeToken<? extends R>) TypeToken.of(getGenericReturnType()); 264 } 265 266 /** 267 * Returns all declared parameters of this {@code Invokable}. Note that if this is a constructor 268 * of a non-static inner class, unlike {@link Constructor#getParameterTypes}, the hidden {@code 269 * this} parameter of the enclosing class is excluded from the returned parameters. 270 */ 271 @IgnoreJRERequirement 272 public final ImmutableList<Parameter> getParameters() { 273 Type[] parameterTypes = getGenericParameterTypes(); 274 Annotation[][] annotations = getParameterAnnotations(); 275 @Nullable Object[] annotatedTypes = 276 ANNOTATED_TYPE_EXISTS ? getAnnotatedParameterTypes() : new Object[parameterTypes.length]; 277 ImmutableList.Builder<Parameter> builder = ImmutableList.builder(); 278 for (int i = 0; i < parameterTypes.length; i++) { 279 builder.add( 280 new Parameter( 281 this, i, TypeToken.of(parameterTypes[i]), annotations[i], annotatedTypes[i])); 282 } 283 return builder.build(); 284 } 285 286 /** Returns all declared exception types of this {@code Invokable}. */ 287 public final ImmutableList<TypeToken<? extends Throwable>> getExceptionTypes() { 288 ImmutableList.Builder<TypeToken<? extends Throwable>> builder = ImmutableList.builder(); 289 for (Type type : getGenericExceptionTypes()) { 290 // getGenericExceptionTypes() will never return a type that's not exception 291 @SuppressWarnings("unchecked") 292 TypeToken<? extends Throwable> exceptionType = 293 (TypeToken<? extends Throwable>) TypeToken.of(type); 294 builder.add(exceptionType); 295 } 296 return builder.build(); 297 } 298 299 /** 300 * Explicitly specifies the return type of this {@code Invokable}. For example: 301 * 302 * <pre>{@code 303 * Method factoryMethod = Person.class.getMethod("create"); 304 * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class); 305 * }</pre> 306 */ 307 public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { 308 return returning(TypeToken.of(returnType)); 309 } 310 311 /** Explicitly specifies the return type of this {@code Invokable}. */ 312 public final <R1 extends R> Invokable<T, R1> returning(TypeToken<R1> returnType) { 313 if (!returnType.isSupertypeOf(getReturnType())) { 314 throw new IllegalArgumentException( 315 "Invokable is known to return " + getReturnType() + ", not " + returnType); 316 } 317 @SuppressWarnings("unchecked") // guarded by previous check 318 Invokable<T, R1> specialized = (Invokable<T, R1>) this; 319 return specialized; 320 } 321 322 @SuppressWarnings("unchecked") // The declaring class is T's raw class, or one of its supertypes. 323 @Override 324 public final Class<? super T> getDeclaringClass() { 325 return (Class<? super T>) member.getDeclaringClass(); 326 } 327 328 /** Returns the type of {@code T}. */ 329 // Overridden in TypeToken#method() and TypeToken#constructor() 330 @SuppressWarnings("unchecked") // The declaring class is T. 331 public TypeToken<T> getOwnerType() { 332 return (TypeToken<T>) TypeToken.of(getDeclaringClass()); 333 } 334 335 abstract @Nullable Object invokeInternal(@Nullable Object receiver, @Nullable Object[] args) 336 throws InvocationTargetException, IllegalAccessException; 337 338 abstract Type[] getGenericParameterTypes(); 339 340 @SuppressWarnings("Java7ApiChecker") 341 abstract AnnotatedType[] getAnnotatedParameterTypes(); 342 343 /** This should never return a type that's not a subtype of Throwable. */ 344 abstract Type[] getGenericExceptionTypes(); 345 346 abstract Annotation[][] getParameterAnnotations(); 347 348 abstract Type getGenericReturnType(); 349 350 /** 351 * Returns the {@link AnnotatedType} for the return type. 352 * 353 * @since 14.0 354 */ 355 @SuppressWarnings("Java7ApiChecker") 356 public abstract AnnotatedType getAnnotatedReturnType(); 357 358 static class MethodInvokable<T> extends Invokable<T, Object> { 359 360 final Method method; 361 362 MethodInvokable(Method method) { 363 super(method); 364 this.method = method; 365 } 366 367 @Override 368 final @Nullable Object invokeInternal(@Nullable Object receiver, @Nullable Object[] args) 369 throws InvocationTargetException, IllegalAccessException { 370 return method.invoke(receiver, args); 371 } 372 373 @Override 374 Type getGenericReturnType() { 375 return method.getGenericReturnType(); 376 } 377 378 @Override 379 Type[] getGenericParameterTypes() { 380 return method.getGenericParameterTypes(); 381 } 382 383 @Override 384 @SuppressWarnings("Java7ApiChecker") 385 AnnotatedType[] getAnnotatedParameterTypes() { 386 return method.getAnnotatedParameterTypes(); 387 } 388 389 @Override 390 @SuppressWarnings("Java7ApiChecker") 391 public AnnotatedType getAnnotatedReturnType() { 392 return method.getAnnotatedReturnType(); 393 } 394 395 @Override 396 Type[] getGenericExceptionTypes() { 397 return method.getGenericExceptionTypes(); 398 } 399 400 @Override 401 final Annotation[][] getParameterAnnotations() { 402 return method.getParameterAnnotations(); 403 } 404 405 @Override 406 public final TypeVariable<?>[] getTypeParameters() { 407 return method.getTypeParameters(); 408 } 409 410 @Override 411 public final boolean isOverridable() { 412 return !(isFinal() 413 || isPrivate() 414 || isStatic() 415 || Modifier.isFinal(getDeclaringClass().getModifiers())); 416 } 417 418 @Override 419 public final boolean isVarArgs() { 420 return method.isVarArgs(); 421 } 422 } 423 424 static class ConstructorInvokable<T> extends Invokable<T, T> { 425 426 final Constructor<?> constructor; 427 428 ConstructorInvokable(Constructor<?> constructor) { 429 super(constructor); 430 this.constructor = constructor; 431 } 432 433 @Override 434 final Object invokeInternal(@Nullable Object receiver, @Nullable Object[] args) 435 throws InvocationTargetException, IllegalAccessException { 436 try { 437 return constructor.newInstance(args); 438 } catch (InstantiationException e) { 439 throw new RuntimeException(constructor + " failed.", e); 440 } 441 } 442 443 /** 444 * If the class is parameterized, such as {@link java.util.ArrayList ArrayList}, this returns 445 * {@code ArrayList<E>}. 446 */ 447 @Override 448 Type getGenericReturnType() { 449 Class<?> declaringClass = getDeclaringClass(); 450 TypeVariable<?>[] typeParams = declaringClass.getTypeParameters(); 451 if (typeParams.length > 0) { 452 return Types.newParameterizedType(declaringClass, typeParams); 453 } else { 454 return declaringClass; 455 } 456 } 457 458 @Override 459 Type[] getGenericParameterTypes() { 460 Type[] types = constructor.getGenericParameterTypes(); 461 if (types.length > 0 && mayNeedHiddenThis()) { 462 Class<?>[] rawParamTypes = constructor.getParameterTypes(); 463 if (types.length == rawParamTypes.length 464 && rawParamTypes[0] == getDeclaringClass().getEnclosingClass()) { 465 // first parameter is the hidden 'this' 466 return Arrays.copyOfRange(types, 1, types.length); 467 } 468 } 469 return types; 470 } 471 472 @Override 473 @SuppressWarnings("Java7ApiChecker") 474 AnnotatedType[] getAnnotatedParameterTypes() { 475 return constructor.getAnnotatedParameterTypes(); 476 } 477 478 @Override 479 @SuppressWarnings("Java7ApiChecker") 480 public AnnotatedType getAnnotatedReturnType() { 481 return constructor.getAnnotatedReturnType(); 482 } 483 484 @Override 485 Type[] getGenericExceptionTypes() { 486 return constructor.getGenericExceptionTypes(); 487 } 488 489 @Override 490 final Annotation[][] getParameterAnnotations() { 491 return constructor.getParameterAnnotations(); 492 } 493 494 /** 495 * {@inheritDoc} 496 * 497 * <p>{@code [<E>]} will be returned for ArrayList's constructor. When both the class and the 498 * constructor have type parameters, the class parameters are prepended before those of the 499 * constructor's. This is an arbitrary rule since no existing language spec mandates one way or 500 * the other. From the declaration syntax, the class type parameter appears first, but the call 501 * syntax may show up in opposite order such as {@code new <A>Foo<B>()}. 502 */ 503 @Override 504 public final TypeVariable<?>[] getTypeParameters() { 505 TypeVariable<?>[] declaredByClass = getDeclaringClass().getTypeParameters(); 506 TypeVariable<?>[] declaredByConstructor = constructor.getTypeParameters(); 507 TypeVariable<?>[] result = 508 new TypeVariable<?>[declaredByClass.length + declaredByConstructor.length]; 509 System.arraycopy(declaredByClass, 0, result, 0, declaredByClass.length); 510 System.arraycopy( 511 declaredByConstructor, 0, result, declaredByClass.length, declaredByConstructor.length); 512 return result; 513 } 514 515 @Override 516 public final boolean isOverridable() { 517 return false; 518 } 519 520 @Override 521 public final boolean isVarArgs() { 522 return constructor.isVarArgs(); 523 } 524 525 private boolean mayNeedHiddenThis() { 526 Class<?> declaringClass = constructor.getDeclaringClass(); 527 if (declaringClass.getEnclosingConstructor() != null) { 528 // Enclosed in a constructor, needs hidden this 529 return true; 530 } 531 Method enclosingMethod = declaringClass.getEnclosingMethod(); 532 if (enclosingMethod != null) { 533 // Enclosed in a method, if it's not static, must need hidden this. 534 return !Modifier.isStatic(enclosingMethod.getModifiers()); 535 } else { 536 // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of 537 // static initializer. But there seems no way to tell in that case. :( 538 // This may cause issues when an anonymous class is created inside a static initializer, 539 // and the class's constructor's first parameter happens to be the enclosing class. 540 // In such case, we may mistakenly think that the class is within a non-static context 541 // and the first parameter is the hidden 'this'. 542 return declaringClass.getEnclosingClass() != null 543 && !Modifier.isStatic(declaringClass.getModifiers()); 544 } 545 } 546 } 547 548 private static final boolean ANNOTATED_TYPE_EXISTS = initAnnotatedTypeExists(); 549 550 private static boolean initAnnotatedTypeExists() { 551 try { 552 Class.forName("java.lang.reflect.AnnotatedType"); 553 } catch (ClassNotFoundException e) { 554 return false; 555 } 556 return true; 557 } 558}