Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public class MainWindow {
  2.  
  3. @UsesTextChanger
  4. private JButton btn1;
  5.  
  6. @UsesTextChanger
  7. private JLabel lb1;
  8.  
  9. public void ChangeTexts() {
  10.  
  11. for (Field field: MainWindow.class.getDeclaredFields()) {
  12. field.setAccessible(true);
  13. UsesTextChanger usesTextChanger = field.getAnnotation(UsesTextChanger.class);
  14. if (usesTextChanger != null){
  15.  
  16. try {
  17. Method method = field.getType().getMethod("setText", new Class[]{String.class});
  18. method.invoke(field, "my new text");
  19.  
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. }
  26. }
  27.  
  28. java.lang.IllegalArgumentException: object is not an instance of declaring class
  29.  
  30. for (Component component: this.frame.getContentPane().getComponents()) {
  31. try {
  32. boolean componentUsesTextChangerAnnotation = true; // Is there a way to check if an annotation exists in an instanced object?
  33. if (componentUsesTextChangerAnnotation) {
  34. Method method = component.getClass().getMethod("setText", new Class[]{String.class});
  35. method.invoke(component, "my new text");
  36. }
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement