Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ff3843f569ec2f9ce4ab2cd74c96002c2f78164 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright (c) 2018 Jesper Steen Møller and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Jesper Steen Møller - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;

import java.io.IOException;

import org.eclipse.jdt.core.tests.util.CompilerTestSetup;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

import junit.framework.Test;

@SuppressWarnings({ "rawtypes" })
public class JEP286ReservedWordTest extends AbstractRegressionTest {

public static Class testClass() {
	return JEP286ReservedWordTest.class;
}
public void initialize(CompilerTestSetup setUp) {
	super.initialize(setUp);
}
public static Test suite() {
	return buildMinimalComplianceTestSuite(testClass(), F_1_8);
}

public JEP286ReservedWordTest(String testName){
	super(testName);
}

public void test0001_class_var_warning() throws IOException {
	String classVar =
		"	public class var { public int a; };\n";	

	String classX =
			"public class X {\n" + 
			classVar +
			"	+\n" +
			"}\n";
	String errorTail =
			"----------\n" +
			"2. ERROR in X.java (at line 3)\n" +
			"	+\n" +
			"	^\n" +
			"Syntax error on token \"+\", delete this token\n" + 
			"----------\n";

	if (Long.compare(this.complianceLevel, ClassFileConstants.JDK10) >= 0) {
		this.runNegativeTest(
				new String[] {
					"X.java",
					classX
				},
				"----------\n" +
				"1. ERROR in X.java (at line 2)\n" +
				classVar +
				"	             ^^^\n" +
				"'var' is not a valid type name\n" +
				errorTail);
	} else {
		this.runNegativeTest(
				new String[] {
					"X.java",
					classX
				},
				"----------\n" +
				"1. WARNING in X.java (at line 2)\n" +
				classVar +
				"	             ^^^\n" +
				"'var' should not be used as an type name, since it is a reserved word from source level 10 on\n" +
				errorTail);
	}
}
public void test0002_interface_var_warning() throws IOException {
	String interfaceVar =
		"	interface var { };\n";	

	String classX =
			"public class X {\n" + 
			interfaceVar +
			"	+\n" +
			"}\n";
	String errorTail =
			"----------\n" +
			"2. ERROR in X.java (at line 3)\n" +
			"	+\n" +
			"	^\n" +
			"Syntax error on token \"+\", delete this token\n" + 
			"----------\n";

	if (Long.compare(this.complianceLevel, ClassFileConstants.JDK10) >= 0) {
		this.runNegativeTest(
				new String[] {
					"X.java",
					classX
				},
				"----------\n" +
				"1. ERROR in X.java (at line 2)\n" +
				interfaceVar +
				"	          ^^^\n" +
				"'var' is not a valid type name\n" +
				errorTail);
	} else {
		this.runNegativeTest(
				new String[] {
					"X.java",
					classX
				},
				"----------\n" +
				"1. WARNING in X.java (at line 2)\n" +
				interfaceVar +
				"	          ^^^\n" +
				"'var' should not be used as an type name, since it is a reserved word from source level 10 on\n" +
				errorTail);
	}
}
public void testBug530920() throws IOException {
	String classX = "public class X<var extends Number> { }\n";
	this.runNegativeTest(
		new String[] {
			"X.java",
			classX
		},
		Long.compare(this.complianceLevel, ClassFileConstants.JDK10) >= 0 ?
			"----------\n" +
			"1. ERROR in X.java (at line 1)\n" +
			"	public class X<var extends Number> { }\n" +
			"	               ^^^\n" +
			"'var' is not allowed here\n"
		:
			"----------\n" +
			"1. WARNING in X.java (at line 1)\n" +
			"	public class X<var extends Number> { }\n" +
			"	               ^^^\n" +
			"'var' should not be used as an type name, since it is a reserved word from source level 10 on\n"
		);
}
public void testBug530920a() throws IOException {
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"	<var extends Number> var getNumber() {\n" +
			"		return null;\n" +
			"	}\n" +
			"}"	
		},
		Long.compare(this.complianceLevel, ClassFileConstants.JDK10) >= 0 ?
			"----------\n" +
			"1. ERROR in X.java (at line 2)\n" +
			"	<var extends Number> var getNumber() {\n" +
			"	 ^^^\n" +
			"'var' is not allowed here\n"
		:
			"----------\n" +
			"1. WARNING in X.java (at line 2)\n" +
			"	<var extends Number> var getNumber() {\n" +
			"	 ^^^\n" +
			"'var' should not be used as an type name, since it is a reserved word from source level 10 on\n"
		);
}
}

Back to the top