You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Person.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package javassist.tools.reflect;
  2. import java.lang.reflect.Method;
  3. import java.util.Arrays;
  4. import org.junit.Assert;
  5. /**
  6. * Work with ClassMetaobjectTest.java
  7. *
  8. * @author Brett Randall
  9. */
  10. public class Person {
  11. public String name;
  12. public static int birth = 3;
  13. public static final String defaultName = "John";
  14. public Person(String name, int birthYear) {
  15. if (name == null)
  16. this.name = defaultName;
  17. else
  18. this.name = name;
  19. Person.birth = birthYear;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public int getAge(int year) {
  25. return year - birth;
  26. }
  27. public static void main(String[] args) throws Exception {
  28. Person person = new Person("Bob", 10);
  29. Metalevel metalevel = (Metalevel) person;
  30. ClassMetaobject cmo = metalevel._getClass();
  31. // this should return the index of the original getAge method
  32. int methodIndex = cmo.getMethodIndex("getAge",
  33. new Class[] {Integer.TYPE});
  34. System.out.println(methodIndex);
  35. // and the name verified by getMethodName
  36. String methodName = cmo.getMethodName(methodIndex);
  37. System.out.println(methodName);
  38. // check the name
  39. Assert.assertEquals("Incorrect method was found",
  40. "getAge", methodName);
  41. // check the arguments
  42. Method method = cmo.getReflectiveMethods()[methodIndex];
  43. Assert.assertTrue("Method signature did not match",
  44. Arrays.equals(method.getParameterTypes(),
  45. new Class[] {Integer.TYPE}));
  46. System.out.println(method);
  47. System.out.println("OK");
  48. }
  49. }