ReflectionTest:由输入的类名得到类的信息
package reflection; import java.lang.reflect.*;
import java.util.*; public class ReflectionTest {
public static void main(String[] args)
{
String name;
if(args.length>0) name =args[0];
else
{
Scanner in = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.util.Date):");
name = in.next();
}
try
{
Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if (modifiers.length() > 0) System.out.print(modifiers + " ");
System.out.print("class " + name);
if (supercl != null && supercl != Object.class) System.out.print(" extends " + supercl.getName()); System.out.print("\n{\n");
printConstructors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}"); }
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
System.exit(0);
}
public static void printConstructors(Class cl)
{
Constructor[] constructors = cl.getDeclaredConstructors();
for(Constructor c:constructors)
{
String name = c.getName();
System.out.print(" ");
String modifiers = Modifier.toString(c.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");
System.out.print(name+"("); Class[] paramTypes = c.getParameterTypes();
for (int j=0; j<paramTypes.length; j++)
{
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
} public static void printMethods(Class cl)
{
Method[] methods = cl.getDeclaredMethods(); for(Method m:methods)
{
Class retType = m.getReturnType();
String name = m.getName(); System.out.print(" ");
String modifiers = Modifier.toString(m.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");//private static final都是修饰符
System.out.print(retType.getName()+" "+name + "("); Class[] paramTypes = m.getParameterTypes();//获取一个方法的所有参数
for(int j=0;j<paramTypes.length;j++)
{
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());//获取一个参数的类型
}
System.out.println(");");
}
} public static void printFields(Class cl)
{
Field[] fields = cl.getDeclaredFields(); for(Field f:fields)
{
Class type = f.getType();
String name = f.getName();
System.out.print(" ");
String modifiers = Modifier.toString(f.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");
System.out.println(type.getName()+" "+ name +";"); }
} }
ReflectionTest:由输入的类名得到类的信息的更多相关文章
- C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即 cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...
- C#反射实例应用--------获取程序集信息和通过类名创建类实例
AppDomain.CurrentDomain.GetAssemblies();获取程序集,但是获取的只是已经加载的dll,引用的获取不到. System.Reflection.Assembly.Ge ...
- C#反射 获取程序集信息和通过类名创建类实例(转载)
C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...
- [ActionScript 3.0] 如何获得实例对象的类名及类
package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.S ...
- day21——面向对象初识、结构、从类名研究类、从对象研究类、logging模块进阶版
day21 面向对象的初识 面向对象第一个优点: 对相似功能的函数,同一个业务下的函数进行归类,分类. 想要学习面向对象必须站在一个上帝的角度去分析考虑问题. 类: 具有相同属性和功能的一类事物. 对 ...
- 反射01 Class类的使用、动态加载类、类类型说明、获取类的信息
0 Java反射机制 反射(Reflection)是 Java 的高级特性之一,是框架实现的基础. 0.1 定义 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...
- -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
-XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num #instances #bytes class name ------ ...
- C# 利用反射查看类的信息
using System; using System.Collections; using System.Collections.Generic; using System.Reflection; u ...
- 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
[源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...
随机推荐
- 和重复搭建开发环境说 Bye Bye 之Vagrant
每每新同事入职,都要在自己电脑上配置一堆环境,费神费力:每每开发测试都要重新配置开发环境,手工搭建,步骤很繁琐,极易出错. 大神在时,大神搭建,大神不在,以手抚膺坐长叹.为此,VVVVVagrant横 ...
- hibernate如何配置自动生成表
hibernate自动生成表有两种方法: 1.直接写代码,通过方法来创建数据库表. 2.通过 hibernate.cfg.xml配置标签来创建数据表. 下面依次实现: 1.直接写代码,通过方法来创建数 ...
- open jdk性能与稳定性测试比较(转载)
因为oracle jdk从jdk8u201之后就不提供免费下载了,所以最近在看openjdk的分支实现,网上搜了下,有下列选择和比较(我们目前主要在跑的是open jdk,不少人推荐的zulu ope ...
- Vehicle routing with Optaplanner graph-theory
Vehicle routing with Optaplanner - Stack Overflow https://stackoverflow.com/questions/22285252/vehic ...
- Chrome与chromedriver.exe的版本对应
Chrome与chromedriver.exe的版本对应 分类专栏: pyhton3.7+selenium3 转:https://blog.csdn.net/weixin_44545954/art ...
- centos7.6下编译安装zabbix4.0.10长期支持版
一.安装数据库,这里使用的是percona-server5..24版本 配置如下 [root@zabbix4_clone:~]# cat /etc/my.cnf # Example MySQL con ...
- RestSHarp
异步扩展: public static class RestClientExtensions { private static Task<T> SelectAsync<T>(t ...
- Opencv拉普拉斯算子做图像增强
Opencv拉普拉斯算子——图像增强 #include <iostream> #include <opencv2/opencv.hpp> using namespace std ...
- 中标麒麟+Qt+mysql解决驱动无法加载的问题
问题描述:都安装了Qt,Mysql之后,发现Qt始终不能连接Mysql 1.安装Qt 2.写程序直接连接QMysql 打印QSqlDatabase: * driver not loaded ,进入/h ...
- Sublime Text 3能用支持的插件推荐
从二月份用测试版本build 3012开始用sublime text 3,虽然很多插件在sublime text 3不工作了,因为sublime text 3修复了2的一些bug.提升了性能并集成了不 ...