提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
输出班级信息
/* * JBoss, Home of Professional Open Source. * Copyright 2013, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This 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 software 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 software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ //package com.nowjava; import java.lang.reflect.Constructor; /*来自 时代Java公众号 - N o w J a v a . c o m*/ import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; outputClass(clazz); } public static void outputClass(Class<?> clazz) { /* *来 自 时 代 J a v a 公 众 号 - N o w J a v a . c o m */ System.out.println("****** Class " + clazz + " " + clazz.getClassLoader() + " *******"); System.out.println("\nFields:"); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { System.out.println(" --" + field.getName() + "-" + field.getType().getName() + " (" + field.getType().getClassLoader() + ")"); } System.out.println("\nConstructors"); Constructor<?>[] ctors = clazz.getDeclaredConstructors(); for (Constructor<?> ctor : ctors) { System.out.println(" --constructor ["); for (Class<?> param : ctor.getParameterTypes()) { System.out.println(" " + param.getClass().getName() + " (" + param.getClass().getClassLoader() + ")"); } System.out.println(" ]"); } System.out.println("\nMethods:"); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(" " + method.getName() + " [");