Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getDeclaredConstructor on an interface?

Tags:

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?

like image 871
ajb Avatar asked Apr 12 '16 05:04

ajb


People also ask

What is Getdeclaredconstructor in Java?

getDeclaredConstructors() Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. Field.

Is Java Lang a Class?

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.

What is the use of Class Class in Java?

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.


2 Answers

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.

like image 72
wero Avatar answered Sep 24 '22 16:09

wero


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 this Class 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 this Class object represents an interface, a primitive type, an array class, or void.

like image 39
Dragan Bozanovic Avatar answered Sep 23 '22 16:09

Dragan Bozanovic