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 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...
随机推荐
- php手记之08-tp5中间件
01-创建中间件 php think make:middleware 中间件的名称 这个指令会 application/http/middleware目录下面生成一个中间件文件. 02-注册中间件三种 ...
- Tomcat学习四步走:内核、集群、参数及性能
主题简介: 内核实现原理 分布式集群 生产部署关键参数 性能监控和分析 一.内核实现原理 HTTP Web服务器与浏览器之间以HTTP协议通信,浏览器要访问服务器即向服务器发送HTTP请求报文. 如图 ...
- php 将office文件(word/excel/ppt)转化为pdf(windows和linux只要安装对应组件应该就行)
一.配置环境 (1)配置php.ini 添加:extension=php_com_dotnet.dll com.allow_dcom = true // 去掉号,改为true 重启环境 (2) 安装 ...
- 【java/Json】用Java对象构建Json语法树
本文后续:https://www.cnblogs.com/xiandedanteng/p/11973129.html 编译第一步:将文本解析成Java对象构成的语法树 第二步:将语法树输出整形好的Js ...
- cv2.fillConvexPoly()与cv2.fillPoly()填充多边形
cv2.fillConvexPoly() cv2.fillConvexPoly()函数可以用来填充凸多边形,只需要提供凸多边形的顶点即可. 我们来画一个三角形 img = np.zeros((1080 ...
- C#读取Word指定页的内容
/// <summary> /// Word按页读取内容 /// </summary> /// <param name="page">页数< ...
- springMVC Controller 参数映射
springMVC 对参数为null或参数不为null的处理 - 小浩子的博客 - CSDN博客https://blog.csdn.net/change_on/article/details/7664 ...
- B2B2C 商业模式
b2b2c_百度百科https://baike.baidu.com/item/b2b2c/876805 What is Business to Business to Consumer (B2B2C) ...
- 123457123456#0#-----com.tym.niuniuChengYu05--前拼后广--最牛成语tym
com.tym.niuniuChengYu05--前拼后广--最牛成语tym
- 报错:(未解决)Opening socket connection to server master/192.168.52.26:2181. Will not attempt to authenticate using SASL (unknown error)
报错背景: CDH集群中,将kafka和Flume整合,将kafka的数据发送给Flume消费. 启动kafka的时候正常,但是启动Flume的时候出现了报错现象. 报错现象: DH--.cdh5./ ...