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.

autoboxing.xml 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <chapter id="autoboxing" xreflabel="Autoboxing and Unboxing">
  2. <title>Autoboxing and Unboxing</title>
  3. <sect1 id="boxing-inJava5">
  4. <title>Autoboxing and Unboxing in Java 5</title>
  5. <para>
  6. Java 5 (and hence AspectJ 1.5) supports automatic conversion of
  7. primitive types (int, float, double etc.) to their object equivalents
  8. (Integer, Float, Double,...) in assignments and method and constructor
  9. invocations. This conversion is know as autoboxing.
  10. </para>
  11. <para>Java 5 also supports automatic unboxing, where wrapper types
  12. are automatically converted into their primitive equivalents if
  13. needed for assignments or method or constructor invocations.</para>
  14. <para>For example:</para>
  15. <programlisting><![CDATA[
  16. int i = 0;
  17. i = new Integer(5); // auto-unboxing
  18. Integer i2 = 5; // autoboxing
  19. ]]></programlisting>
  20. </sect1>
  21. <sect1 id="autoboxing-in-aspectj5">
  22. <title>Autoboxing and Join Point matching in AspectJ 5</title>
  23. <para>Most of the pointcut designators match based on signatures, and
  24. hence are unaffected by autoboxing. For example, a call to a method</para>
  25. <programlisting><![CDATA[
  26. public void foo(Integer i);
  27. ]]></programlisting>
  28. <para>is <emphasis>not</emphasis> matched by a pointcut
  29. <literal>call(void foo(int))</literal> since the signature declares
  30. a single <literal>Integer</literal> parameter, not an <literal>int</literal>.
  31. </para>
  32. <para>The <literal>args</literal> pointcut designator is affected by
  33. autoboxing since it matches based on the runtime type of the arguments.
  34. AspectJ 5 applies autoboxing and unboxing in determining argument matching.
  35. In other words, <literal>args(Integer)</literal> will match any join
  36. point at which there is a single argument of type <literal>Integer</literal>
  37. or of type <literal>int</literal>.</para>
  38. <itemizedlist>
  39. <listitem>args(Integer) and args(int) are equivalent</listitem>
  40. <listitem>args(Float) and args(float) are equivalent</listitem>
  41. <listitem>args(Double) and args(double) are equivalent</listitem>
  42. <listitem>args(Short) and args(short) are equivalent</listitem>
  43. <listitem>args(Byte) and args(byte) are equivalent</listitem>
  44. <listitem>args(Long) and args(long) are equivalent</listitem>
  45. <listitem>args(Boolean) and args(boolean) are equivalent</listitem>
  46. </itemizedlist>
  47. <para>
  48. Autoboxing and unboxing are also applied when binding pointcut or
  49. advice parameters, for example:
  50. </para>
  51. <programlisting><![CDATA[
  52. pointcut foo(int i) : args(i);
  53. before(Integer i) : foo(i) {
  54. ...
  55. }
  56. ]]></programlisting>
  57. </sect1>
  58. <sect1 id="autoboxing-and-method-dispatch">
  59. <title>Inter-type method declarations and method dispatch</title>
  60. <para>Autoboxing, unboxing, and also varargs all affect the method
  61. dispatch algorithm used in Java 5. In AspectJ 5, the target method
  62. of a call is selected according to the following algorithm:</para>
  63. <orderedlist>
  64. <listitem>Attempt to locate a matching method or inter-type declared
  65. method without considering
  66. autoboxing, unboxing, or vararg invocations.</listitem>
  67. <listitem>If no match is found, try again considering autoboxing
  68. and unboxing.</listitem>
  69. <listitem>Finally try again considering both autoboxing, unboxing,
  70. and varargs.</listitem>
  71. </orderedlist>
  72. <para>One consequence is that a directly matching inter-type declared
  73. method will take precedence over a method declared locally in the
  74. target class but that only matches via autoboxing.</para>
  75. </sect1>
  76. </chapter>