我能找到的所有的方法和參數的方法需要使用反射,下面是我做的方式:的Java反射-Spring如何識別參數是用戶定義的對象或原始類型
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();
}
這是實用程序類別I:
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
如何通過反射標識該怎麼也找說法是物體或不數組。
請說明。
問候
請嘗試應用[SSCCE](http://sscce.org/)概念([like this](http://stackoverflow.com/questions/16142708/java-reflection-find-the-arguments-和最註解的前最方法#comment23065374_16142708))。你的問題在這裏並不是關於Spring的。你可以用一個簡單的問題*「如何知道一個類是否代表原始類型」來創建一個小例子來說明你的請求。這麼多片段使*真正的問題很難得到。 – sp00m 2013-04-22 14:35:24