1

when i check the parameter of the method which is in a class using java reflection for java.math.BigDecimal and java.lang.String isPrimitive() return false. yes they are not primitive but i want to differnciate between user defined class and these java class

Class[] parameterTypes = method2.getParameterTypes();

for (Class class1 : parameterTypes) { // check the parameter type and put them in to a ArrayList
                                    methodParams = new MethodParams();
                                    strClassNameToFix = class1.getName();
                                    strClassname =strClassNameToFix.replaceAll("\\[L", "").replaceAll("\\;","");

                                        methodParams.setDataType(strClassname);
                                        if(class1.isArray()){
                                            methodParams.setArray(true);
                                        }
                                        if(class1.isPrimitive()){
                                            methodParams.setPrimitive(true);
                                        }
                                        tempParamsList.add(methodParams);
                            }

based on the above code i set true of false methodParams.setPrimitive(true); i have done this because there are few cases where i get user defined object , in my case com.hexgen.ro.request.CreateOrderRO

so how to set this?

also using reflection i get the class name, methods declared in it and the arguments type for the method.

But i am not able to get the argument name like if i have declared a method like the following :

 class test{
    public String testMethod(int a, String b){
    return "test";
    }

}

in the above code i am able to get the folloing

Class name : test
Method name : testMethod
Arguments Type : int and String

But i also want to get int a and String b type of the argument as well as the declared variable name

How to do this.

Please help me to get this done.

Best Regards

Java Questions
  • 7,813
  • 41
  • 118
  • 176

3 Answers3

2

There is no special flag differentiating a Java API class like BigDecimal from a user-defined class like CreateOrderRO. You will need to either examine their package names or keep track of a set of classes you want to treat differently from others.

To answer your second question, the names of method parameters are not maintained at runtime. This is reflected by the fact that a Method can only report the formal types of its parameters, not what they are called.

EDIT: It looks like discovering method parameter names at runtime is possible, but only if compiling with debug information and using something like Spring's ParameterNameDiscoverer. See this post for more details: Getting the name of a method parameter (credit to PM 77-1's comment). IMHO any serious solution that requires compiling with debug information is a serious design flaw.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1

If you compile with debug information then you can get the parameter names. You can compile with debug using -g argument

Otherwise parameters names are not retained.

For differentiating user defined class you can check the package name. You can maintain a list packages which you want to term as user defined or maintain list of packages which you do not term as user defined.

Reason being if you are using any third party library then the classes of those libraries are user defined for you or not?

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
1

You can have a method isPrimitive() (though I would like a better name, using the same name you used) that will do something like this:

boolean isPrimitive(Class class1) throws ClassNotFoundException {

    String className = class1.getName();
    if (className.equals("java.math.BigDecimal")|| className.equals("java.lang.String")) {
        return true;
    }
    return false;
}
rajesh
  • 3,247
  • 5
  • 31
  • 56