InvocationHandler.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* java.lang.reflect.InvocationHandler - dynamically executes methods in
  2. proxy instances
  3. Copyright (C) 2001 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.lang.reflect;
  33. /**
  34. * This interface defines an invocation handler. Suppose you are using
  35. * reflection, and found a method that requires that its parameter
  36. * be an object of a given interface. You want to call this method,
  37. * but have no idea what classes implement that interface. So, you can
  38. * create a {@link Proxy} instance, a convenient way to dynamically
  39. * generate a class that meets all the necessary properties of that
  40. * interface. But in order for the proxy instance to do any good, it
  41. * needs to know what to do when interface methods are invoked! So,
  42. * this interface is basically a cool wrapper that provides runtime
  43. * code generation needed by proxy instances.
  44. *
  45. * <p>While this interface was designed for use by Proxy, it will also
  46. * work on any object in general.</p>
  47. *
  48. * <p>Hints for implementing this class:</p>
  49. *
  50. * <ul>
  51. * <li>Don't forget that Object.equals, Object.hashCode, and
  52. * Object.toString will call this handler. In particular,
  53. * a naive call to proxy.equals, proxy.hashCode, or proxy.toString
  54. * will put you in an infinite loop. And remember that string
  55. * concatenation also invokes toString.</li>
  56. * <li>Obey the contract of the Method object you are handling, or
  57. * the proxy instance will be forced to throw a
  58. * {@link NullPointerException}, {@link ClassCastException},
  59. * or {@link UndeclaredThrowableException}.</li>
  60. * <li>Be prepared to wrap/unwrap primitives as necessary.</li>
  61. * <li>The Method object may be owned by a different interface than
  62. * what was actually used as the qualifying type of the method
  63. * invocation in the Java source code. This means that it might
  64. * not always be safe to throw an exception listed as belonging
  65. * to the method's throws clause.</li>
  66. * </ul>
  67. *
  68. * <p><small>For a fun time, create an InvocationHandler that handles the
  69. * methods of a proxy instance of the InvocationHandler interface!</small></p>
  70. *
  71. * @see Proxy
  72. * @see UndeclaredThrowableException
  73. *
  74. * @author Eric Blake (ebb9@email.byu.edu)
  75. * @since 1.3
  76. * @status updated to 1.4
  77. */
  78. public interface InvocationHandler
  79. {
  80. /**
  81. * When a method is invoked on a proxy instance, it is wrapped and
  82. * this method is called instead, so that you may decide at runtime
  83. * how the original method should behave.
  84. *
  85. * @param proxy the instance that the wrapped method should be
  86. * invoked on. When this method is called by a Proxy object,
  87. * `proxy' will be an instance of {@link Proxy}, and oddly enough,
  88. * <code>Proxy.getInvocationHandler(proxy)</code> will return
  89. * <code>this</code>!
  90. * @param method the reflected method to invoke on the proxy.
  91. * When this method is called by a Proxy object, 'method'
  92. * will be the reflection object owned by the declaring
  93. * class or interface, which may be a supertype of the
  94. * interfaces the proxy directly implements.
  95. * @param args the arguments passed to the original method, or
  96. * <code>null</code> if the method takes no arguments.
  97. * (But also be prepared to handle a 0-length array).
  98. * Arguments of primitive type, such as <code>boolean</code>
  99. * or <code>int</code>, are wrapped in the appropriate
  100. * class such as {@link Boolean} or {@link Integer}.
  101. * @return whatever is necessary to return from the wrapped method.
  102. * If the wrapped method is <code>void</code>, the proxy
  103. * instance will ignore it. If the wrapped method returns
  104. * a primitive, this must be the correct wrapper type whose value
  105. * is exactly assignable to the appropriate type (no widening
  106. * will be performed); a null object in this case causes a
  107. * {@link NullPointerException}. In all remaining cases, if
  108. * the returned object is not assignment compatible to the
  109. * declared type of the original method, the proxy instance
  110. * will generate a {@link ClassCastException}.
  111. * @throws Throwable this interface is listed as throwing anything,
  112. * but the implementation should only throw unchecked
  113. * exceptions and exceptions listed in the throws clause of
  114. * all methods being overridden by the proxy instance. If
  115. * something is thrown that is not compatible with the throws
  116. * clause of all overridden methods, the proxy instance will
  117. * wrap the exception in an UndeclaredThrowableException.
  118. * Note that an exception listed in the throws clause of the
  119. * `method' parameter might not be declared in additional
  120. * interfaces also implemented by the proxy object.
  121. *
  122. * @see Proxy
  123. * @see UndeclaredThrowableException
  124. */
  125. Object invoke(Object proxy, Method method, Object[] args)
  126. throws Throwable;
  127. }