0

I've been going through the answers I've seen on SO and thus far I can't find any solutions that work correctly for me. Essentially I'm just using reflection to get a method, and then get all its parameter types like this:

Type[] parameters = method.getGenericParameterTypes();

From there I am iterating through the parameters to get their respective classes so I can pass the correct data. I've tried the following:

Type currentType = parameters[index];
Class<?> clazz = Class.forName(currentType.getClass().getName());
if (clazz.isAssignableFrom(Number.class)) {
     //do stuff that is number specific
     //EG drill down farther into specific subclass like int,
     //double, etc for more exact parsing
} else if (clazz.isAssignableFrom(String.class)) {
     //Do stuff that is specific to string
} else {
     //Nope, no love here.
}

But it doesn't correctly detect when it should be a Number or String and always falls into the last else statement. There must be something really simple that I'm overlooking, but for the life of me I cannot determine what it could be.

Thanks to all in advance.

Update: Here is a really simple example of a method stub I'm parsing. It isn't anything complicated.

public void methodWithInt(int integer){
    //do something
}

public void methodWithString(String string){
    //do something else
}

Update: Based on Sotirios Delimanolis answer I've made some progress.

if (currentType instanceof Class) {
    Class<?> currentClazz = (Class<?>) currentType;
    if (currentClazz.isAssignableFrom(Number.class)) {
        // This isn't getting reached for either int or Integer
    } else if (currentClazz.isAssignableFrom(String.class)) {
        // This IS working correctly for strings
    } else {
        // Both int and Integer fall here
    }
} else {
    // Are primitives supposed to be handled here? If so int isn't
    // being sent here, but is falling in the the else from the
    // condition previous to this one.
}
7
  • Can you provide an example method signature? Commented Sep 11, 2014 at 17:27
  • 1
    question 1: why are you using reflection? I ask, because it's usually a sign that you're using the wrong approach to solve the problem you're really having. Unless your doing some kind of blind loading, you generally want to stay as far away from reflection for as long as possible (it's a great tool that exists to make use of when you have no other choice). Commented Sep 11, 2014 at 17:28
  • What does currentType.getClass().getName() return? I think it isn't what you expect. I suggest you google the javadocs for this method. Commented Sep 11, 2014 at 17:30
  • Also, int.class and Integer.class are two different things. Commented Sep 11, 2014 at 17:32
  • I second @Mike'Pomax'Kamermans. Why are you using reflection? What is the original problem that you are trying to solve? Commented Sep 11, 2014 at 17:33

1 Answer 1

2

This

Type currentType = parameters[index];

already gives you the type of the parameter.

Calling

currentType.getClass()

returns a Class object for the type Type, not what you want.

Class is a Type subtype. You can check if the Type reference you have is an instanceof of Class and then perform your isAssignableFrom.

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

5 Comments

I'm not sure how exactly to go about this. if (currentType instanceof Number){ Doesn't recognize int or Integer. Additionally else if (currentType instanceof String) { Won't even compile as it give me a compiler error. Can you elaborate or point me in the direction of some documentation to help me better understand?
@BCqrstoO No, the Type object is potentially a Class, so if (currentType instanceof Class). Then cast currentType to a Class reference so that you can use it in isAssignableFrom.
I think I have it now, or at least almost. I have updated the question to reflect what I believe you are trying to convey to me. It is correctly handling String object, but doesn't seem to be catching primitive int or boxed Integers in either case. Sorry for all the pestering, but reflection is very new to me.
@BCqrstoO For one, your isAssignableFrom is backwards. The method should be invoked on the super type. So String.class.isAssignableFrom(type) (even though String cannot have subtypes) and Number.class.isAssignableFrom(..). Additionally, and I tried to hint at this in the comments, your parameter of type int would have a int.class type object. int.class is not the same as Integer.class and definitely is not assignable to Number.class.
Sorry I did actually figure out they were backwards, but I haven't updated the question to reflect that. I'll continue on my own and figure out how to handle the primitives. Thanks for your help!

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.