In Java, I want to create a few geometric shapes based on user input. The trick is that I can’t change the existing API, so I can’t use varargs syntax like
public Shape(Object... attrs) {}
User input:
shape.1.triangle.arg1 = 3
shape.1.triangle.arg2 = 4
shape.1.triangle.arg3 = 5
shape.1.triangle.arg4 = "My first triangle"
shape.2.rectangle.arg1 = 4
shape.2.rectangle.arg2 = 7
shape.2.rectangle.arg3 = "Another string label"
Should lead to method invocations like:
Shape s1 = new Triangle(arg1, arg2, arg3, arg4);
Or generically to:
String shapeType = "triangle";
Object[] args = {arg1, arg2, arg3, arg4};
// This won't work, because newInstance() doesn't take args
Shape s1 = Class.forName(shapeType).newInstance(args);
String shapeType = "rectangle";
Object[] args = {arg1, arg2, arg3};
// This won't work, because newInstance() doesn't take args
Shape s2 = Class.forName(shapeType).newInstance(args);
The problem is that the constructor for Triangle does not allow varargs (…), and I can’t change it. Specifically, the constructors are
public Triangle (int a, int b, int c, String label) {}
public Rectangle (int a, int b, String label) {}
So how do I create the right shapes based on user input?
This is an interesting approach. The problem is not that
Trianglelacks a vararg constructor, but rather the fact thannewInstance()can only invoke the default constructor.mschaef has proposed a reasonable approach. It does still require a static awareness of the constructor forms of each shape. This knowledge will then be hidden in the respective factories. It is probably the best solution. The problem is that you have to write a factory for each shape.
However, you can write code that dynamically invokes the correct constructor using the reflection APIs. If you have lots of shapes, that will solve the problem in one place for all of them. You would obtain the
Classobject, and then callgetDeclaredConstructors()to obtain an array of constructors. Constructor argument types can be queried bygetParameterTypes(). Your code would have to find the ideal constructor based on the parameters you have. This is essentially what the compiler would have done with static types.This solution is not particularly elegant, but it does have plus points:
If you write it correctly once, it will always work for any new shape that you might introduce. You can place the code in a neat utility class and never look at it again.
In general, I still consider using reflection as an undesirable choice.