Kian Peng Yong

Greenhorn
+ Follow
since Aug 29, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kian Peng Yong

Dear sir,

I have a variable name = home.canvas.color.

String home.canvas.color = "purple";

If I obtain an input from user = "home.canvas.color", is there a way that I can retrieve the value "purple" from the actual variable home.canvas.color?

If so, can you show me please?

Thank you very much.

Best regards,
Yong

13 years ago
Dear sir/madam,

I am wondering whether it is possible in JAVA programming world ..... I am trying to make global variables configurable from a configuration file. I know I have the option to upload all my variables into a Hastable and always refer to this Hastable for a value. However, I will loose the friendly feature in my java IDE to find the variable list as I write my codes ..... so I am wondering .....

If I have several classes with the same class name (GlobalVariable.java) but under different package and storing variables. For example:


test.utility.file - GlobalVariables.java
test.utility.database - GlobalVariables.java
:
:
test.product.device - GlobalVariables.java



Is there a way to iterate through these GlobalVariables classes and extract into a single Hastable (assuming all variables in these GlobalVariables has a unique name with the Package full path attached in front of the variable name)?

[1] To obtain the list of Package in a project
[2] To obtain all the GlobalVariables classes from [1]
[3] To obtain the list of variables in all the GlobalVariables classes in [2]
[4] Then .... extract these variables [3] and place them into a hashtable

Thank you very much and hope to hear from you soon.

Best regards,
Yong

14 years ago
Hi Steve,

Thank you so much for helping

I followed your instructions and managed to get it working the way I wanted it:

try {

Class componentClass = Class.forName(componentFullName);

Class[] classes = new Class[1];
classes[0]=String.class;

Constructor ctor = componentClass.getConstructor(classes);

Object[] objects = new Object[1];
objects[0]=componentKey;

Object component = ctor.newInstance(objects);

componentClasses.put(componentKey, component);

}catch(Exception e) {
System.out.println("Unable to initialise component: "+componentKey+" -> "+componentFullName);
}


Looking at the example code you shown me, I can't help wondering is there a better way to declare the class[] and object[] in the code I attached above (in Red)? Can I cast a class to class[] and object to object[] ... or is there a better way/method to do this??

Thank you very much again.

Best regards,
Yong
14 years ago

Steve Luke wrote:Look at the API for Class. It has methods for getting a particular Constructor, if you know its parameter types, and a method for getting an array of constructors which you can search through to find the best CTor for your task.

Once you have the correct Constructor, you can use one of its methods to create a new instance using the Constructor.

See if you can find the correct methods in the API, and let us know if you run into trouble.



Hi Steve, thank you for the quick reply. In order to get the correct constructor, I need to do this:

Constructor[] constructors = FileReader.class.getDeclaredConstructors();

for (int j=0; j<constructors.length; j++) {

Constructor constructor = constructors[j];
Class[] parameterTypes = constructor.getParameterTypes();

for (int i=0; i><parameterTypes.length; i++) {

Class c = parameterTypes[i];
System.out.println("Param type name = " + c.getName());
}
}


Now, .... this code is still not flexible enough to automatically go through the list of modules I am creating (FileReader, TextMapper ... etc).

Also, from the output of this code, I get:

Param type name = java.lang.String

So, this shows me the FileReader class expect a single parameter, which is a String, correct?

Now ... how do I access the componentName which is a public String declared inside the FileReader Class if I want to compare it with a String obtained from a Configuration text file? i.e. if I have the following code:

if (componentValueFromConfigFile.equals(FileReader.componentName)) {
FileReader component = new FileReader(componentKey);
componentClasses.put(componentKey, component);
}

Assuming we can somehow replace the FileReader with something from the Constructor[] ?

Sorry ... I am new to this Dynamic coding ..... Am I getting this totally wrong? ....

Thank you very much and hope to hear from you soon.

Best regards,
Yong
14 years ago

Rob Prime wrote:Not newInstance, but using reflection you could do this:
- use Class.forName to get the Class instance
- get the right Constructor
- create an instance using that Constructor



Could you give me an example please? Thank you very much.
14 years ago
Dear sir/madam,

I have a situation where I find myself adding more codes into a function as the number of components/classes increases.

I am trying to collect the list of classes into a Hashtable (componentClasses) by declaring them one by one according to a configuration file.
.
.
.
if (componentName.equals("FileReader")) {
FileReader component = new FileReader(componentKey);
componentClasses.put(componentKey, component);
}
else if (componentName.equals("TextMapper")) {
TextMapper component = new TextMapper(componentKey);
componentClasses.put(componentKey, component);
}
else if (componentName.equals("InputQueue")) {
InputQueue component = new InputQueue(componentKey);
componentClasses.put(componentKey, component);
}
.
.
.

As shown above, I have put in place "FileReader", "TextMapper" and "InputQueue" declaration.
However, if I create another class, lets say "EmailWriter", I will need to add the following into the existing codes:


else if (componentName.equals("EmailWriter")) {
EmailWriter component = new EmailWriter(componentKey);
componentClasses.put(componentKey, component);
}

Is there a way I can make my code dynamically declare the necessary component?
So that I do not need to keep adding additional codes into this function?

Thank you very much.




14 years ago