1

I want to invoke a method by reflection in java.

I have on my hand the Method instance of the method I want to invoke (so I can get the types of its parameters), in addition, I have the values of these parameters as Strings.

I have an assumption that all the parameters MUST be primitives.

for example, if I want to invoke the following method:

public static double calc(int a, double b, String op){...}

I have the parameter as a String Array:

String[]: {"25", "34.45", "add"}

So, how can I convert this String array to array that contains (int, double, string)? I know that I can go over all the primitive types and try to parse the value in each type... Is there an easier way? something like "generic parse" method.

4
  • 2
    Generic parse ! Your requirement is not generic at all , parse each element as different data type is not generic ! Commented Jul 18, 2013 at 9:56
  • you are right, I mentioned the reflection just to tell the motivation of what I want to do. Commented Jul 18, 2013 at 10:01
  • There is no such thing, at least standard implementation. But you can easily make your own: knowing method, find out parameter types, and in a loop, parse string representation according to the required parameter type. Commented Jul 18, 2013 at 10:02
  • There is no such thing as 'casting String to [a] primitive type'. You have to convert. Commented Jul 19, 2013 at 0:29

3 Answers 3

3

You shouldn't go over all the primitive types and try to parse the value in each type.

You should get the type of each argument from the Method instance, and based on the type of the argument, call the appropriate method to transform the String into the required type: Double.valueOf() for a double (Double.TYPE), Integer.valueOf() for an int (Integer.TYPE), etc.

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

4 Comments

but here is the point, I don't have the types explicitly, I have them as a Type instances (which I get from method.getParameterTypes()), i.e. I don't know that the first one is int, the second is double and the last one is String
if (type.equals(Integer.TYPE)) { arg = Integer.valueOf(s); } else if (type.equals(Double.TYPE)) { arg = Double.valueOf(s); } ...
so I need to go over all the primitive types and do this... there is no shorter way?
I don't know of any other way. There are only 8 primitive types. Shouldn't be too hard.
2

You can use ConvertUtils from commons-beanutils, see [convert method](https://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/ConvertUtils.html#convert(java.lang.String, java.lang.Class)). You need to implement the loop over the array of types however...

Comments

1

First your question IMHO has nothing to do with reflection. Primitive types cannot be instantiated using reflection. Reflection is for objects only.

how can I convert this String array to array that contains (int, double, string)?

You can't. An array in Java cannot contain different data types. A workaround would be to use List<Object>. The problem is that this will require casts later to retrieve the elements (with the possibility of getting a ClassCastException).

Is there an easier way? something like "generic parse" method.

I don't know about anything like this in the standard library. And I doubt there's any because what should "25" parsed to? int? long? float? BigInteger? This is up to developer, so you have to write this yourself.

2 Comments

I didn't mean to convert the array itself, but to iterate its elements. regarding your second note, I mentioned that I have the type, i.e. I'm looking for a method that takes a String ("25") and Class<?> object that I want to convert to (int or Integer in this case) and it will return an Object (new Integer(25))
Oh I didn't see you have the Class for each parameter. My bad. Still your question has some confusing statements, like "the parameters MUST be primitives" but String is not a primitive. Also what I mean is that you can't get an array of different types but you can use a List.

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.