package aresoft.controller.test;
import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class Demo3 { @SuppressWarnings("unchecked") public static <T> void test(){ try { //1,加载类 Class clazz=Class.forName("aresoft.controller.test.DemoBean"); //2、根据构造器参数类型获取相应的构造器对象 Constructor csr = clazz.getConstructor(String[].class); String str[]={"1","testname","man"}; //3、创建实体对象 T t1 = (T)csr.newInstance((Object)str); List<Field> fieldList=getFields(clazz); int flag=0; Object obj=null; for(Field f:fieldList){ if(isClassType(f)){ flag=flag+1; Method method=commonMethod(Meiju.GET.getName(),clazz,f);// System.out.println(method.getReturnType()); if(!method.getReturnType().toString().equals("void")){ obj=method.invoke(t1,new Object[]{}); } System.out.println(obj.toString()); if(flag>2){ break; } Annotation[] as=f.getAnnotations(); //每个Field 的注释 if(as.length >=0){ for(Annotation a:as){ // System.out.println("annotation:"+a.toString()); } } } } //方法注解 Zhujie zjmethod=clazz.getMethod(commonMethod(Meiju.GET.getName(),clazz,fieldList.get(1)).getName(),new Class[]{}).getAnnotation(Zhujie.class); System.out.println("getName()方法的注解:"+zjmethod.name()); //字段注解 Zhujie zjfield=clazz.getDeclaredField((fieldList.get(1)).getName()).getAnnotation(Zhujie.class); System.out.println("name字段的注解:"+zjfield.name()); //利用枚举获得方法的前面是get还是set Method setmethod=commonMethod(Meiju.SET.getName(),clazz,fieldList.get(3)); if(setmethod.getReturnType().toString().equals("void")){ //如果返回类型 void System.out.println("无返回类型"); } Integer i=new Integer(1); setmethod.invoke(t1,i); } catch (Exception e) { e.printStackTrace(); } } //得到所有的Field包括父类的 public static List<Field> getFields(Class<?> clazz){ List<Field> fieldList=new ArrayList<Field>(); do{ Field[] fields=clazz.getDeclaredFields(); for(Field f:fields){ fieldList.add(f); } clazz=clazz.getSuperclass(); }while(clazz != Object.class && clazz!=null); return fieldList; } //判断Field是否是基本类型 public static Boolean isClassType(Field field){ Class<?> type=field.getType(); Boolean isBaseClass=false; if(type.isPrimitive() || type.getPackage() !=null || type.getPackage().getName().equals("java.lang") || type.getPackage().getName().equals("java.math") || type.getPackage().getName().equals("java.math")){ isBaseClass=true; }else if(type.isArray()){ isBaseClass=false; } return isBaseClass; } //Method get public static Method getMethod(Class<?> clazz,Field field){ StringBuffer sb =new StringBuffer(); sb.append(Meiju.GET.getName()); Method method=null; try { String firstChar=field.getName().substring(0, 1).toUpperCase(); sb.append(firstChar); sb.append(field.getName().substring(1, field.getName().length())); method=clazz.getDeclaredMethod(sb.toString(),new Class[]{}); } catch (Exception e) { e.printStackTrace(); } return method; } //Method set public static Method setMethod(Class<?> clazz,Field field){ StringBuffer sb =new StringBuffer(); sb.append(Meiju.SET.getName()); Method method=null; try { String firstChar=field.getName().substring(0, 1).toUpperCase(); sb.append(firstChar); sb.append(field.getName().substring(1, field.getName().length())); method=clazz.getDeclaredMethod(sb.toString(),field.getType()); } catch (Exception e) { e.printStackTrace(); } return method; } //Method common public static Method commonMethod(String gsmethod,Class<?> clazz,Field field){ StringBuffer sb =new StringBuffer(); sb.append(gsmethod);//get or set Method method=null; try { String firstChar=field.getName().substring(0, 1).toUpperCase(); sb.append(firstChar); sb.append(field.getName().substring(1, field.getName().length())); if(gsmethod.equals(Meiju.GET.getName())){ method=clazz.getDeclaredMethod(sb.toString(),new Class[]{}); }else if(gsmethod.equals(Meiju.SET.getName())){ method=clazz.getDeclaredMethod(sb.toString(),field.getType()); } } catch (Exception e) { e.printStackTrace(); } return method; } public static void main(String[] args) { test(); }}//实体类
package aresoft.controller.test;
import javax.persistence.Column;@Zhujie(name="jeecg_demo")public class DemoBean { /**id*/ @Zhujie(name="id") private String id; /**姓名*/ @Zhujie(name="name") private String name; /**性别*/ private String sex; /**年龄*/ private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } @Zhujie(name="jeecg_demo_method") public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public DemoBean(String[] string) { super(); this.id = string[0]; this.name = string[1]; this.sex = string[2]; } public DemoBean() { super(); // TODO Auto-generated constructor stub } }//注解
package aresoft.controller.test;
import static java.lang.annotation.ElementType.CONSTRUCTOR;import static java.lang.annotation.ElementType.FIELD;import static java.lang.annotation.ElementType.LOCAL_VARIABLE;import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.ElementType.PARAMETER;import static java.lang.annotation.ElementType.TYPE;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface Zhujie { String name() default "test"; }//枚举
package aresoft.controller.test;
public enum Meiju { GET("get"),SET("set"); private String name; private Meiju(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }