|
5 | 5 | * which accompanies this distribution, and is available at
|
6 | 6 | * https://www.eclipse.org/legal/epl-2.0/
|
7 | 7 | *
|
8 |
| - * This is an implementation of an early-draft specification developed under the Java |
9 |
| - * Community Process (JCP) and is made available for testing and evaluation purposes |
10 |
| - * only. The code is not compatible with any specification of the JCP. |
11 |
| - * |
12 | 8 | * SPDX-License-Identifier: EPL-2.0
|
13 | 9 | *
|
14 | 10 | * This is an implementation of an early-draft specification developed under the Java
|
@@ -2708,4 +2704,167 @@ public static void main(String... args) {
|
2708 | 2704 | "");
|
2709 | 2705 | verifyClassFile("version 24 : 68.65535", "X.class", ClassFileBytesDisassembler.SYSTEM);
|
2710 | 2706 | }
|
| 2707 | + |
| 2708 | + public void testFieldAssignment_OK() throws Exception { |
| 2709 | + runConformTest(new String[] { |
| 2710 | + "X.java", |
| 2711 | + """ |
| 2712 | + public class X { |
| 2713 | + final String s; |
| 2714 | + X(String s0) { |
| 2715 | + s = s0; |
| 2716 | + super(); |
| 2717 | + } |
| 2718 | + X() { |
| 2719 | + s = ""; |
| 2720 | + super(); |
| 2721 | + } |
| 2722 | + public static void main(String... args) { |
| 2723 | + System.out.print(new X("OK").s); |
| 2724 | + } |
| 2725 | + } |
| 2726 | + """ |
| 2727 | + }, |
| 2728 | + "OK"); |
| 2729 | + } |
| 2730 | + |
| 2731 | + public void testFieldAssignmentNotAlways_NOK() throws Exception { |
| 2732 | + runNegativeTest(new String[] { |
| 2733 | + "X.java", |
| 2734 | + """ |
| 2735 | + public class X { |
| 2736 | + final String s; |
| 2737 | + X(String s0) { |
| 2738 | + s = s0; |
| 2739 | + super(); |
| 2740 | + } |
| 2741 | + X() { |
| 2742 | + } |
| 2743 | + public static void main(String... args) { |
| 2744 | + System.out.print(new X("OK").s); |
| 2745 | + } |
| 2746 | + } |
| 2747 | + """ |
| 2748 | + }, |
| 2749 | + """ |
| 2750 | + ---------- |
| 2751 | + 1. WARNING in X.java (at line 4) |
| 2752 | + s = s0; |
| 2753 | + ^ |
| 2754 | + You are using a preview language feature that may or may not be supported in a future release |
| 2755 | + ---------- |
| 2756 | + 2. WARNING in X.java (at line 5) |
| 2757 | + super(); |
| 2758 | + ^^^^^^^^ |
| 2759 | + You are using a preview language feature that may or may not be supported in a future release |
| 2760 | + ---------- |
| 2761 | + 3. ERROR in X.java (at line 7) |
| 2762 | + X() { |
| 2763 | + ^^^ |
| 2764 | + The blank final field s may not have been initialized |
| 2765 | + ---------- |
| 2766 | + """); |
| 2767 | + } |
| 2768 | + |
| 2769 | + public void testFieldAssignmentInLambda_NOK() throws Exception { |
| 2770 | + runNegativeTest(new String[] { |
| 2771 | + "X.java", |
| 2772 | + """ |
| 2773 | + public class X { |
| 2774 | + String s; |
| 2775 | + X(String s0) { |
| 2776 | + s = s0; |
| 2777 | + super(); |
| 2778 | + } |
| 2779 | + X() { |
| 2780 | + Runnable r = () -> s = ""; |
| 2781 | + super(); |
| 2782 | + } |
| 2783 | + public static void main(String... args) { |
| 2784 | + System.out.print(new X("OK").s); |
| 2785 | + } |
| 2786 | + } |
| 2787 | + """ |
| 2788 | + }, |
| 2789 | + """ |
| 2790 | + ---------- |
| 2791 | + 1. WARNING in X.java (at line 4) |
| 2792 | + s = s0; |
| 2793 | + ^ |
| 2794 | + You are using a preview language feature that may or may not be supported in a future release |
| 2795 | + ---------- |
| 2796 | + 2. WARNING in X.java (at line 5) |
| 2797 | + super(); |
| 2798 | + ^^^^^^^^ |
| 2799 | + You are using a preview language feature that may or may not be supported in a future release |
| 2800 | + ---------- |
| 2801 | + 3. ERROR in X.java (at line 8) |
| 2802 | + Runnable r = () -> s = ""; |
| 2803 | + ^ |
| 2804 | + Cannot assign field 's' inside a lambda expression within an early construction context of class X |
| 2805 | + ---------- |
| 2806 | + 4. WARNING in X.java (at line 9) |
| 2807 | + super(); |
| 2808 | + ^^^^^^^^ |
| 2809 | + You are using a preview language feature that may or may not be supported in a future release |
| 2810 | + ---------- |
| 2811 | + """); |
| 2812 | + } |
| 2813 | + |
| 2814 | + public void testFieldAssignmentInLocal_NOK() throws Exception { |
| 2815 | + runNegativeTest(new String[] { |
| 2816 | + "X.java", |
| 2817 | + """ |
| 2818 | + public class X { |
| 2819 | + String s; |
| 2820 | + X(String s0) { |
| 2821 | + s = s0; |
| 2822 | + super(); |
| 2823 | + } |
| 2824 | + X() { |
| 2825 | + Runnable r = new Runnable() { |
| 2826 | + public void run() { s = "Anonymous"; }; |
| 2827 | + }; |
| 2828 | + class Local { |
| 2829 | + { |
| 2830 | + s = "Local"; |
| 2831 | + } |
| 2832 | + } |
| 2833 | + super(); |
| 2834 | + } |
| 2835 | + public static void main(String... args) { |
| 2836 | + System.out.print(new X("OK").s); |
| 2837 | + } |
| 2838 | + } |
| 2839 | + """ |
| 2840 | + }, |
| 2841 | + """ |
| 2842 | + ---------- |
| 2843 | + 1. WARNING in X.java (at line 4) |
| 2844 | + s = s0; |
| 2845 | + ^ |
| 2846 | + You are using a preview language feature that may or may not be supported in a future release |
| 2847 | + ---------- |
| 2848 | + 2. WARNING in X.java (at line 5) |
| 2849 | + super(); |
| 2850 | + ^^^^^^^^ |
| 2851 | + You are using a preview language feature that may or may not be supported in a future release |
| 2852 | + ---------- |
| 2853 | + 3. ERROR in X.java (at line 9) |
| 2854 | + public void run() { s = "Anonymous"; }; |
| 2855 | + ^ |
| 2856 | + Cannot assign field 's' from class 'X' in an early construction context |
| 2857 | + ---------- |
| 2858 | + 4. ERROR in X.java (at line 13) |
| 2859 | + s = "Local"; |
| 2860 | + ^ |
| 2861 | + Cannot assign field 's' from class 'X' in an early construction context |
| 2862 | + ---------- |
| 2863 | + 5. WARNING in X.java (at line 16) |
| 2864 | + super(); |
| 2865 | + ^^^^^^^^ |
| 2866 | + You are using a preview language feature that may or may not be supported in a future release |
| 2867 | + ---------- |
| 2868 | + """); |
| 2869 | + } |
2711 | 2870 | }
|
0 commit comments