- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- java.lang.reflect.Executable
-
- java.lang.reflect.Method
-
- All Implemented Interfaces:
AnnotatedElement,GenericDeclaration,Member
public final class Method extends Executable
AMethodprovides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).A
Methodpermits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws anIllegalArgumentExceptionif a narrowing conversion would occur.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)Compares thisMethodagainst the specified object.AnnotatedTypegetAnnotatedReturnType()Returns anAnnotatedTypeobject that represents the use of a type to specify the return type of the method/constructor represented by this Executable.<T extends Annotation>
TgetAnnotation(Class<T> annotationClass)Returns this element's annotation for the specified type if such an annotation is present, else null.Annotation[]getDeclaredAnnotations()Returns annotations that are directly present on this element.Class<?>getDeclaringClass()Returns theClassobject representing the class or interface that declares the method represented by this object.ObjectgetDefaultValue()Returns the default value for the annotation member represented by thisMethodinstance.Class<?>[]getExceptionTypes()Returns an array ofClassobjects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.Type[]getGenericExceptionTypes()Returns an array ofTypeobjects that represent the exceptions declared to be thrown by this executable object.Type[]getGenericParameterTypes()Returns an array ofTypeobjects that represent the formal parameter types, in declaration order, of the executable represented by this object.TypegetGenericReturnType()Returns aTypeobject that represents the formal return type of the method represented by thisMethodobject.intgetModifiers()Returns the Java language modifiers for the executable represented by this object.StringgetName()Returns the name of the method represented by thisMethodobject, as aString.Annotation[][]getParameterAnnotations()Returns an array of arrays ofAnnotations that represent the annotations on the formal parameters, in declaration order, of theExecutablerepresented by this object.intgetParameterCount()Returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.Class<?>[]getParameterTypes()Returns an array ofClassobjects that represent the formal parameter types, in declaration order, of the executable represented by this object.Class<?>getReturnType()Returns aClassobject that represents the formal return type of the method represented by thisMethodobject.TypeVariable<Method>[]getTypeParameters()Returns an array ofTypeVariableobjects that represent the type variables declared by the generic declaration represented by thisGenericDeclarationobject, in declaration order.inthashCode()Returns a hashcode for thisMethod.Objectinvoke(Object obj, Object... args)Invokes the underlying method represented by thisMethodobject, on the specified object with the specified parameters.booleanisBridge()Returnstrueif this method is a bridge method; returnsfalseotherwise.booleanisDefault()Returnstrueif this method is a default method; returnsfalseotherwise.booleanisSynthetic()Returnstrueif this executable is a synthetic construct; returnsfalseotherwise.booleanisVarArgs()Returnstrueif this executable was declared to take a variable number of arguments; returnsfalseotherwise.voidsetAccessible(boolean flag)Set theaccessibleflag for this reflected object to the indicated boolean value.StringtoGenericString()Returns a string describing thisMethod, including type parameters.StringtoString()Returns a string describing thisMethod.-
Methods inherited from class java.lang.reflect.AccessibleObject
canAccess, getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAccessible, isAnnotationPresent, setAccessible, trySetAccessible
-
Methods inherited from interface java.lang.reflect.AnnotatedElement
getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAnnotationPresent
-
Methods inherited from class java.lang.reflect.Executable
getAnnotatedExceptionTypes, getAnnotatedParameterTypes, getAnnotatedReceiverType, getAnnotationsByType, getParameters
-
-
-
-
Method Detail
-
setAccessible
public void setAccessible(boolean flag)
Description copied from class:AccessibleObjectSet theaccessibleflag for this reflected object to the indicated boolean value. A value oftrueindicates that the reflected object should suppress checks for Java language access control when it is used. A value offalseindicates that the reflected object should enforce checks for Java language access control when it is used, with the variation noted in the class description.This method may be used by a caller in class
Cto enable access to amemberofdeclaring classDif any of the following hold:-
CandDare in the same module. - The member is
publicandDispublicin a package that the module containingDexportsto at least the module containingC. - The member is
protectedstatic,Dispublicin a package that the module containingDexports to at least the module containingC, andCis a subclass ofD. -
Dis in a package that the module containingDopensto at least the module containingC. All packages in unnamed and open modules are open to all modules and so this method always succeeds whenDis in an unnamed or open module.
This method cannot be used to enable access to private members, members with default (package) access, protected instance members, or protected constructors when the declaring class is in a different module to the caller and the package containing the declaring class is not open to the caller's module.
If there is a security manager, its
checkPermissionmethod is first called with aReflectPermission("suppressAccessChecks")permission.- Overrides:
setAccessiblein classAccessibleObject- Parameters:
flag- the new value for theaccessibleflag- Throws:
InaccessibleObjectException- if access cannot be enabledSecurityException- if the request is denied by the security manager- See Also:
AccessibleObject.trySetAccessible(),MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
-
-
getDeclaringClass
public Class<?> getDeclaringClass()
Returns theClassobject representing the class or interface that declares the method represented by this object.- Specified by:
-
-