1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.str;
public class ZiFuChuan {
public static String ss = "全局变量!!!!!!" ;
String s2 = null ;
public int aa = 1 ;
int aa2;
double dou = 1.1 ;
Double d = new Double( 1.1 );
public static void main(String[] args) {}
public ZiFuChuan(){}
public ZiFuChuan(String st){}
public ZiFuChuan( int orgInt,String orgString){}
public static void tmpArr( int a[],String s,StringBuffer s2){}
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
package com.fanshe;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import com.str.ZiFuChuan;
public class GetClass {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
// TODO Auto-generated method stub
Class classS = ZiFuChuan. class ;
//获取包
Package classPackage = classS.getPackage();
System.out.println( "package " +classPackage.getName());
//获取类的修饰符
int mod = classS.getModifiers();
String classModifier = Modifier.toString(mod);
System.out.println(classModifier + " class ZiFuChuan {" );
//调用全局变量方法
getFieldContent(classS);
//调用构造方法
getConstructorContent(classS);
//调用其他方法
getMethodContent(classS);
System.out.println( "}" );
}
/**
* 获取所有全局变量
* @param classS
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
public static void getFieldContent(Class classS) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
String typeName = "" ;
Field[] field = classS.getDeclaredFields();
for (Field fi : field){
typeName = fi.getType().getName();
String xsfTmp = Modifier.toString(fi.getModifiers()) ;
//全局变量有修饰符的在变量前加修饰符并且修饰符和数据类型之间用空格分隔,否则不需要加空格
String xiushifu = xsfTmp.length() == 0 ? "" : xsfTmp + " " ;
String value = getValue(typeName,fi,classS);
if ( "null" .equals(value)||value == null || "null;" .equals(value)){
System.out.println( " " +xiushifu+typeName+ " " +fi.getName()+ "; " );
} else {
System.out.println( " " +xiushifu+typeName+ " " +fi.getName()+ " = " + getValue(typeName,fi,classS));
}
}
}
public static void getConstructorContent(Class classS){
Constructor[] con = classS.getConstructors();
for (Constructor c : con){
int mod = c.getModifiers();
String ConstructorModifier = Modifier.toString(mod);
String constructorParameter = getConstructorParameter(c);
System.out.println( " " +ConstructorModifier+ " " + c.getName() + "(" +constructorParameter+ "){" );
System.out.println( " }" );
}
}
/**
* 获取构造方法中的参数
* @param c
* @return
*/
public static String getConstructorParameter(Constructor c){
String qxTemp = "" ;
String qx = "" ;
int con = 0 ;
Class[] parameterTypeArr = c.getParameterTypes();
//获取构造方法中的参数
for (Class clas : parameterTypeArr){
qxTemp += clas.getTypeName() + " org" +con+ "," ;
con++;
}
int qxTempLength = qxTemp.length();
//去掉空参的构造方法
if (qxTempLength > 0 ){
qx = qxTemp.substring( 0 , qxTempLength- 1 );
}
return qx;
}
/**
* 获取除构造方法外其他的方法的逻辑
* @param classS
*/
public static void getMethodContent(Class classS){
Method[] method = classS.getDeclaredMethods();
for (Method m : method){
int mod = m.getModifiers();
String methodModifier = Modifier.toString(mod);
//获取方法返回值类型
Type type = m.getGenericReturnType();
String methodParameter = getMethodParameter(m);
System.out.println( " " +methodModifier + " " + type.getTypeName() + " " + m.getName() + "(" +methodParameter+ "){" );
System.out.println( " }" );
}
}
/**
* 获取其他方法的参数
* @param m
* @return
*/
public static String getMethodParameter(Method m){
String qxTemp = "" ;
String qx = "" ;
int con = 0 ;
Class[] parameterTypeArr = m.getParameterTypes();
//获取构造方法中的参数
for (Class clas : parameterTypeArr){
qxTemp += clas.getTypeName()+ " org" +con+ "," ;
con++;
}
int qxTempLength = qxTemp.length();
//去掉空参的构造方法
if (qxTempLength > 0 ){
qx = qxTemp.substring( 0 , qxTempLength- 1 );
}
return qx;
}
/**
* 全局变量初始化值
* @param typeName
* @param fi
* @param classS
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static String getValue(String typeName,Field fi,Class classS) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
String value = "" ;
Object obj = classS.newInstance();
fi.setAccessible( true );
String[] types = { "java.lang.Integer" ,
"java.lang.Double" ,
"java.lang.Float" ,
"java.lang.Long" ,
"java.lang.Short" ,
"java.lang.Byte" ,
"java.lang.Boolean" ,
"java.lang.Character" ,
"int" , "double" , "long" , "short" , "byte" , "boolean" , "char" , "float" };
for (String str : types) {
if (fi.getType().getName().equals( "java.lang.String" )){
Object fiObj = fi.get(obj);
//判断变量是否初始化
if (fiObj != null ){
//String 类型参数需要用双引号扩上
value = "\"" +fiObj.toString()+ "\";" ;
} else {
value = null ;
}
} else if (fi.getType().getName().equals(str)){
value = fi.get(obj).toString()+ ";" ;
}
}
return value;
}
|