我做的程序可以生成類,枚舉和接口的源代碼使用反射,但我有問題與枚舉生成。Java反射 - 枚舉生成
我EumTest類生成枚舉文件
private void generateEnum(Class<?> cls,PrintWriter writer) {
this.writePackage(cls, writer);
this.writeAnnotations(writer, cls.getDeclaredAnnotations());
writer.write(Modifier.toString(cls.getModifiers())+ " enum " + cls.getSimpleName());
this.writeImplementation(cls, writer);
writer.write("{");
this.writeNewLine(writer);
Object[] cons = cls.getEnumConstants();
for (int i = 0; i < cons.length; i++) {
writer.write(cons[i].toString());
if(i != cons.length - 1)
writer.write(",");
}
writer.write(";");
this.writeNewLine(writer);
this.writeFields(cls, writer);
this.writeConstructors(cls, writer);
this.writeMethods(cls,writer);
writer.write("}");
}
結果
public enum EnumTest{
a,b;
private String r;
private EnumTest(){
}
private void some(){
}
public int[] some2(int[] b){
return b;
}
}
方法是這樣的新枚舉
package metaprogramovanie.test;
public final enum EnumTest{
a,b;
private java.lang.String r;
private static final metaprogramovanie.test.EnumTest[] $VALUES;
private EnumTest(java.lang.String arg0,int arg1){
}
public static metaprogramovanie.test.EnumTest[] values(){
return null;
}
public static metaprogramovanie.test.EnumTest valueOf(java.lang.String arg0){
return null;
}
private void some(){
}
public int daco(int arg0){
return 0;
}
}
正如你可以看到有索姆錯誤。例如修改生成最終的枚舉不能是最終的,未來有更多的方法和字段,構造函數PARAMATERS ...
構造產生
private void writeConstructors(Class<?> cls, PrintWriter writer){
Constructor[] cons = cls.getDeclaredConstructors();
for (Constructor constructor : cons) {
this.writeAnnotations(writer, constructor.getDeclaredAnnotations());
writer.write(Modifier.toString(constructor.getModifiers()) + " " + cls.getSimpleName());
this.writeParameters(writer,constructor.getParameters());
writer.write("{");
this.writeNewLine(writer);
writer.write("}");
this.writeNewLine(writer);
}
}
場產生
private void writeFields(Class<?> cls, PrintWriter writer){
Field[] atr = cls.getDeclaredFields();
for (Field field : atr) {
if(field.isEnumConstant()){
System.out.println("JE");
}
else{
this.writeAnnotations(writer, field.getDeclaredAnnotations());
writer.write(Modifier.toString(field.getModifiers())+" " + field.getType().getTypeName()+ " " + field.getName());
if(Modifier.isStatic(field.getModifiers())){
try{
Object value = field.get(null);
writer.write(" = " + this.getFieldValue(field));
}catch(IllegalAccessException ex){
}
}
writer.write(";");
this.writeNewLine(writer);
}
}
this.writeNewLine(writer);
}
方法產生
private void writeMethods(Class<?> cls, PrintWriter writer){
Method[] methods = cls.getDeclaredMethods();
for (Method met : methods) {
this.writeAnnotations(writer, met.getDeclaredAnnotations());
writer.write(Modifier.toString(met.getModifiers())+" "+met.getReturnType().getTypeName() +" "+ met.getName());
this.writeParameters(writer, met.getParameters());
writer.write("{");
this.writeNewLine(writer);
if(!met.getReturnType().equals(Void.TYPE)){
this.writeMethodBody(writer,met);
}
this.writeNewLine(writer);
writer.write("}");
this.writeNewLine(writer);
}
this.writeNewLine(writer);
}
如果你有任何想法如何獲得可編譯的枚舉。
'daco'從哪裏來? – RealSkeptic
sry我複製它,並重命名在stackoverflow some2是達科但我補充說[]不知道爲什麼我會改變它 – salian