The javadoc for Class::getDeclaredConstructor
(http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-) says:
Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object. [emphasis mine]
Since you can't declare a constructor for an interface, what could it mean to return a "specified constructor" of an interface?
I tried it on Runnable.class
and got NoSuchMethodException
. Is there a case where getDeclaredConstructor
will work on an interface? Or is this language in the javadoc just an error? Or does it mean something other than how I'm interpreting it?
getDeclaredConstructors() Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. Field.
lang Description. Provides classes that are fundamental to the design of the Java programming language. The most important classes are Object , which is the root of the class hierarchy, and Class , instances of which represent classes at run time.
Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
A call to Class.getConstructor
will result in a call to Class.privateGetDeclaredConstructors
to retrieve all declared constructors. The matching constructor is selected from that list:
private Constructor<T>[] privateGetDeclaredConstructors(boolean publicOnly) { ... // No cached value available; request value from VM if (isInterface()) { @SuppressWarnings("unchecked") Constructor<T>[] temporaryRes = (Constructor<T>[]) new Constructor<?>[0]; res = temporaryRes; } else { res = getDeclaredConstructors0(publicOnly); } ... return res; }
(I removed part of the code which deals with cached constructors).
So for interfaces the constructor list is always empty and a NoSuchMethodException
will always be thrown.
I don't consider this an error in javadoc. Class
object can represent class or interface, there is no error in that statement.
If you're using reflection and explicitly ask for specific elements, you have to make sure that elements with the identification specified in the reflective calls exist. If you're asking for a specific constructor that does not exist in the provided Class
, you'll get NoSuchMethodException
be it a class for an interface, primitive type, array, void or simply a class that does not declare such a constructor.
Above I emphasised the word specific. For example, in a similar method that returns all constructors for a Class
(Class::getDeclaredConstructors) interfaces are handled appropriately:
Returns an array of
Constructor
objects reflecting all the constructors declared by the class represented by thisClass
object. These are public, protected, default (package) access, and private constructors. The elements in the array returned are not sorted and are not in any particular order. If the class has a default constructor, it is included in the returned array. This method returns an array of length 0 if thisClass
object represents an interface, a primitive type, an array class, or void.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With