mardi 17 octobre 2017

getDeclaredConstructor doesn't find constructor with interface as parameter

I have an interface

public interface IDrawing

Next I have a class implementing this interface:

public class Line implements IDrawing

Now I have a class with a constructor that accepts above as parameter:

public LineChanger(Line line)

No problems there.

When I use reflection to call above constructor, it throws a NoSuchMethodException

//drawing variable is of class Line implementing IDrawing
.getDeclaredConstructor(IDrawing.class).newInstance(drawing);

The exception goes away when I change the constructor to:

public LineChanger(IDrawing line)

Is it possible to get the reflection working without changing the constructor?

I wish to keep the constructor strict (i.e. only accept one specific class in this case Line) but keep the reflection working for my factories.





Aucun commentaire:

Enregistrer un commentaire