java - How to find property name by accessor method name with popular library? -
i have custom annotation, applying getters and/or setters.
not want processing. find annotated methods , wish calculate property name of method. i.e. need cut-out "get" or "set" or "is" prefix , decapitalization.
are there common library spring or apache beanutils, provide these functions me?
pfb sample code method names having custom annotation :
find methods custom annotations:
import java.lang.annotation.annotation; import java.lang.reflect.method; public class findannotations { public static void getannotations(class clazz) { (method method : clazz.getdeclaredmethods()) { (annotation annotation : method.getdeclaredannotations()) { if (annotation.annotationtype() == customannotation.class) { // processing here string name = method.getname(); system.out.println(name + " : " + name.replaceall("get|set|is", "")); } } } } public static void main(string[] args) { getannotations(childtest.class); } }
childtest uses customannotation:
public class childtest { private string childname; @customannotation public void setchildname(string childname) { this.childname = childname; } @customannotation public string getchildname() { return this.childname; } @customannotation public boolean ischildname(string childname) { return true; } }
custom annotation class:
@target(elementtype.method) @retention(retentionpolicy.runtime) public @interface customannotation { string info() default ""; }
Comments
Post a Comment