Go to a Type with a dynamic name (Java)
I am creating a program which should be able to open different perspectives. Perspectives, in my program, mean different layouts of the controls. The user is able to create and pick which perspective they want to use. There is also a Default Perspective. The Default Perspective can be chosen.
Because of this I do not know how many perspectives there are, or what the Default Perspective is. My program uses engine.perspective
to Read and Write to the text file (Perspective.TXT
) in which the all the perspectives are stored. When the user creates a perspective my program automatically makes a *.JAVA
file with all the perspective data. This is my code for reading Perspective.TXT
and calling the correct type:
ArrayList<String> ALLPRSPCTVE = Read.getAllPerspectives();
String PRSPCTVE = Read.getDefaultPerspective();
{
String[] ARR = (String[]) ALLPRSPCTVE.toArray();
for (int i = 0; i < ALLPRSPCTVE.size(); i++) {// Set All Perspectives
if (PRSPCTVE == ARR[i]) {
}
}
}
How do I change the String
returned from the method Read.getAllPerspectives
to something JAVA will recognize as a type and g开发者_开发技巧o to it?
You have a software architecture problem.
But, if you want to do exactly what you say, you can still make it possible, even though it's not the right way.
- you have to call the java compiler to transform your java file into class file
- and then you can use reflection to load dynamically a class (using
Class.forName
) - and finally instanciate this class to get a new object whose methods can be used, which would be much easier if you had a common interface for all those classes.
But, please don't do that and go on reading.
Alan Turing introduced modern computer at the middle of the 20's century. One of its main features is that it dissociates data from code.
Data is the a part of memory containing variables and code is a set of instructions that manipulates those data (and other creating during programs's lifes. In java, program/code is written inside classes).
Data can then be divided into two different support : dynamic, fast and volatile memory (modern RAM) and long term storage (hard disk/database).
Note that code is data. It has to be saved on disk, loaded in memory, but it is executed. Simple data is not executed, it's mere data, read and written but not executed.
This has very practical impliactions for all applications, and you application is violating his separation of data and code has you use code (java classes) to memorize data.
Here is what you should do :
- save your data in a format that has nothing to do with java. For a simple format in which fields have values, you could use a property file (tutorial here). This will be your long term storage solution.
- Then try to wrap together all functions related to reading, manipulating and writing those properties in a separate class, with public methods that your other classes will be able to call to have access to this data.
use Class.forName(name).newInstance()
精彩评论