1

I'm using this code in order to check a method parameters type and it doesn't seem to work.

Any ideas what am I not doing right?

Class<?>[] parameter_types = m.getParameterTypes();
if (parameter_types[0].equals(Integer.class)) {
   // DO SOMETHING
}

m is a method with integer type parameter:

public void m(int param);
2
  • 2
    "It doesn't work" is not a helpful problem description. Commented Jan 3, 2015 at 16:36
  • can you please post the method signature of "m" Commented Jan 3, 2015 at 16:41

2 Answers 2

4

Integer and int are different types. Integer is the reference type java.lang.Integer. int is a primitive type and has its own Class object.

if (parameter_types[0].equals(int.class)) {
Sign up to request clarification or add additional context in comments.

1 Comment

had no clue int had attributes, always assumed that primitives didn't. Learnt something new today. I tip my hat to you.
3

Try

   String parameter_types = m.getParameterClassName(1);
      if (parameter_types.equals(int.class)) {
      //DO SOMETHING
   }

For more information: click!

1 Comment

beaten to it by 3 minutes I see. You deserve the upvote too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.