/**
* Copyright (C) 2011 JTalks.org Team
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//package com.nowjava;
/* from 时 代 Java - nowjava.com*/
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] argv) throws Exception {
Class beanClass = String.class;
System.out.println(getAllFields(beanClass));
}
/**
* Retrieves all fields from the class, including its super classes. <br>
*
* <i>Leaves out all static fields.</i>
*
* @param beanClass class whose fields will be extracted
* @return list of extracted fields
*/
public static List<Field> getAllFields(Class<?> beanClass) {
/**
来 自 n o w j a v a . c o m - 时 代 Java
**/
List<Class<?>> allClasses = getClassHierarchy(beanClass);
List<Field> fields = new ArrayList<Field>();
for (Class<?> current : allClasses) {
for (Field field : current.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
fields.add(field);
}
}
}
return fields;
}
/**
* Creates all class hierarchy from the given class to Object,
* <b>exclusive</b>, i.e. Object won't go to the list). Interfaces won't be
* listed in the result.
*
* @param beanClass class for building its hierarchy
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。