package com.wgtgt.fanshe; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; public class FanShe { public static final String ONE_EMPTY="\t"; public static final String TWO_EMPTY="\t\t"; /** * @param args 程序入口, */ public static void main(String[] args) { // find("com.wgtgt.fanshe.FanShe"); find("com.wgtgt.bean.Book"); } public static void find(String className){ StringBuilder begin=new StringBuilder(); begin.append("package test;"); begin.append("\n"); begin.append("public class "); begin.append("Test"); begin.append("{"); begin.append("\n"); begin.append(" public static void main(String[] args){"); //begin.append("\n"); System.out.println(begin); Class clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Method[]methods=clazz.getMethods(); for (Method method : methods) { // System.out.println(method.getName()); // System.out.println(method.getDeclaringClass()); // System.out.println(method.getGenericExceptionTypes()); //// Type[]types=method.getGenericParameterTypes(); //// for (Type type : types) { //// System.out.println("type="+type); //// } // System.out.println(method.getGenericReturnType()); // System.out.println(method.getModifiers()); // System.out.println(method.toGenericString()); //break; System.out.println(TWO_EMPTY+"//=================="); // String className2=method.getDeclaringClass().getSimpleName(); String className3=className.substring(className.lastIndexOf(".")+1); String methodName=method.getName(); String str=null; String resultYuanShi=method.getReturnType().getName(); String result=null; String returnName=format(method.getName()); String sum=null; String printStr=null; boolean isVoid=false; if("void".equals(resultYuanShi)){ result=""; isVoid=true; }else{ result=cast2(resultYuanShi)+" "+returnName+"="; printStr=TWO_EMPTY+"System.out.println(\""+returnName+"=\"+"+returnName+");"; } if(isStaticMethod(method)){ str=className3+"."+methodName+"("+paramList(method)+");"; }else{ str=getFirstLittle(className3)+"."+methodName+"("+paramList(method)+");"; } if(isVoid){ sum=TWO_EMPTY+result+str; }else{ sum=TWO_EMPTY+result+str+"\n"+printStr; } System.out.println(sum); } String end="\t}\n}"; System.out.println(end); } private static String format(String name) { if(name.startsWith("get")){ return getFirstLittle(name.substring(3)); } return name; } private static String paramList(Method method) { StringBuilder sb=new StringBuilder(); Type[]types=method.getGenericParameterTypes(); for (Type type : types) { sb.append(cast(type)); sb.append(","); } if(types.length>0){ sb.deleteCharAt(sb.length()-1); } return sb.toString(); } private static String cast(Type type) { String typeName=type.toString(); if("int".equals(typeName)){ return "123"; }else if("long".equals(typeName)){ return "123"; }else if("byte".equals(typeName)){ return "123"; }else if("double".equals(typeName)){ return "123.25"; }else if("boolean".equals(typeName)){ return "false"; }else if("char".equals(typeName)){ return "'a'"; }else if("short".equals(typeName)){ return "123"; }else if("float".equals(typeName)){ return "123.25"; }else if("class java.lang.String".equals(typeName)){ return "\"abc\""; }else if("class [Ljava.lang.String;".equals(typeName)){ return "new String[]{\"abc\",\"eee\"}"; }else if("class java.util.Date".equals(typeName)){ return "new java.util.Date();"; }else if("interface java.util.List".equals(typeName)){ return "list"; }else if("class java.util.ArrayList".equals(typeName)){ return "list"; }else if("interface java.util.Map".equals(typeName)){ return "map"; }else if("class java.util.HashMap".equals(typeName)){ return "map"; }else if("".equals(typeName)){ return ""; }else if("class java.lang.Object".equals(typeName)){ return "\"abc\""; }else{ return "null"; //System.err.print(typeName); } //return null; } private static String cast2(String typeName) { //String typeName=type.toString(); if("void".equals(typeName)){ return ""; }else if("int".equals(typeName)){ return "int"; }else if("long".equals(typeName)){ return "long"; }else if("byte".equals(typeName)){ return "byte"; }else if("double".equals(typeName)){ return "double"; }else if("boolean".equals(typeName)){ return "boolean"; }else if("char".equals(typeName)){ return "char"; }else if("short".equals(typeName)){ return "short"; }else if("float".equals(typeName)){ return "float"; }else if("java.lang.String".equals(typeName)){ return "String"; }else if("[Ljava.lang.String;".equals(typeName)){ return "String[]"; }else if("class java.util.Date".equals(typeName)){ return "java.util.Date"; }else if("interface java.util.List".equals(typeName)){ return "list"; }else if("class java.util.ArrayList".equals(typeName)){ return "ArrayList"; }else if("interface java.util.Map".equals(typeName)){ return "map"; }else if("class java.util.HashMap".equals(typeName)){ return "HashMap"; }else if("".equals(typeName)){ return ""; }else if("java.lang.Object".equals(typeName)){ return "Object"; }else{ return "Object"; //System.err.print(typeName); } //return null; } private static boolean isStaticMethod(Method method) { return Modifier.isStatic(method.getModifiers()); } private static String getFirstLittle(String str) { return str.substring(0,1).toLowerCase()+str.substring(1); } }