0

我能够使用反射找到该方法采用的所有方法和参数,以下是我的做法:

HexgenClassUtils hexgenClassUtils = new HexgenClassUtils();
        Class cls;


        try {
            List classNames = hexgenClassUtils.findMyTypes("com.hexgen.*");
            Iterator<Class> it = classNames.iterator();
            while(it.hasNext())
            {

                Class obj = it.next(); 
                System.out.println("Methods available in : "+obj.getName());
                System.out.println("===================================");
                //if(obj.getName().equals("com.hexgen.api.facade.HexgenWebAPI")){
                    cls = Class.forName(obj.getName());
                    Method[] method = cls.getDeclaredMethods();
                    int i=1;

                    for (Method method2 : method) {
                        PreAuthorize preAuthorizeAnnotation = method2.getAnnotation(PreAuthorize.class);
                        if(preAuthorizeAnnotation !=null){
                            System.out.println(+i+":Method Name : "+method2.getName());
                            RequestMapping methodRequestMappingAnnotation = method2.getAnnotation(RequestMapping.class);
                            //RequestMethod[] methods = methodRequestMappingAnnotation.method(); // to get the request method type
                            mappingValues = methodRequestMappingAnnotation.value(); // to get the url value
                            System.out.println("URL Value : "+mappingValues[0]);
                            Class[] parameterTypes = method2.getParameterTypes();
                            for (Class class1 : parameterTypes) {
                                System.out.println("Parameter Type : "+class1.getName());
                            }
                            i++;
                        }

                    }
                //}

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

这是我使用的实用程序类:

package com.hexgen.tools;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.SystemPropertyUtils;

public class HexgenClassUtils {
    @SuppressWarnings({ "rawtypes"})
    public List<Class> findMyTypes(String basePackage) throws IOException, ClassNotFoundException
    {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

        List<Class> candidates = new ArrayList<Class>();
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                                   resolveBasePackage(basePackage) + "/" + "**/*.class";
        Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
        for (Resource resource : resources) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                if (isCandidate(metadataReader)) {
                    candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
                }
            }
        }
        return candidates;
    }
    public String resolveBasePackage(String basePackage) {
        return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public boolean isCandidate(MetadataReader metadataReader) throws ClassNotFoundException
    {
        try {
            Class c = Class.forName(metadataReader.getClassMetadata().getClassName());
            if (!c.isInterface() && c.getAnnotation(Controller.class) != null) {
                return true;
            }
        }
        catch(Throwable e){
        }
        return false;
    }

}

输出是:

Methods available in : com.hexgen.api.facade.HexgenWebAPI
===================================
1:Method Name : createRequisition
URL Value : /trade/createrequisition
Parameter Type : [Lcom.hexgen.ro.request.CreateRequisitionRO;
Parameter Type : boolean
2:Method Name : createOrder
URL Value : /trade/createorder
Parameter Type : com.hexgen.ro.request.CreateOrderRO
Parameter Type : boolean
3:Method Name : RetrieveReportFields
URL Value : /reports/fields
Parameter Type : java.math.BigDecimal
4:Method Name : generateURL
URL Value : /reports/generateurl
Parameter Type : com.hexgen.ro.request.GenerateURLRO

例如 :

参数类型:[Lcom.hexgen.ro.request.CreateRequisitionRO; 参数类型:布尔值

first one is user defined array of object and the second one is Primitive type

如何通过反射识别这一点以及如何找到参数是否为对象数组。

请说清楚。

此致

4

2 回答 2

1

您可以使用isArray()检查参数类型是否为数组。

在您的代码中,它将是class1.isArray()

除了原语,一切都是对象。

我不知道直截了当的方法,但您可以检查是否与所有原语class1.getComponentType()不匹配,那么您有一个对象。Integer.TYPE, Double.TYPE, Boolean.TYPE, etc.

于 2013-04-22T13:34:03.710 回答
0

Class#isPrimitive()一定是你要找的。

于 2013-04-22T14:30:14.190 回答