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.

MemberInitializationsAfterExplicitConstructorCalls.java 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import org.aspectj.testing.Tester;
  2. /**
  3. * PR#476:
  4. * Member initializations are run after explicit
  5. * constructor calls ("this()") when they should be run beforehand.
  6. * The following program would produce a NullPointerException because
  7. * 'member' is set to null *after* the explicit constructor sets it
  8. * to "correctValue".
  9. */
  10. public class MemberInitializationsAfterExplicitConstructorCalls {
  11. public static void main(String[] args) {
  12. // passes - no constructor call to this
  13. ThisCall thisCall = new ThisCall("foo");
  14. thisCall.go();
  15. // fails - constructor call to this
  16. thisCall = new ThisCall();
  17. thisCall.go();
  18. }
  19. static class ThisCall {
  20. String init = "INIT";
  21. String member = null;
  22. /** set init and member to input */
  23. ThisCall(String input) {
  24. this.init = input;
  25. this.member = input;
  26. }
  27. ThisCall() {
  28. this("correctValue");
  29. Tester.check(!"INIT".equals(init), "String constructor: !\"INIT\".equals(init)");
  30. Tester.check(null != member, "String constructor: null != member");
  31. // get NPE here if using member
  32. }
  33. void go() {
  34. Tester.check(!"INIT".equals(init), "instance method: !\"INIT\".equals(init)");
  35. Tester.check(null != member, "instance method: null != member");
  36. // get NPE here if using member
  37. }
  38. }
  39. }