Guest User

Untitled

a guest
May 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. String label = annotation.label();
  2.  
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.Target;
  5. import java.lang.annotation.ElementType;
  6. import java.lang.annotation.RetentionPolicy;
  7.  
  8. @Target( { ElementType.METHOD } )
  9. @Retention( RetentionPolicy.RUNTIME )
  10. public @interface Campo {
  11.  
  12. int maxLength() default 0;
  13.  
  14. boolean required() default false;
  15.  
  16. String label();
  17.  
  18. }
  19.  
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.lang.reflect.Method;
  22.  
  23. public class GeraForm {
  24.  
  25. public String incluir(Class<?> formClass) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  26. Class<?> wantedClass = formClass;
  27. Method[] classMethods = wantedClass.getDeclaredMethods();
  28. String methodName = "temp";
  29.  
  30. //I need to return a String in HTML form style at the and of the method
  31. //Im gonna construct the string piece by piece to return at the end of the method
  32. String returnString = "a";
  33.  
  34. //I get the name of the method and put it to lower case
  35. for(int i=0; i < classMethods.length; i++) {
  36. if(classMethods[i].getName().startsWith("set")) {
  37. methodName = classMethods[i].getName().substring(3, classMethods[i].getName().length()).toLowerCase();
  38. }
  39. }
  40.  
  41. Campo annotation = classMethods[0].getAnnotation(Campo.class);
  42.  
  43. //Here I get the value of every field in the annotation
  44. String label = annotation.label();
  45. int maxLength = annotation.maxLength();
  46. boolean required = annotation.required();
  47.  
  48. returnString = label+"<input type="text" name=""+methodName+""";
  49. if(maxLength != 0) {
  50. returnString = returnString +" maxlength=""+maxLength+""";
  51. }
  52. if(required == true) {
  53. returnString = returnString + " required="" + required + """;
  54. }
  55.  
  56. returnString = returnString + ">";
  57.  
  58.  
  59. return returnString;
  60. }
  61.  
  62. }
Add Comment
Please, Sign In to add comment