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.

MethodSignatureImpl.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.runtime.reflect;
  14. import java.lang.reflect.Method;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. import org.aspectj.lang.reflect.MethodSignature;
  18. class MethodSignatureImpl extends CodeSignatureImpl implements MethodSignature {
  19. private Method method;
  20. Class<?> returnType;
  21. MethodSignatureImpl(int modifiers, String name, Class<?> declaringType, Class[] parameterTypes, String[] parameterNames,
  22. Class[] exceptionTypes, Class<?> returnType) {
  23. super(modifiers, name, declaringType, parameterTypes, parameterNames, exceptionTypes);
  24. this.returnType = returnType;
  25. }
  26. MethodSignatureImpl(String stringRep) {
  27. super(stringRep);
  28. }
  29. /* name is consistent with reflection API */
  30. public Class getReturnType() {
  31. if (returnType == null)
  32. returnType = extractType(6);
  33. return returnType;
  34. }
  35. protected String createToString(StringMaker sm) {
  36. StringBuffer buf = new StringBuffer();
  37. buf.append(sm.makeModifiersString(getModifiers()));
  38. if (sm.includeArgs)
  39. buf.append(sm.makeTypeName(getReturnType()));
  40. if (sm.includeArgs)
  41. buf.append(" ");
  42. buf.append(sm.makePrimaryTypeName(getDeclaringType(), getDeclaringTypeName()));
  43. buf.append(".");
  44. buf.append(getName());
  45. sm.addSignature(buf, getParameterTypes());
  46. sm.addThrows(buf, getExceptionTypes());
  47. return buf.toString();
  48. }
  49. /*
  50. * (non-Javadoc)
  51. *
  52. * @see org.aspectj.lang.reflect.MemberSignature#getAccessibleObject()
  53. */
  54. public Method getMethod() {
  55. if (method == null) {
  56. Class<?> dtype = getDeclaringType();
  57. try {
  58. method = dtype.getDeclaredMethod(getName(), getParameterTypes());
  59. } catch (NoSuchMethodException nsmEx) {
  60. // pr154427 - search
  61. Set<Class<?>> searched = new HashSet<>();
  62. searched.add(dtype); // avoids another getDeclaredMethod() on dtype
  63. method = search(dtype, getName(), getParameterTypes(), searched);
  64. }
  65. }
  66. return method;
  67. }
  68. /**
  69. * Hunt for a method up the hierarchy for a specified type.
  70. *
  71. * @param type the type on which to look for the method
  72. * @param name the name of the method
  73. * @param params the parameters of the method
  74. * @param searched a set of types already searched to avoid looking at anything twice
  75. * @return the method if found, or null if not found
  76. */
  77. private Method search(Class<?> type, String name, Class[] params, Set<Class<?>> searched) {
  78. if (type == null) {
  79. return null;
  80. }
  81. if (!searched.contains(type)) {
  82. searched.add(type);
  83. try {
  84. return type.getDeclaredMethod(name, params);
  85. } catch (NoSuchMethodException nsme) {
  86. // drop through and check superclass and interfaces
  87. }
  88. }
  89. Method m = search(type.getSuperclass(), name, params, searched);
  90. if (m != null) {
  91. return m;
  92. }
  93. Class[] superinterfaces = type.getInterfaces();
  94. if (superinterfaces != null) {
  95. for (Class<?> superinterface : superinterfaces) {
  96. m = search(superinterface, name, params, searched);
  97. if (m != null) {
  98. return m;
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104. }