Java - 反射提取类
这是我的文件(已更新):
import java.lang.reflect.*;
public class ClassExtract {
public void printClassDefinition (String[] args){
try {
Class cls = Class.forName(args[0]);
Field fieldlist[]
= cls.getDeclaredFields();
for (int i
= 0; i < ((fieldlist.length + 1) - (fieldlist.length)); i++)
{
Field fld = fieldlist[i];
System.out.println("+ " + fld.getDeclaringClass().toString().replaceAll("class", ""));
System.out.println("------");
}
for (int j = 0; j < fieldlist.length; j++) {
Field fld1 = fieldlist[j];
int mod = fld1.getModifiers();
System.out.println(Modifier.toString(mod).toString().replaceAll("public", "+")
.replaceAll("private", "-").replaceAll("protected", "#") + " " +
fld1.getType() + " " + fld1.getName());
}
}
catch (Throwable e) {
System.err.println(e);
}
}
public static void main(String args[])
{
new ClassExtract().printClassDefinition(args);
}
}
我应该读取这个文件:并且
public class Complex{
private int re;
private int im;
public Complex(int re, int im){
this.re = re;
this.im = im;
}
public Complex add(Complex h){
return new Complex(this.re+h.re, this.im+h.im);
}
public Complex sub(Complex h){
return new Complex(this.re-h.re, this.im-h.im);
}
public String toString(){
if (im >= 0){
return re +" + " +im + "i";
}
else return re +" " +im + "i";
}
public static void main(String[] args){
Complex c1 = new Complex(5,1);
Complex c2 = new Complex(5,0);
System.out.printf("c1 = %s, c2 = %s\n", c1, c2);
System.out.printf("c1 + c2 = %s\n", c1.add(c2) );
}}
它应该生成类似的内容:
(+) Complex
***
(-) int re
(-) int im
***
(+) Complex(re:int, im:int)
(+) add():Complex
(+) sub():Complex
(+) toString():String
(+) main()
由于我的文件迭代,因此我在该行之后得到了“re”和“int”的打印,以及我的格式总的来说是一团糟。我可以使用所有可能的帮助。谢谢。
This is my file (UPDATED):
import java.lang.reflect.*;
public class ClassExtract {
public void printClassDefinition (String[] args){
try {
Class cls = Class.forName(args[0]);
Field fieldlist[]
= cls.getDeclaredFields();
for (int i
= 0; i < ((fieldlist.length + 1) - (fieldlist.length)); i++)
{
Field fld = fieldlist[i];
System.out.println("+ " + fld.getDeclaringClass().toString().replaceAll("class", ""));
System.out.println("------");
}
for (int j = 0; j < fieldlist.length; j++) {
Field fld1 = fieldlist[j];
int mod = fld1.getModifiers();
System.out.println(Modifier.toString(mod).toString().replaceAll("public", "+")
.replaceAll("private", "-").replaceAll("protected", "#") + " " +
fld1.getType() + " " + fld1.getName());
}
}
catch (Throwable e) {
System.err.println(e);
}
}
public static void main(String args[])
{
new ClassExtract().printClassDefinition(args);
}
}
and I am supposed to read this file:
public class Complex{
private int re;
private int im;
public Complex(int re, int im){
this.re = re;
this.im = im;
}
public Complex add(Complex h){
return new Complex(this.re+h.re, this.im+h.im);
}
public Complex sub(Complex h){
return new Complex(this.re-h.re, this.im-h.im);
}
public String toString(){
if (im >= 0){
return re +" + " +im + "i";
}
else return re +" " +im + "i";
}
public static void main(String[] args){
Complex c1 = new Complex(5,1);
Complex c2 = new Complex(5,0);
System.out.printf("c1 = %s, c2 = %s\n", c1, c2);
System.out.printf("c1 + c2 = %s\n", c1.add(c2) );
}}
and it should generate something like:
(+) Complex
***
(-) int re
(-) int im
***
(+) Complex(re:int, im:int)
(+) add():Complex
(+) sub():Complex
(+) toString():String
(+) main()
Since my file iterates through i get a print of "re" and "int" after the line for example, and my format is in general messed up. I could use all possible help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,伙计,恐怕您的代码与解决方案相差很远,向您发送正确的答案就像作弊一样......不过,我会推荐您使用以下方法。
Sorry man, I am afraid your code is miles away from the solution, to send you the correct answer would be like cheating... I would recommend you the following approach though.