我能够使用反射找到该方法采用的所有方法和参数,以下是我的做法:
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
如何通过反射识别这一点以及如何找到参数是否为对象数组。
请说清楚。
此致