Simulation using a simple reflection effect IOC

Package Anno; 

Import java.lang.annotation.Annotation;
 Import java.lang.reflect.Constructor;
 Import as java.lang.reflect.Field;
 Import the java.lang.reflect.Method;
 Import java.util.Arrays; 

/ ** 
 * @program: tx_annotation_demo 
 * @description: an analog implementation uses reflection IOC (inversion of control) 
 * @author : CZG 
 * @Create: 2019-09-27 22:41 
 * / 
public  class MySpring { 

    public  static  void main (String [] args) {
         // Title: the parameters: package name a class name string parameters: a target object corresponding to the annotation data (no data exists in the constructor parameter) already exists 

        Person p= (The Person) Ioc, ( "anno.Person" ); 
        System.out.println (p.toString ()); 
    } 


    public  static Object Ioc, (String ClassName) { 

        Object O = null ;
         the try { 
            Class clazz = the Class.forName ( ClassName);
              // get the object 
             O = clazz.newInstance ();
              // get constructor with no arguments 
            the constructor constructor = clazz.getConstructor (); 

            System.out.println ( "----------------------- - start the annotation data obtained --------------- " );
             // obtained above constructor with no arguments custom annotations
            Annotation = constructor.getAnnotation annotation (MyAnnotaion. Class );
             // get the annotation Class Class 
            Class aClass = annotation.getClass ();
             // a method for obtaining class annotated 
            Method, Test aClass.getMethod = ( "Test" );
             // get its data inside 
            String [] data = (String []) test.invoke (annotation); 
            System.out.println ( "annotation data:" + of Arrays.toString (data)); 

            System.out.println ( " -------------- method of constructing composite object set: set + capitalize the first letter of the name of the object properties --------------- " ); 

            // build combinations setXxxx method represented Object object
             // to obtain private property
            Field, [] = Fields clazz.getDeclaredFields ();
             // Loop through property name 
            for ( int I = 0; I <fields.length; I ++ ) { 
                Field, Field = Fields [I]; 
                String name = field.getName ();
                 // attribute combination corresponding to the set method of 
                the StringBuilder setMethod = new new the StringBuilder ( "set") the append (name.substring (0,1. ) .toUpperCase ()) 
                        .append (name.substring ( . 1 ));
                 // get property type 
                Class filedType = Field.getType ();
                 //The method of operation set is obtained and 
                Method, Method = clazz.getMethod (setMethod.toString (), filedType);
                 // needs to read the annotation string String type corresponding to the other turn out
                 // Integer Integer new new I = ( "") ;
                 // find the corresponding attribute type to be of type String constructor constructs a new object 
                Method.invoke (O, filedType.getConstructor (String. class ) .newInstance (Data [I])); 
            } 

        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 

        return O; 
    } 

}
package anno;

/**
 * @program: tx_annotation_demo
 * @description: 自定义注解测试使用
 * @author: czg
 * @create: 2019-09-27 21:54
 */
public class Person {

    //@MyAnnotaion(test = {"czg","18","男"})
    private String name;

    private Integer age;

    private String sex;

    @MyAnnotaion(test = {"czg","18","男"})
    public Person() {
    }

    public Person(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
Package Anno; 

Import Classes in java.lang.annotation *. ; 

/ ** 
 * @program: tx_annotation_demo 
 * @description: 
 * 
 * custom annotations (somewhat similar to the use of the interface) 
 * Features: annotated method must have a return value, the return value must be the following five 
 * basic String data type enumeration type enum type annotation types @ array type (internal array must be the first four) 
 * 
 * NOTE to use must also use Java annotations to provide good yuan (RMB Note: meta-annotation is not used to use, and is used to help explain the custom annotation) 
 * 
 * there are common meta-annotation: @Target description notes the statement is where to put the use of (above notes above, the above properties, methods, classes in general, the above construction method, the foregoing parameters) 
 * ElementType.FIELD (attributes above), ElementType.METHOD (above method), ElementType.CONSTRUCTOR (constructor above) may be annotated 
 * @Retention SOURCE domain what role this annotation described in current : Source file CLASS: bytecode file RUNTIME: memory to run 
 * @Inherited describe whether the current annotation will be Class objects inherit
 * @Documented describes whether the current annotation can generate a document 
 * @author: CZG 
 * @Create: 2019-09-27 20:50 
 * / 
@Target ({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR}) 
@Retention (RetentionPolicy.RUNTIME) 
public @ interface MyAnnotaion { 

    public  static  Final String NAME_TEST = "XXXX"; // general notes this is relatively small inside 

    // public abstract is generally the default 
    public  abstract String [] Test (); 
}

 

Guess you like

Origin www.cnblogs.com/czgxxwz/p/11601014.html