Another Issue about Class.forName, Class.getMethod, and having *.java files work correctly
I am a C/C++ native and I have just enough knowledge to be dangerous to myself in Java and in the Processing IDE. I am trying to create a class hierarchy and interface for different matrix shapes, which will be used to graph a 2D surface in 4D, while only showing 3 of 4 axes.
I am giving the background here. Jump down to see my current problem.
I am graphing complex numbers as inputs and outputs of a one-variable function (i.e. any polynomial with whole degrees only – I started with x^2). I wanted to look at the relationships between various simple grid shapes composed of points that would be evaluated and graphed. For this, I created a means to have the equivalent of a triangle matrix, but did it as a 1D array instead of a sparse matrix. As a result, iterating through each shape requires unique code. Calculating the points on a diagonal of a triangle matrix is different from a square matrix. Without an abstract class or interface, I had to define code sections for particular grid shapes all in the same function, and I really wanted to get it cleaned up. After reaching my goal, I have been going back to group the unique sections into specialized classes that implement a ShapeMatrix interface (e.g. class TriangleMatrix has all of its special cases in separate functions that are part of the interface).
My classes
Complex (two member variables, floats to express real and imaginary parts)
ComplexArray (its only member variable is an array of Complex)
TriangleMatrix implements ShapeMatrix extends ComplexArray
RectangleMatrix implements ShapeMatrix extends ComplexArray
LineMatrix implements ShapeMatrix extends ComplexArray
ComplexFunction (two member variables of type ShapeMatrix: inputs and outputs)
ComplexFunction3D extends ComplexFunction
My Interface
ShapeMatrix {drawPoints(); drawLines2D(); drawLines3D(ComplexFunction3D);}
Inside of ComplexFunction3D I started running into an issue. It is the only class that has more data than its ShapeMatrix member data can work on via one function call. Also, conceptually, any class implementing ShapeMatrix would conceivably only have one array inside of it. Thus, I settled on this inside of ComplexFunction3D:
-
inputs.drawLines3D(this);
I feel that drawLines3D() should be a static function, but I can’t use a specific class name to invoke it directly. Thus I turned to reflection in hopes of sorting it out.
Something like:
-
Class cls = inputs.getClass();
-
Method meth = cls.getMethod("drawLines3D", this.getClass());
-
meth.invoke(inputs, this);