0

Is there a way to create primitive data type variables use Java Reflection?

For example,

public Object createObj(String type, String value){
  if(type.compareTo("char") == 0)
    //return a char
  else if(type.compareTo("int") == 0)
    //return an int
  ....
}
6
  • 2
    err, return 'c'; and such? Why reflection? Commented Dec 5, 2015 at 23:59
  • The reason being that we only know the type and value at run time, I was thinking reflection might be handy here. Commented Dec 6, 2015 at 0:02
  • No, the code on the next line after if(type.compareTo("char") == 0) knows the type at compile time. It's char or actually Character when you return that wrapped as object. Commented Dec 6, 2015 at 0:04
  • Sorry but what exactly do you mean by: "create primitive data type variables use Java Reflection"? You can't create variable using reflection, but you can inspect information about class. You can also access constructors and invoke them, but to do so you will need to pass all required arguments which constructor expects. So could you edit your question and add some example of input and results you are trying to achieve? Commented Dec 6, 2015 at 0:22
  • 1
    They are not objects but Java will automatically "box" them and return a Character or Integer object instead. See docs.oracle.com/javase/tutorial/java/data/autoboxing.html you can write return 'c' and the compiler will automatically make return Character.valueOf('c') out of it. Commented Dec 6, 2015 at 0:32

1 Answer 1

1

The common idiom I see here is to use Class.forName().

public static Object makeNew(String type) throws Exception {
    Class clazz = Class.forName(type);
    return clazz.newInstance();
}

For int, char you have to use the names of their respective class-types, you can't actually make a primitive. "java.lang.Integer", "java.lang.Character", etc. respectively. You'll need to check those specially if you want to pass in "int", "char", etc.

Adding a "value" as a string is much harder. Most classes will not have a way of changing a string into an initial state. You'll definitely have to special case the primitives to provide an initial value. Overall, I think this is not a great way to approach whatever problem you are tying to solve.


So you mention in your comment about using setter methods. One problem is how do you determine which setter do you call? If you pass a parameter of "10", for a JButton, is that the setAlignmentX, setAlignmentY, or the setText method?

At this point you have to go whole hog on it.

<class>
  <name>javax.swing.JButton</name>
  <set><method>setAlignmentX</method><value>10</value></set>
</class>

Now you have the problem that some setters take other classes as parameters. And some classes are immutable (Integer and Character are), they have no setters at all, you'll have to call a ctor.

You're basically getting into serialization here (which is a very hard problem). Take a look at XmlEncoder and XmlDecoder, they do something close to what you want. https://docs.oracle.com/javase/8/docs/api/java/beans/XMLEncoder.html

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for helping. I'm trying to avoid the conventional if-statement checking here. So, basically I want a generic return type and reflection is what I came up with.
Assuming we have a class with char, int, String, long private members, and the program uses reflection to set these data members by dynamically calling their setter methods. I was thinking maybe a generic type for the setter argument would be an good idea?
If you have a class with x, y, and z and those are all type double, how do you know which parameter goes with which setter? This is a pretty large can of worms.
I apologize for the confusion. For simplicity, assume there's only one setter for each type.
You'll need to find some way of calling its ctor or some method. See my comment above. This is a hard problem. The XmlDecoder class can do this, but it's a big complicated class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.