mar*_*osh 5 java reflection collections
我有方法获取如下所示的bean属性列表.如何检查方法返回类型是集合(如List,Set ...).isInstance(Collection.class)不起作用.
public static List<String> getBeanProperties(String className, boolean withLists) {
ArrayList<String> a = new ArrayList();
try {
Class c = Class.forName(className);
Method methods[] = c.getMethods();
for (int i = 0; i < methods.length; i++) {
String m = methods[i].getName();
if(m.startsWith("get") && methods[i].getParameterTypes().length == 0) {
if((methods[i].getReturnType().isInstance(Collection.class)) && !withLists) {
// skip lists
} else {
String f = m.substring(3);
char ch = f.charAt(0);
char lower = Character.toLowerCase(ch);
f = lower + f.substring(1);
a.add(f);
}
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5381 次 |
| 最近记录: |