首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java-Reflection-Spring如何识别参数是用户定义的对象类型还是原始类型

Java-Reflection-Spring如何识别参数是用户定义的对象类型还是原始类型
EN

Stack Overflow用户
提问于 2013-04-22 21:10:47
回答 2查看 956关注 0票数 0

我可以使用反射找到该方法的所有方法和参数,如下所示:

代码语言:javascript
复制
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();
        }

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

代码语言:javascript
复制
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;
    }

}

输出结果为:

代码语言:javascript
复制
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;参数类型:布尔型

代码语言:javascript
复制
first one is user defined array of object and the second one is Primitive type

如何通过反射来识别这一点,以及如何找到参数是对象的数组还是不是。

请澄清。

诚挚的问候

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-22 21:34:04

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

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

除了原语之外,所有的东西都是对象。

我不知道一种直接的方法,但是你可以检查如果class1.getComponentType()Integer.TYPE, Double.TYPE, Boolean.TYPE, etc.不匹配,也就是所有的原语,那么你就有了一个对象。

票数 1
EN

Stack Overflow用户

发布于 2013-04-22 22:30:14

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16147977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档