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.adoc 2.6KB

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