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?
rectangleandtrianglehave appropriate constructors.