spring-framework / org.springframework.util / ReflectionUtils

ReflectionUtils

abstract class ReflectionUtils

Simple utility class for working with the reflection API and handling reflection exceptions.

Only intended for internal use.

Author
Juergen Hoeller

Author
Rob Harrop

Author
Rod Johnson

Author
Costin Leau

Author
Sam Brannen

Author
Chris Beams

Since
1.2.2

Constructors

<init>

ReflectionUtils()

Simple utility class for working with the reflection API and handling reflection exceptions.

Only intended for internal use.

Properties

COPYABLE_FIELDS

static val COPYABLE_FIELDS: FieldFilter

Pre-built FieldFilter that matches all non-static, non-final fields.

NON_BRIDGED_METHODS

static val NON_BRIDGED_METHODS: MethodFilter

Pre-built MethodFilter that matches all non-bridge methods.

USER_DECLARED_METHODS

static val USER_DECLARED_METHODS: MethodFilter

Pre-built MethodFilter that matches all non-bridge methods which are not declared on java.lang.Object.

Functions

accessibleConstructor

open static fun <T : Any> accessibleConstructor(clazz: Class<T>, vararg parameterTypes: Class<*>): Constructor<T>

Obtain an accessible constructor for the given class and parameters.

clearCache

open static fun clearCache(): Unit

Clear the internal method/field cache.

declaresException

open static fun declaresException(method: Method, exceptionType: Class<*>): Boolean

Determine whether the given method explicitly declares the given exception or one of its superclasses, which means that an exception of that type can be propagated as-is within a reflective invocation.

doWithFields

open static fun doWithFields(clazz: Class<*>, fc: FieldCallback): Unit
open static fun doWithFields(clazz: Class<*>, fc: FieldCallback, ff: FieldFilter): Unit

Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

doWithLocalFields

open static fun doWithLocalFields(clazz: Class<*>, fc: FieldCallback): Unit

Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

doWithLocalMethods

open static fun doWithLocalMethods(clazz: Class<*>, mc: MethodCallback): Unit

Perform the given callback operation on all matching methods of the given class, as locally declared or equivalent thereof (such as default methods on Java 8 based interfaces that the given class implements).

doWithMethods

open static fun doWithMethods(clazz: Class<*>, mc: MethodCallback): Unit

Perform the given callback operation on all matching methods of the given class and superclasses.

The same named method occurring on subclass and superclass will appear twice, unless excluded by a MethodFilter.

open static fun doWithMethods(clazz: Class<*>, mc: MethodCallback, mf: MethodFilter): Unit

Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified MethodFilter.

findField

open static fun findField(clazz: Class<*>, name: String): Field

Attempt to find a Field on the supplied Class with the supplied name. Searches all superclasses up to Object.

open static fun findField(clazz: Class<*>, name: String, type: Class<*>): Field

Attempt to find a Field on the supplied Class with the supplied name and/or Class. Searches all superclasses up to Object.

findMethod

open static fun findMethod(clazz: Class<*>, name: String): Method

Attempt to find a Method on the supplied class with the supplied name and no parameters. Searches all superclasses up to Object.

Returns null if no Method can be found.

open static fun findMethod(clazz: Class<*>, name: String, vararg paramTypes: Class<*>): Method

Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.

Returns null if no Method can be found.

getAllDeclaredMethods

open static fun getAllDeclaredMethods(leafClass: Class<*>): Array<Method>

Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.

getField

open static fun getField(field: Field, target: Any): Any

Get the field represented by the supplied Field on the specified Object. In accordance with Field#get(Object) semantics, the returned value is automatically wrapped if the underlying field has a primitive type.

Thrown exceptions are handled via a call to #handleReflectionException(Exception).

getUniqueDeclaredMethods

open static fun getUniqueDeclaredMethods(leafClass: Class<*>): Array<Method>

Get the unique set of declared methods on the leaf class and all superclasses. Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out.

handleInvocationTargetException

open static fun handleInvocationTargetException(ex: InvocationTargetException): Unit

Handle the given invocation target exception. Should only be called if no checked exception is expected to be thrown by the target method.

Throws the underlying RuntimeException or Error in case of such a root cause. Throws an UndeclaredThrowableException otherwise.

handleReflectionException

open static fun handleReflectionException(ex: Exception): Unit

Handle the given reflection exception. Should only be called if no checked exception is expected to be thrown by the target method.

Throws the underlying RuntimeException or Error in case of an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message or UndeclaredThrowableException otherwise.

invokeJdbcMethod

open static fun invokeJdbcMethod(method: Method, target: Any): Any

Invoke the specified JDBC API Method against the supplied target object with no arguments.

open static fun invokeJdbcMethod(method: Method, target: Any, vararg args: Any): Any

Invoke the specified JDBC API Method against the supplied target object with the supplied arguments.

invokeMethod

open static fun invokeMethod(method: Method, target: Any): Any

Invoke the specified Method against the supplied target object with no arguments. The target object can be null when invoking a static Method.

Thrown exceptions are handled via a call to #handleReflectionException.

open static fun invokeMethod(method: Method, target: Any, vararg args: Any): Any

Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.

Thrown exceptions are handled via a call to #handleReflectionException.

isCglibRenamedMethod

open static fun isCglibRenamedMethod(renamedMethod: Method): Boolean

Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".

isEqualsMethod

open static fun isEqualsMethod(method: Method): Boolean

Determine whether the given method is an "equals" method.

isHashCodeMethod

open static fun isHashCodeMethod(method: Method): Boolean

Determine whether the given method is a "hashCode" method.

isObjectMethod

open static fun isObjectMethod(method: Method): Boolean

Determine whether the given method is originally declared by java.lang.Object.

isPublicStaticFinal

open static fun isPublicStaticFinal(field: Field): Boolean

Determine whether the given field is a "public static final" constant.

isToStringMethod

open static fun isToStringMethod(method: Method): Boolean

Determine whether the given method is a "toString" method.

makeAccessible

open static fun makeAccessible(field: Field): Unit

Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).

open static fun makeAccessible(method: Method): Unit

Make the given method accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).

open static fun makeAccessible(ctor: Constructor<*>): Unit

Make the given constructor accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).

rethrowException

open static fun rethrowException(ex: Throwable): Unit

Rethrow the given Throwable, which is presumably the target exception of an InvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.

Rethrows the underlying exception cast to an Exception or Error if appropriate; otherwise, throws an UndeclaredThrowableException.

rethrowRuntimeException

open static fun rethrowRuntimeException(ex: Throwable): Unit

Rethrow the given Throwable, which is presumably the target exception of an InvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.

Rethrows the underlying exception cast to a RuntimeException or Error if appropriate; otherwise, throws an UndeclaredThrowableException.

setField

open static fun setField(field: Field, target: Any, value: Any): Unit

Set the field represented by the supplied Field on the specified Object to the specified value. In accordance with Field#set(Object, Object) semantics, the new value is automatically unwrapped if the underlying field has a primitive type.

Thrown exceptions are handled via a call to #handleReflectionException(Exception).

shallowCopyFieldState

open static fun shallowCopyFieldState(src: Any, dest: Any): Unit

Given the source object and the destination, which must be the same class or a subclass, copy all fields, including inherited fields. Designed to work on objects with public no-arg constructors.