US:+16362902365 & BD:+8801711573570

info@techgensolution.com

A Leading Web Development Company 

Sharing is caring!

About the reflection to obtain the information on the enumeration, javaSE jdk related API does not provide a method, the direct access can only get the annotation on the enum class, the Internet search for a long time, the Chinese did not find the solution result, it feels too pithy , then record and share as follows:
I. ( reflection + enumeration + freemarker, automatic generation of entity classes, automatic construction of table construction index (a) generated entity class, enumeration detailed solution ), in the method of automatically generating entity classes, Enumerations are used as configuration files. This enumeration class can be improved in some places. Those enumeration instances can be implemented with annotations! This configuration is more convenient, and it is also convenient to reflect the data.
The original enumeration class:

/**

* @author Khandokar Mahmud
*
*/
@EntityConfig(entityName = “Character”, tableName = “zh_character”)
public enum EntityCharacter {
account (String.class),
name (String.class, LENGTH, 12),
profession (String.class, INDEX, true),
sex (String.class),
level (String.class),
test (String.class, LENGTH, 6, INDEX, true, NULLABLE, true, DEFAULTS, “test”),
testnew (int.class, LENGTH, 6, INDEX, true, DEFAULTS, 100),
;

public EntityConfigData conf;

private EntityCharacter(Class clazz, Object…params) {
conf = new EntityConfigData(clazz, params);
}
}

Improved enumeration class:

@EntityConfig(entityName = “Character”, tableName = “zh_character”)
public enum EntityCharacter {
@FieldConfig(String.class)
account,
@FieldConfig(value = String.class, length = 12)
name,
@FieldConfig(value = String.class, index = true)
profession,
@FieldConfig(String.class)
sex,
@FieldConfig(int.class)
level,
@FieldConfig(value = long.class, index = true, nullable = true, defaults = “test”)
test,
@FieldConfig(value = String.class, index = true, length = 6)
testnew,
;

}

Attached – used custom FieldConfig annotations:
package com.test.annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Rentention;
import java.lang.annotation.RententionPolicy;
import java.lang.annotation.Target;

@Rentention(RententionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface FieldConfig {
Class value();
int length() default 0;
boolean index() default false;
boolean nullable() default false;
String defaults() default “”;
}

public static Object getFieldInfo(Object enumm, String name) throws Exception{

Annotation annotation = enumm.getClass().getField(enumName).getAnnotation(FieldConfig.class);

// 在获取想要的方法
Method method = FieldConfig.class.getMethod(name);

Object result = method.invoke(annotation);

return result;
}

Sharing is caring!

Grab the latest deals on your inbox

Your email is safe ❤️ . I hate SPAM same as you!

Techgen IT Solution provides its clients with a variety of offshore business solutions like system design, .NET technology stack including C#, ASP.NET, MVC, Web API, SQL, Entity Framework, JavaScript and related technologies, namely AngularJS, Knockout, NodeJS etc

USA OFFICE

BD OFFICE 

© 2019 Techgen IT Solution. All Rights Reserved.

shares