Essays-java Annotations, Reflections, enumerations

Source: Internet
Author: User


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, Load Class
Class clazz=class.forname ("Aresoft.controller.test.DemoBean");
2. Get the corresponding constructor object according to the constructor parameter type
Constructor CSR = Clazz.getconstructor (String[].class);
String str[]={"1", "testname", "Man"};
3. Create an Entity object
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 (); Comments for each field
if (As.length >=0) {
for (Annotation A:As) {
System.out.println ("Annotation:" +a.tostring ());
}
}
}
}


Method annotations
Zhujie Zjmethod=clazz.getmethod (Commonmethod (Meiju.GET.getName (), Clazz,fieldlist.get (1)). GetName (), New class[]{} ). Getannotation (Zhujie.class);
Note System.out.println ("GetName () Method:" +zjmethod.name ());
field annotations
Zhujie Zjfield=clazz.getdeclaredfield ((Fieldlist.get (1)). GetName ()). Getannotation (Zhujie.class);
System.out.println ("Name field annotation:" +zjfield.name ());


Gets or sets whether the method is preceded by an enumeration
Method Setmethod=commonmethod (Meiju.SET.getName (), Clazz,fieldlist.get (3));
if (Setmethod.getreturntype (). toString (). Equals ("void")) {//If return type void
System.out.println ("No return type");
}
Integer i=new integer (1);
Setmethod.invoke (T1,i);
} catch (Exception e) {
E.printstacktrace ();
}
}

Get all the field including the parent class
public static list<field> GetFields (class<?> clazz) {
List<field> fieldlist=new arraylist<field> ();
bo=
Field[] Fields=clazz.getdeclaredfields ();
for (Field f:fields) {
Fieldlist.add (f);
}
Clazz=clazz.getsuperclass ();
}while (clazz! = Object.class && clazz!=null);
return fieldlist;
}

Determine if field is a basic type
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 ();
}
}



Entity class

Package aresoft.controller.test;

Import Javax.persistence.Column;

@Zhujie (name= "Jeecg_demo")
public class Demobean {
/**id*/
@Zhujie (name= "id")
Private String ID;
/** Name * *
@Zhujie (name= "name")
private String name;
/** Sex * *
Private String sex;
/** Age */
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
}

}


Annotations

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";

}


Enumeration

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;
}

}


Essays-java Annotations, Reflections, enumerations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.