Java reflection constructor call

Class Constructor

Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor’s formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.

Field Summary

Fields declared in interface java.lang.reflect.Member

Method Summary

Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method/constructor represented by this Executable object.

Returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor represented by this Executable.

Returns this element’s annotation for the specified type if such an annotation is present, else null.

Returns the Class object representing the class that declares the constructor represented by this object.

Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.

Returns an array of Type objects that represent the exceptions declared to be thrown by this executable object.

Returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

Returns an array of arrays of Annotation s that represent the annotations on the formal parameters, in declaration order, of the Executable represented by this object.

Returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.

Returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Returns true if this executable was declared to take a variable number of arguments; returns false otherwise.

Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor’s declaring class, with the specified initialization parameters.

Methods declared in class java.lang.reflect.Executable

Methods declared in class java.lang.reflect.AccessibleObject

Methods declared in class java.lang.Object

Methods declared in interface java.lang.reflect.AnnotatedElement

Method Details

setAccessible

  • C and D are in the same module.
  • The member is public and D is public in a package that the module containing D exports to at least the module containing C .
  • The member is protected static , D is public in a package that the module containing D exports to at least the module containing C , and C is a subclass of D .
  • D is in a package that the module containing D opens to at least the module containing C . All packages in unnamed and open modules are open to all modules and so this method always succeeds when D is in an unnamed or open module.
Читайте также:  Python convert unicode to byte

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.

  • static final fields declared in any class or interface
  • final fields declared in a hidden class
  • final fields declared in a record

The accessible flag when true suppresses Java language access control checks to only enable read access to these non-modifiable final fields.

If there is a security manager, its checkPermission method is first called with a ReflectPermission(«suppressAccessChecks») permission.

A SecurityException is also thrown if this object is a Constructor object for the class Class and flag is true.

getDeclaringClass

Returns the Class object representing the class that declares the constructor represented by this object.

getName

Returns the name of this constructor, as a string. This is the binary name of the constructor’s declaring class.

getModifiers

getTypeParameters

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order. Returns an array of length 0 if the underlying generic declaration declares no type variables.

getParameterTypes

Returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object. Returns an array of length 0 if the underlying executable takes no parameters. Note that the constructors of some inner classes may have an implicitly declared parameter in addition to explicitly declared ones.

getParameterCount

Returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.

getGenericParameterTypes

Returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object. An array of length 0 is returned if the underlying executable takes no parameters. Note that the constructors of some inner classes may have an implicitly declared parameter in addition to explicitly declared ones. Also note that as a modeling artifact, the number of returned parameters can differ depending on whether or not generic information is present. If generic information is present, only parameters explicitly present in the source will be returned; if generic information is not present, implicit and synthetic parameters may be returned as well. If a formal parameter type is a parameterized type, the Type object returned for it must accurately reflect the actual type arguments used in the source code. If a formal parameter type is a type variable or a parameterized type, it is created. Otherwise, it is resolved.

Читайте также:  Что такое daphne python

getExceptionTypes

Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object. Returns an array of length 0 if the executable declares no exceptions in its throws clause.

getGenericExceptionTypes

Returns an array of Type objects that represent the exceptions declared to be thrown by this executable object. Returns an array of length 0 if the underlying executable declares no exceptions in its throws clause. If an exception type is a type variable or a parameterized type, it is created. Otherwise, it is resolved.

equals

Compares this Constructor against the specified object. Returns true if the objects are the same. Two Constructor objects are the same if they were declared by the same class and have the same formal parameter types.

hashCode

Returns a hashcode for this Constructor . The hashcode is the same as the hashcode for the underlying constructor’s declaring class name.

toString

Returns a string describing this Constructor . The string is formatted as the constructor access modifiers, if any, followed by the fully-qualified name of the declaring class, followed by a parenthesized, comma-separated list of the constructor’s formal parameter types. For example:

 public java.util.HashMap(int,float) 

If the constructor is declared to throw exceptions, the parameter list is followed by a space, followed by the word » throws » followed by a comma-separated list of the thrown exception types. The only possible modifiers for constructors are the access modifiers public , protected or private . Only one of these may appear, or none if the constructor has default (package) access.

Читайте также:  Resttemplate get with headers java

toGenericString

Returns a string describing this Constructor , including type parameters. The string is formatted as the constructor access modifiers, if any, followed by an angle-bracketed comma separated list of the constructor’s type parameters, if any, including informative bounds of the type parameters, if any, followed by the fully-qualified name of the declaring class, followed by a parenthesized, comma-separated list of the constructor’s generic formal parameter types. If this constructor was declared to take a variable number of arguments, instead of denoting the last parameter as » Type[] «, it is denoted as » Type. «. A space is used to separate access modifiers from one another and from the type parameters or class name. If there are no type parameters, the type parameter list is elided; if the type parameter list is present, a space separates the list from the class name. If the constructor is declared to throw exceptions, the parameter list is followed by a space, followed by the word » throws » followed by a comma-separated list of the generic thrown exception types. The only possible modifiers for constructors are the access modifiers public , protected or private . Only one of these may appear, or none if the constructor has default (package) access.

newInstance

Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor’s declaring class, with the specified initialization parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary. If the number of formal parameters required by the underlying constructor is 0, the supplied initargs array may be of length 0 or null. If the constructor’s declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java Language Specification . If the required access and argument checks succeed and the instantiation will proceed, the constructor’s declaring class is initialized if it has not already been initialized. If the constructor completes normally, returns the newly created and initialized instance.

isVarArgs

Returns true if this executable was declared to take a variable number of arguments; returns false otherwise.

isSynthetic

Источник

Оцените статью