Java annotations are a tall feature in Java, and many open-source frameworks use the functionality of Java annotations. Like Spring,hibernate and so on.
Starting with java1.5 a few years ago, Java has added generics, annotations, and concurrency. These features are all Java-tall features. It is still widely used,
Demonstrate the classical importance of knowledge through the test of live time. The newest technology is not all very important knowledge.
Two reflection methods for annotations
Getdeclaredmethods () and getannotation (), they all belong to the Annotatedelement interface (Class,method and field classes all implement the interface)
annotation[] anns = field.getdeclaredannotations ();
annotation[] anns = field.getannotations ();
What is the difference between these two methods?
Although the returned type is the same, Getdeclaredannotations returns a specific statement of the annotation;
Field.getannotations () The Getannotations method used by the AccessibleObject of the field class's parent class ; Getannotations method returns the Getdeclaredannotations method;
The content of the Getdeclaredannotations method is the throw new Assertionerror ("All subclasses should override this method");
That is, all subclasses of the AccessibleObject class must override this method. That is, the correct way to call should be to call the Subclass field Getdeclaredannotations method.
/** * */ Packageannotation.database;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/** * @authorAdministrator **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) Public@InterfaceConstraints {BooleanPrimaryKey ()default false; BooleanAllownull ()default true; BooleanUnique ()default false;}
/** * */ Packageannotation.database;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/** * @authorAdministrator **/@Target (Elementtype.type) @Retention (retentionpolicy.runtime) Public@Interfacedbtable { PublicString name ()default"";}
/** * */ Packageannotation.database;/** * @authorAdministrator **/@DBTable (Name= "MEMBER") Public classMember {@SQLString (30) String FirstName; @SQLString (50) String LastName; @SQLInteger Integer age; @SQLString (Value= Constraints = @Constraints (PrimaryKey =true) ) String handle; Static intMemberCount; PublicString Getfirstname () {returnFirstName; } PublicString Getlastname () {returnLastName; } PublicInteger getage () {returnAge ; } PublicString gethandle () {returnhandle; } PublicString toString () {returnhandle; }}
Packageannotation.database;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/** * @authorAdministrator **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) Public@InterfaceSqlinteger {String name ()default""; Constraints Constraints ()default@Constraints;}
/** * */ Packageannotation.database;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/** * @authorAdministrator **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) Public@InterfaceSQLString {intValue ()default0; String name ()default""; Constraints Constraints ()default@Constraints;}
/** * */ Packageannotation.database;Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Field;Importjava.util.ArrayList;Importjava.util.List;/** * @authorAdministrator **/ Public classTablecreator {/** * @paramargs *@throwsclassnotfoundexception*/ Public Static voidMain (string[] args)throwsException {if(Args.length < 1) {System.out.println ("Arguments:annotated Classes"); System.exit (0); } for(String Classname:args) {Class<?> cl =Class.forName (className); DBTable dbtable= Cl.getannotation (dbtable.class); if(DBTable = =NULL) {System.out.println ("No dbtable Annotations in class" +className); Continue; } String tableName=Dbtable.name (); if(Tablename.length () < 1) {TableName=cl.getname (). toUpperCase (); } List<String> columndefs =NewArraylist<string>(); for(Field field:cl.getDeclaredFields ()) {String ColumnName=NULL; annotation[] Anns=field.getdeclaredannotations (); Field.getannotations (); if(Anns.length < 1) { Continue; } if(Anns[0]instanceofSqlinteger) {Sqlinteger sInt= (Sqlinteger) anns[0]; if(Sint.name (). Length () < 1) {ColumnName=field.getname (). toUpperCase (); } Else{columnName=Sint.name (); } columndefs.add (ColumnName+ "INT" +getconstraints (Sint.constraints ())); } if(Anns[0]instanceofSQLString) {SQLString sstring= (SQLString) anns[0]; if(Sstring.name (). Length () < 1) {ColumnName=field.getname (). toUpperCase (); } Else{columnName=Sstring.name (); } columndefs.add (ColumnName+ "VARCHAR (" +Sstring.value ()+ ")" +getconstraints (Sstring.constraints ())); }} StringBuilder CreateCommand=NewStringBuilder ("CREATE TABLE" + TableName + "("); for(String columndef:columndefs) {createcommand.append ("\ n" + columndef + ","); } String tablecreate= createcommand.substring (0, Createcommand.length ()-1) + ");"; System.out.println ("Table Creation SQL for" +ClassName+ "is: \ n" +tablecreate); } } Private Staticstring getconstraints (Constraints con) {string Constraints= ""; if(!Con.allownull ()) {Constraints+ = "Not NULL"; } if(Con.primarykey ()) {Constraints+ = "PRIMARY KEY"; } if(Con.unique ()) {Constraints+ = "UNIQUE"; } returnconstraints; }}
Java Programming Ideas-annotations Generate external example code