Java - Parameter annotations -
having trouble getting parameter annotations of method, below easy test demonstration, directions towards error welcome :
// annotation public @interface @ {} // class public class annotest { public void mytest(@at string myvar1, string myvar2){} } // test public class app { public static void main(string[] args) { class myclass = annotest.class; method method = myclass.getmethods()[0]; annotation[][] parameterannotations = method.getparameterannotations(); // should output 1 instead of 0 system.out.println(parameterannotations[0].length); } }
you not setting implicitly setting retention runtime, way defaults @retention (retentionpolicy.class) says represent in class file not present in vm. make work add interface: @retention (retentionpolicy.runtime) class annotatio, works again ! :d
while @ it, might want set specific @target parameters , not methods/fields/classes etc.
Comments
Post a Comment