Skip to content

Commit 6841230

Browse files
committed
反射对象构造函数、方法及字段
1 parent 051930c commit 6841230

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.javacore.reflection;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Method;
6+
import java.lang.reflect.Modifier;
7+
8+
/*
9+
* Copyright [2015] [Jeff Lee]
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*/
23+
24+
/**
25+
* @author Jeff Lee
26+
* @since 2015-11-9 10:45:19
27+
* 反射对象构造函数、方法及字段
28+
*/
29+
public class ReflectionTest {
30+
public final int AGE = 1;
31+
public static void main(String[] args) {
32+
Class cl = null;
33+
try {
34+
cl = Class.forName("org.javacore.reflection.ReflectionTest");
35+
System.out.println("打印析构函数:");
36+
printConstructors(cl);
37+
System.out.println("打印方法:");
38+
printMethods(cl);
39+
System.out.println("打印字段:");
40+
printFields(cl);
41+
} catch (ClassNotFoundException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
/**
47+
* 打印Class对象的析构方法
48+
* @param cl
49+
*/
50+
public static void printConstructors(Class cl){
51+
// 返回类所有的析构方法
52+
Constructor[] constructors = cl.getDeclaredConstructors();
53+
for (Constructor c : constructors){
54+
// 返回析构方法名称
55+
String name = c.getName();
56+
System.out.print(" ");
57+
// 获取Java语言的修饰符
58+
// 修饰符由 Java 虚拟机的 public、protected、private、
59+
// final、static、abstract 和 interface 对应的常量组成;
60+
String modifiers = Modifier.toString(c.getModifiers());
61+
if (modifiers.length() > 0)
62+
System.out.print(modifiers + " ");
63+
System.out.print(name + "(");
64+
65+
// 获取析构方法的参数对象列表数组
66+
Class[] paramTypes = c.getParameterTypes();
67+
for (int i = 0; i < paramTypes.length;i++){
68+
if (i > 0)
69+
System.out.print(", ");
70+
System.out.print(paramTypes[i].getName());
71+
}
72+
System.out.println(");");
73+
}
74+
}
75+
76+
/**
77+
* 打印对象所有的方法
78+
* @param cl
79+
*/
80+
public static void printMethods(Class cl){
81+
// 获取类所有方法对象数组
82+
Method[] methods = cl.getMethods();
83+
for (Method m : methods) {
84+
// 获取方法返回对象
85+
Class retType = m.getReturnType();
86+
String name = m.getName();
87+
88+
System.out.print(" ");
89+
// 获取Java语言的修饰符
90+
// 修饰符由 Java 虚拟机的 public、protected、private、
91+
// final、static、abstract 和 interface 对应的常量组成;
92+
String modifiers = Modifier.toString(cl.getModifiers());
93+
if (modifiers.length() > 0)
94+
System.out.print(modifiers + " ");
95+
System.out.print(retType.getName() +" " + name + "(");
96+
97+
// 获取方法的参数对象列表数组
98+
Class[] paramTypes = m.getParameterTypes();
99+
for (int i = 0; i < paramTypes.length;i++){
100+
if (i > 0)
101+
System.out.print(", ");
102+
System.out.print(paramTypes[i].getName());
103+
}
104+
System.out.println(");");
105+
}
106+
}
107+
108+
public static void printFields(Class clazz){
109+
// 获取字段Field对象数组
110+
Field[] fields = clazz.getFields();
111+
for (Field field : fields){
112+
// 获取字段声明类型对象
113+
Class type = field.getType();
114+
// 获取字段名称
115+
String name = field.getName();
116+
117+
System.out.print(" ");
118+
// 获取Java语言的修饰符
119+
// 修饰符由 Java 虚拟机的 public、protected、private、
120+
// final、static、abstract 和 interface 对应的常量组成;
121+
String modifiers = Modifier.toString(field.getModifiers());
122+
if (modifiers.length() > 0)
123+
System.out.print(modifiers + " ");
124+
System.out.print(type.getName() + " " + name);
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)