java reflection mechanism

  • 2020-06-03 06:23:44
  • OfStack

Guidance of this paper:

By reflection mechanism

Gets basic information about the class Gets the annotation information for the class Get generic information

package reflection;
@AnnotationUserTable("datebaseExample")
public class User {
 @AnnotationUserField(uName="name",type="varchar",length=10)
 private String name;
 @AnnotationUserField(uName="age",type="int",length=3)
 private int age;
 @AnnotationUserField(uName="sex",type="char",length=2)
 private String sex;
 public User() {
  super();
 }
 public User(String name, int age, String sex) {
  super();
  this.name = name;
  this.age = age;
  this.sex = sex;
 }
 public String getName() {
  return name;
 }
 public void setName() {
  this.name = "test";
 }
 public int getAge() {
  return age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
}

bean:User

package reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) 
public @interface AnnotationUserTable {
 String value();
}

 Custom annotations: Class annotations 

package reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationUserField {
 String uName();
 String type();
 int length(); 
}

 Custom annotations: Attribute annotations 

package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo01 {
 static Class<?> c = null;
 public static void main(String[] args) {
   try {
    c = Class.forName("reflection.User");
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  test();// Gets information about a class's properties, methods, and so on 
 }
 static void test(){
  try {
   //  Gets the name of the class 
   System.out.println(" Gets the name of the class ");
   System.out.println("getName():" + c.getName());//  To get the package name + The name of the class 
   System.out.println("getSimpleName():" + c.getSimpleName());//  Get the name of the class 
   System.out.println("getCanonicalName():" + c.getCanonicalName());//  Get the name of the class 
   System.out.println("*******************************");
   //  Get attribute information 
   System.out.println(" Get attribute information ");
   Field[] fields = c.getDeclaredFields();
   // Field[] fields = c.getFields();  Can only get public Modifies attribute information 
   for (Field f : fields) {
    String fName = f.getName();
    System.out.println(c.getDeclaredField(fName));
   }
   System.out.println("*******************************");
   //  Get method information 
   System.out.println(" Get method information ");
   Method[] methods = c.getDeclaredMethods();
   for (Method m : methods) {
    // String mName = m.getName();
    System.out.println(m.getName() + "-->" + m);
   }
   System.out.println(" Get the corresponding by name alone getName methods :" + c.getDeclaredMethod("getName"));
   System.out.println(" Get the corresponding by name alone setSex methods :" + c.getDeclaredMethod("setSex", String.class));//  Method has arguments and must pass the parameter type 
   System.out.println("*******************************");
   //  Gets constructor information 
   System.out.println(" Gets constructor information ");
   Constructor<?>[] constructor = c.getConstructors();
   for (Constructor<?> cons : constructor) {
    System.out.println(cons);
   }
  } catch (NoSuchFieldException | SecurityException e) {
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   e.printStackTrace();
  }
 }
}

main1 : Gets basic information about a class through a reflection mechanism 

output:


 Gets the name of the class 
getName():reflection.User
getSimpleName():User
getCanonicalName():reflection.User
*******************************
 Get attribute information 
private java.lang.String reflection.User.name
private int reflection.User.age
private java.lang.String reflection.User.sex
*******************************
 Get method information 
getName-->public java.lang.String reflection.User.getName()
setName-->public void reflection.User.setName()
setSex-->public void reflection.User.setSex(java.lang.String)
getSex-->public java.lang.String reflection.User.getSex()
getAge-->public int reflection.User.getAge()
 Get the corresponding by name alone getName methods :public java.lang.String reflection.User.getName()
 Get the corresponding by name alone setSex methods :public void reflection.User.setSex(java.lang.String)
*******************************
 Gets constructor information 
public reflection.User()
public reflection.User(java.lang.String,int,java.lang.String)

View Console

In the following example, annotation information of a class is obtained through reflection mechanism.


package reflection;
import java.lang.reflect.Field;
/**
 *  Gets information about a class's properties, methods, and so on 
 * 1. Get the element object (such as the attribute) (Note: read the class annotation, which seems to be less 1 Step) 
 * 2. Gets the annotation object of the specified type of the element object 
 * 3. Read the corresponding value of the annotation object 
 */
public class Test02 {
 static Class<?> c = null;
 public static void main(String[] args) {
   try {
    c = Class.forName("reflection.User");
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  test();
 }
 static void test(){
  try {
   //  Gets the specified annotation for the class 
   System.out.println("*********** The specified annotation of the class **************");
   AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class);
   System.out.println(table.value());
   //  Gets the specified annotation for the property 
   System.out.println("*********** Attribute *************");
   Field field = c.getDeclaredField("name");
   AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class);
   System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length());
   //  Spell out the table name and field information obtained DDL Statement, and then pass JDBC Connect to database query 
  } catch (NoSuchFieldException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  }
 }
}

output:


*********** The specified annotation of the class **************
datebaseExample
*********** Attribute *************
name varchar 10

In the following example, generic information is obtained through reflection mechanisms


package reflection;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
 *  Generics are obtained through reflection mechanisms 
 * @author Administrator
 *
 */
public class Test03 { 
 public static void main(String[] args) {
  Class<?> c = Test03.class;
  try {
   System.out.println("******* Gets the type of the parameter value **********");
   Method m1 = c.getDeclaredMethod("method01", Map.class,List.class);
   Type[] types = m1.getGenericParameterTypes();
   for(Type t:types){
    System.out.println(t.getTypeName());
    System.out.println(t.toString());    
   }
   System.out.println("******* Gets the type of the return value **********");
   Method m2 = c.getDeclaredMethod("method02");
   Type ret = m2.getGenericReturnType();
   System.out.println(ret.getTypeName());
   System.out.println(ret.toString());
  } catch (NoSuchMethodException | SecurityException e) {
   e.printStackTrace();
  }
 }
 public void method01(Map<String,String> args1,List<Integer> args2){
 }
 public Map<String,String> method02(){
  return null;
 }
}

 Get generic information through reflection mechanisms 

output:


java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.Map<java.lang.String, java.lang.String>
java.util.List<java.lang.Integer>
java.util.List<java.lang.Integer>

View Console

Related articles: