blob: 633fd9a406c24d8caeef3bd5ed6cffdf98fb9274 [file] [log] [blame]
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +00001/*******************************************************************************
Stephan Herrmannf59da282020-04-10 18:10:58 +02002 * Copyright (c) 2000, 2020 IBM Corporation and others.
Stephan Herrmanna441c702018-10-14 17:51:04 +02003 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +00006 * which accompanies this distribution, and is available at
Stephan Herrmanna441c702018-10-14 17:51:04 +02007 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +000010 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14package org.eclipse.jdt.core.tests.dom;
15
16import java.io.IOException;
Stephan Herrmann63010de2013-05-01 20:08:26 +020017import java.util.Iterator;
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +000018import java.util.List;
19import java.util.Map;
20
21import org.eclipse.core.runtime.IPath;
22import org.eclipse.core.runtime.Path;
23import org.eclipse.jdt.core.IClassFile;
24import org.eclipse.jdt.core.ICompilationUnit;
25import org.eclipse.jdt.core.IJavaProject;
26import org.eclipse.jdt.core.JavaCore;
27import org.eclipse.jdt.core.JavaModelException;
28import org.eclipse.jdt.core.compiler.IProblem;
29import org.eclipse.jdt.core.dom.*;
30import org.eclipse.jdt.core.tests.util.Util;
31
Stephan Herrmann5d44b3a2015-03-17 18:08:14 +010032@SuppressWarnings("rawtypes")
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +000033public abstract class ConverterTestSetup extends AbstractASTTests {
Stephan Herrmannb9e10522012-03-15 21:24:23 +010034 /**
Stephan Herrmann307ebe32013-05-02 02:46:05 +020035 * Internal synonym for deprecated constant AST.JSL3
Stephan Herrmannb9e10522012-03-15 21:24:23 +010036 * to alleviate deprecation warnings.
37 * @deprecated
38 */
39 /*package*/ static final int JLS3_INTERNAL = AST.JLS3;
Stephan Herrmann307ebe32013-05-02 02:46:05 +020040
41 /**
42 * Internal synonym for deprecated constant AST.JSL4
43 * to alleviate deprecation warnings.
44 * @deprecated
45 */
46 /*package*/ static final int JLS4_INTERNAL = AST.JLS4;
47
Stephan Herrmann9688da62017-10-14 22:09:57 +020048 /**
49 * Internal synonym for deprecated constant AST.JSL8
50 * to alleviate deprecation warnings.
51 * @deprecated
52 */
53 /*package*/ static final int JLS8_INTERNAL = AST.JLS8;
54
Stephan Herrmann307ebe32013-05-02 02:46:05 +020055 static int getJLS3() {
56 return JLS3_INTERNAL;
57 }
58
59 static int getJLS4() {
60 return JLS4_INTERNAL;
61 }
Stephan Herrmann9688da62017-10-14 22:09:57 +020062
63 static int getJLS8() {
64 return JLS8_INTERNAL;
Stephan Herrmannc4606e82020-04-11 14:28:09 +020065 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +000066 protected AST ast;
Stephan Herrmann5ab17b32013-04-04 22:06:05 +020067 public static List TEST_SUITES = null;
68 public static boolean PROJECT_SETUP = false;
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +000069
70 protected ConverterTestSetup(String name) {
71 super(name);
72 }
73
74 protected IPath getConverterJCLPath() {
75 return getConverterJCLPath(""); //$NON-NLS-1$
76 }
77
78 protected IPath getConverterJCLPath(String compliance) {
79 return new Path(getExternalPath() + "converterJclMin" + compliance + ".jar"); //$NON-NLS-1$
80 }
81
82 protected IPath getConverterJCLSourcePath() {
83 return getConverterJCLSourcePath(""); //$NON-NLS-1$
84 }
85
86 protected IPath getConverterJCLSourcePath(String compliance) {
87 return new Path(getExternalPath() + "converterJclMin" + compliance + "src.zip"); //$NON-NLS-1$
88 }
89
90 protected IPath getConverterJCLRootSourcePath() {
91 return new Path(""); //$NON-NLS-1$
92 }
93
94 /**
95 * Reset the jar placeholder and delete project.
96 */
97 public void tearDownSuite() throws Exception {
98 this.ast = null;
99 if (TEST_SUITES == null) {
100 this.deleteProject("Converter"); //$NON-NLS-1$
101 this.deleteProject("Converter15"); //$NON-NLS-1$
102 this.deleteProject("Converter16"); //$NON-NLS-1$
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000103 this.deleteProject("Converter17"); //$NON-NLS-1$
Stephan Herrmanneffbeb02013-04-04 21:43:04 +0200104 this.deleteProject("Converter18"); //$NON-NLS-1$
Stephan Herrmann9688da62017-10-14 22:09:57 +0200105 this.deleteProject("Converter9"); //$NON-NLS-1$
Stephan Herrmann5e087fb2018-05-12 15:02:06 +0200106 this.deleteProject("Converter10"); //$NON-NLS-1$
Stephan Herrmanna441c702018-10-14 17:51:04 +0200107 this.deleteProject("Converter11"); //$NON-NLS-1$
Stephan Herrmann37b62262019-09-05 20:02:13 +0200108 this.deleteProject("Converter13"); //$NON-NLS-1$
Stephan Herrmannf59da282020-04-10 18:10:58 +0200109 this.deleteProject("Converter14"); //$NON-NLS-1$
Stephan Herrmann396f3342013-03-14 14:31:57 +0100110 PROJECT_SETUP = false;
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000111 } else {
112 TEST_SUITES.remove(getClass());
113 if (TEST_SUITES.size() == 0) {
114 this.deleteProject("Converter"); //$NON-NLS-1$
115 this.deleteProject("Converter15"); //$NON-NLS-1$
116 this.deleteProject("Converter16"); //$NON-NLS-1$
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000117 this.deleteProject("Converter17"); //$NON-NLS-1$
Stephan Herrmanneffbeb02013-04-04 21:43:04 +0200118 this.deleteProject("Converter18"); //$NON-NLS-1$
Stephan Herrmann9688da62017-10-14 22:09:57 +0200119 this.deleteProject("Converter9"); //$NON-NLS-1$
Stephan Herrmann5e087fb2018-05-12 15:02:06 +0200120 this.deleteProject("Converter10"); //$NON-NLS-1$
Stephan Herrmanna441c702018-10-14 17:51:04 +0200121 this.deleteProject("Converter11"); //$NON-NLS-1$
Stephan Herrmann37b62262019-09-05 20:02:13 +0200122 this.deleteProject("Converter13"); //$NON-NLS-1$
Stephan Herrmannf59da282020-04-10 18:10:58 +0200123 this.deleteProject("Converter14"); //$NON-NLS-1$
Stephan Herrmann396f3342013-03-14 14:31:57 +0100124 PROJECT_SETUP = false;
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000125 }
126 }
127
128 super.tearDownSuite();
129 }
130
Stephan Herrmann4d079452014-03-27 18:01:49 +0100131 public void setUpJCLClasspathVariables(String compliance, boolean useFullJCL) throws JavaModelException, IOException {
Stephan Herrmann9688da62017-10-14 22:09:57 +0200132 if (useFullJCL) {
133 super.setUpJCLClasspathVariables(compliance, useFullJCL);
134 return;
135 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000136 if ("1.5".equals(compliance)
137 || "1.6".equals(compliance)) {
138 if (JavaCore.getClasspathVariable("CONVERTER_JCL15_LIB") == null) {
139 setupExternalJCL("converterJclMin1.5");
140 JavaCore.setClasspathVariables(
141 new String[] {"CONVERTER_JCL15_LIB", "CONVERTER_JCL15_SRC", "CONVERTER_JCL15_SRCROOT"},
142 new IPath[] {getConverterJCLPath(compliance), getConverterJCLSourcePath(compliance), getConverterJCLRootSourcePath()},
143 null);
144 }
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000145 } else if ("1.7".equals(compliance)) {
146 if (JavaCore.getClasspathVariable("CONVERTER_JCL17_LIB") == null) {
147 setupExternalJCL("converterJclMin1.7");
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000148 JavaCore.setClasspathVariables(
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000149 new String[] {"CONVERTER_JCL17_LIB", "CONVERTER_JCL17_SRC", "CONVERTER_JCL17_SRCROOT"},
150 new IPath[] {getConverterJCLPath("1.7"), getConverterJCLSourcePath("1.7"), getConverterJCLRootSourcePath()},
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000151 null);
152 }
Stephan Herrmanneffbeb02013-04-04 21:43:04 +0200153 } else if ("1.8".equals(compliance)) {
154 if (JavaCore.getClasspathVariable("CONVERTER_JCL18_LIB") == null) {
155 setupExternalJCL("converterJclMin1.8");
156 JavaCore.setClasspathVariables(
157 new String[] {"CONVERTER_JCL18_LIB", "CONVERTER_JCL18_SRC", "CONVERTER_JCL18_SRCROOT"},
158 new IPath[] {getConverterJCLPath("1.8"), getConverterJCLSourcePath("1.8"), getConverterJCLRootSourcePath()},
159 null);
160 }
Stephan Herrmann9688da62017-10-14 22:09:57 +0200161 } else if ("9".equals(compliance)) {
162 if (JavaCore.getClasspathVariable("CONVERTER_JCL9_LIB") == null) {
163 setupExternalJCL("converterJclMin9");
164 JavaCore.setClasspathVariables(
165 new String[] {"CONVERTER_JCL9_LIB", "CONVERTER_JCL9_SRC", "CONVERTER_JCL9_SRCROOT"},
166 new IPath[] {getConverterJCLPath("9"), getConverterJCLSourcePath("9"), getConverterJCLRootSourcePath()},
167 null);
168 }
Stephan Herrmann5e087fb2018-05-12 15:02:06 +0200169 } else if ("10".equals(compliance)) {
170 if (JavaCore.getClasspathVariable("CONVERTER_JCL10_LIB") == null) {
171 setupExternalJCL("converterJclMin10");
172 JavaCore.setClasspathVariables(
173 new String[] {"CONVERTER_JCL10_LIB", "CONVERTER_JCL10_SRC", "CONVERTER_JCL10_SRCROOT"},
174 new IPath[] {getConverterJCLPath("10"), getConverterJCLSourcePath("10"), getConverterJCLRootSourcePath()},
175 null);
176 }
Stephan Herrmanna441c702018-10-14 17:51:04 +0200177 } else if ("11".equals(compliance)) {
178 if (JavaCore.getClasspathVariable("CONVERTER_JCL11_LIB") == null) {
179 setupExternalJCL("converterJclMin11");
180 JavaCore.setClasspathVariables(
181 new String[] {"CONVERTER_JCL11_LIB", "CONVERTER_JCL11_SRC", "CONVERTER_JCL11_SRCROOT"},
182 new IPath[] {getConverterJCLPath("11"), getConverterJCLSourcePath("11"), getConverterJCLRootSourcePath()},
183 null);
184 }
Stephan Herrmann7e648ad2019-03-21 15:13:29 +0100185 } else if ("12".equals(compliance)) {
186 if (JavaCore.getClasspathVariable("CONVERTER_JCL12_LIB") == null) {
187 setupExternalJCL("converterJclMin12");
188 JavaCore.setClasspathVariables(
Stephan Herrmann5f5a8392019-10-16 20:36:39 +0200189 new String[] {"CONVERTER_JCL12_LIB", "CONVERTER_JCL12_SRC", "CONVERTER_JCL12_SRCROOT"},
Stephan Herrmann7e648ad2019-03-21 15:13:29 +0100190 new IPath[] {getConverterJCLPath("12"), getConverterJCLSourcePath("12"), getConverterJCLRootSourcePath()},
191 null);
192 }
Stephan Herrmann5f5a8392019-10-16 20:36:39 +0200193 } else if ("13".equals(compliance)) {
194 if (JavaCore.getClasspathVariable("CONVERTER_JCL13_LIB") == null) {
195 setupExternalJCL("converterJclMin13");
196 JavaCore.setClasspathVariables(
197 new String[] {"CONVERTER_JCL13_LIB", "CONVERTER_JCL13_SRC", "CONVERTER_JCL13_SRCROOT"},
198 new IPath[] {getConverterJCLPath("13"), getConverterJCLSourcePath("13"), getConverterJCLRootSourcePath()},
199 null);
200 }
Stephan Herrmannf59da282020-04-10 18:10:58 +0200201 } else if ("14".equals(compliance)) {
202 if (JavaCore.getClasspathVariable("CONVERTER_JCL14_LIB") == null) {
203 setupExternalJCL("converterJclMin14");
204 JavaCore.setClasspathVariables(
205 new String[] {"CONVERTER_JCL14_LIB", "CONVERTER_JCL14_SRC", "CONVERTER_JCL14_SRCROOT"},
206 new IPath[] {getConverterJCLPath("14"), getConverterJCLSourcePath("14"), getConverterJCLRootSourcePath()},
207 null);
208 }
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000209 } else if (JavaCore.getClasspathVariable("CONVERTER_JCL_LIB") == null) {
210 setupExternalJCL("converterJclMin");
211 JavaCore.setClasspathVariables(
212 new String[] {"CONVERTER_JCL_LIB", "CONVERTER_JCL_SRC", "CONVERTER_JCL_SRCROOT"},
213 new IPath[] {getConverterJCLPath(), getConverterJCLSourcePath(), getConverterJCLRootSourcePath()},
214 null);
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000215 }
216 }
217
218 /**
219 * Create project and set the jar placeholder.
220 */
221 public void setUpSuite() throws Exception {
222 super.setUpSuite();
223
224 if (!PROJECT_SETUP) {
225 setUpJavaProject("Converter"); //$NON-NLS-1$
226 setUpJavaProject("Converter15", "1.5"); //$NON-NLS-1$ //$NON-NLS-2$
227 setUpJavaProject("Converter16", "1.6"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000228 setUpJavaProject("Converter17", "1.7"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmanneffbeb02013-04-04 21:43:04 +0200229 setUpJavaProject("Converter18", "1.8"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmann9688da62017-10-14 22:09:57 +0200230 setUpJavaProject("Converter9", "9"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmann5e087fb2018-05-12 15:02:06 +0200231 setUpJavaProject("Converter10", "10"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmanna441c702018-10-14 17:51:04 +0200232 setUpJavaProject("Converter11", "11"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmann37b62262019-09-05 20:02:13 +0200233 setUpJavaProject("Converter13", "13"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmannf59da282020-04-10 18:10:58 +0200234 setUpJavaProject("Converter14", "14"); //$NON-NLS-1$ //$NON-NLS-2$
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000235 waitUntilIndexesReady(); // needed to find secondary types
236 PROJECT_SETUP = true;
237 }
238 }
239
Stephan Herrmann63010de2013-05-01 20:08:26 +0200240 protected void assertExtraDimensionsEqual(String message, List dimensions, String expected) {
241 StringBuffer buffer = new StringBuffer();
242 Iterator iter = dimensions.iterator();
243 while(iter.hasNext()) {
Stephan Herrmannaf4fd6e2014-01-02 21:06:15 +0100244 Dimension dim = (Dimension) iter.next();
Stephan Herrmann59cd3cc2013-05-01 23:38:47 +0200245 buffer.append(convertAnnotationsList(dim.annotations()));
Stephan Herrmann63010de2013-05-01 20:08:26 +0200246 if (iter.hasNext()) {
247 buffer.append("[] ");
248 } else {
249 buffer.append("[]");
250 }
251 }
252 assertEquals(message, expected, buffer.toString());
253 }
254
Stephan Herrmann59cd3cc2013-05-01 23:38:47 +0200255 protected String convertAnnotationsList(List annotations) {
256 StringBuffer buffer = new StringBuffer();
257 Iterator iter = annotations.iterator();
258 while (iter.hasNext()) {
259 buffer.append('@');
260 buffer.append(((Annotation) iter.next()).getTypeName().getFullyQualifiedName());
261 buffer.append(' ');
262 }
263 return buffer.toString();
264 }
265
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000266 public ASTNode runConversion(ICompilationUnit unit, boolean resolveBindings,
267 boolean bindingsRecovery) {
268 return runConversion(astInternalJLS2(), unit, resolveBindings, false, bindingsRecovery);
269 }
270
271 public ASTNode runConversion(ICompilationUnit unit, boolean resolveBindings) {
272 return runConversion(astInternalJLS2(), unit, resolveBindings);
273 }
274
275 public ASTNode runConversion(ICompilationUnit unit, int position, boolean resolveBindings) {
276 return runConversion(astInternalJLS2(), unit, position, resolveBindings);
277 }
278
279 public ASTNode runConversion(IClassFile classFile, int position, boolean resolveBindings) {
280 return runConversion(astInternalJLS2(), classFile, position, resolveBindings);
281 }
282
283 public ASTNode runConversion(char[] source, String unitName, IJavaProject project) {
284 return runConversion(astInternalJLS2(), source, unitName, project);
285 }
286
287 public ASTNode runConversion(char[] source, String unitName, IJavaProject project, boolean resolveBindings) {
288 return runConversion(astInternalJLS2(), source, unitName, project, resolveBindings);
289 }
290
291 public ASTNode runConversion(int astLevel, ICompilationUnit unit, boolean resolveBindings) {
292 return runConversion(astLevel, unit, resolveBindings, false);
293 }
294
295 public ASTNode runConversion(int astLevel, ICompilationUnit unit, boolean resolveBindings, boolean statementsRecovery) {
296 return runConversion(astLevel, unit, resolveBindings, statementsRecovery, false);
297 }
298
299 public ASTNode runConversion(
300 int astLevel,
301 ICompilationUnit unit,
302 boolean resolveBindings,
303 boolean statementsRecovery,
304 boolean bindingsRecovery) {
305 ASTParser parser = ASTParser.newParser(astLevel);
306 parser.setSource(unit);
307 parser.setResolveBindings(resolveBindings);
308 parser.setStatementsRecovery(statementsRecovery);
309 parser.setBindingsRecovery(bindingsRecovery);
310 return parser.createAST(null);
311 }
312
313 class NullBindingVerifier extends ASTVisitor {
314
315 public void endVisit(ArrayAccess node) {
316 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
317 super.endVisit(node);
318 }
319
320 public void endVisit(ArrayCreation node) {
321 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
322 super.endVisit(node);
323 }
324
325 public void endVisit(ArrayInitializer node) {
326 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
327 super.endVisit(node);
328 }
329
330 public void endVisit(Assignment node) {
331 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
332 super.endVisit(node);
333 }
334
335 public void endVisit(BooleanLiteral node) {
336 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
337 super.endVisit(node);
338 }
339
340 public void endVisit(CastExpression node) {
341 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
342 super.endVisit(node);
343 }
344
345 public void endVisit(CharacterLiteral node) {
346 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
347 super.endVisit(node);
348 }
349
350 public void endVisit(ClassInstanceCreation node) {
351 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
352 super.endVisit(node);
353 }
354
355 public void endVisit(ConditionalExpression node) {
356 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
357 super.endVisit(node);
358 }
359
360 public void endVisit(FieldAccess node) {
361 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
362 super.endVisit(node);
363 }
364
365 public void endVisit(InfixExpression node) {
366 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
367 super.endVisit(node);
368 }
369
370 public void endVisit(InstanceofExpression node) {
371 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
372 super.endVisit(node);
373 }
374
375 public void endVisit(MarkerAnnotation node) {
376 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
377 super.endVisit(node);
378 }
379
380 public void endVisit(MethodInvocation node) {
381 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
382 super.endVisit(node);
383 }
384
385 public void endVisit(NormalAnnotation node) {
386 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
387 super.endVisit(node);
388 }
389
390 public void endVisit(NullLiteral node) {
391 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
392 super.endVisit(node);
393 }
394
395 public void endVisit(NumberLiteral node) {
396 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
397 super.endVisit(node);
398 }
399
400 public void endVisit(ParenthesizedExpression node) {
401 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
402 super.endVisit(node);
403 }
404
405 public void endVisit(PostfixExpression node) {
406 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
407 super.endVisit(node);
408 }
409
410 public void endVisit(PrefixExpression node) {
411 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
412 super.endVisit(node);
413 }
414
415 public void endVisit(SingleMemberAnnotation node) {
416 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
417 super.endVisit(node);
418 }
419
420 public void endVisit(StringLiteral node) {
421 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
422 super.endVisit(node);
423 }
424
425 public void endVisit(SuperFieldAccess node) {
426 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
427 super.endVisit(node);
428 }
429
430 public void endVisit(SuperMethodInvocation node) {
431 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
432 super.endVisit(node);
433 }
434
435 public void endVisit(ThisExpression node) {
436 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
437 super.endVisit(node);
438 }
439
440 public void endVisit(TypeLiteral node) {
441 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
442 super.endVisit(node);
443 }
444
445 public void endVisit(VariableDeclarationExpression node) {
446 assertNotNull(node+" should have a binding", node.resolveTypeBinding());
447 super.endVisit(node);
448 }
449
450 public void endVisit(AnnotationTypeDeclaration node) {
451 assertNotNull(node+" should have a binding", node.resolveBinding());
452 super.endVisit(node);
453 }
454
455 public void endVisit(AnnotationTypeMemberDeclaration node) {
456 assertNotNull(node+" should have a binding", node.resolveBinding());
457 super.endVisit(node);
458 }
459
460 public void endVisit(AnonymousClassDeclaration node) {
461 assertNotNull(node+" should have a binding", node.resolveBinding());
462 super.endVisit(node);
463 }
464
465 public void endVisit(ArrayType node) {
466 assertNotNull(node+" should have a binding", node.resolveBinding());
467 super.endVisit(node);
468 }
469
470 public void endVisit(EnumDeclaration node) {
471 assertNotNull(node+" should have a binding", node.resolveBinding());
472 super.endVisit(node);
473 }
474
475 public void endVisit(ImportDeclaration node) {
476 assertNotNull(node+" should have a binding", node.resolveBinding());
477 super.endVisit(node);
478 }
479
480 public void endVisit(MemberRef node) {
481 assertNotNull(node+" should have a binding", node.resolveBinding());
482 super.endVisit(node);
483 }
484
485 public void endVisit(MethodDeclaration node) {
486 assertNotNull(node+" should have a binding", node.resolveBinding());
487 super.endVisit(node);
488 }
489
Stephan Herrmann9688da62017-10-14 22:09:57 +0200490 public void endVisit(ModuleDeclaration node) {
491 assertNotNull(node+" should have a binding", node.resolveBinding());
492 super.endVisit(node);
493 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000494 public void endVisit(MethodRef node) {
495 assertNotNull(node+" should have a binding", node.resolveBinding());
496 super.endVisit(node);
497 }
498
499 public void endVisit(PackageDeclaration node) {
500 assertNotNull(node+" should have a binding", node.resolveBinding());
501 super.endVisit(node);
502 }
503
504 public void endVisit(ParameterizedType node) {
505 assertNotNull(node+" should have a binding", node.resolveBinding());
506 super.endVisit(node);
507 }
508
509 public void endVisit(PrimitiveType node) {
510 assertNotNull(node+" should have a binding", node.resolveBinding());
511 super.endVisit(node);
512 }
513
514 public void endVisit(QualifiedName node) {
515 assertNotNull(node+" should have a binding", node.resolveBinding());
516 super.endVisit(node);
517 }
518
519 public void endVisit(QualifiedType node) {
520 assertNotNull(node+" should have a binding", node.resolveBinding());
521 super.endVisit(node);
522 }
523
524 public void endVisit(SimpleName node) {
525 assertNotNull(node+" should have a binding", node.resolveBinding());
526 super.endVisit(node);
527 }
528
529 public void endVisit(SimpleType node) {
530 assertNotNull(node+" should have a binding", node.resolveBinding());
531 super.endVisit(node);
532 }
533
534 public void endVisit(SingleVariableDeclaration node) {
535 assertNotNull(node+" should have a binding", node.resolveBinding());
536 super.endVisit(node);
537 }
538
539 public void endVisit(TypeDeclaration node) {
540 assertNotNull(node+" should have a binding", node.resolveBinding());
541 super.endVisit(node);
542 }
543
544 public void endVisit(TypeDeclarationStatement node) {
545 assertNotNull(node+" should have a binding", node.resolveBinding());
546 super.endVisit(node);
547 }
548
549 public void endVisit(TypeParameter node) {
550 assertNotNull(node+" should have a binding", node.resolveBinding());
551 super.endVisit(node);
552 }
553
554 public void endVisit(VariableDeclarationFragment node) {
555 assertNotNull(node+" should have a binding", node.resolveBinding());
556 super.endVisit(node);
557 }
558
559 public void endVisit(WildcardType node) {
560 assertNotNull(node+" should have a binding", node.resolveBinding());
561 super.endVisit(node);
562 }
563
564 }
565 public ASTNode runJLS3Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2) {
566 return runJLS3Conversion(unit, resolveBindings, checkJLS2, false);
567 }
568
569 public ASTNode runJLS3Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2, boolean bindingRecovery) {
570
571 // Create parser
572 ASTParser parser;
573 if (checkJLS2) {
574 parser = ASTParser.newParser(astInternalJLS2());
575 parser.setSource(unit);
576 parser.setResolveBindings(resolveBindings);
577 parser.setBindingsRecovery(bindingRecovery);
578 parser.createAST(null);
579 }
580
Stephan Herrmannb9e10522012-03-15 21:24:23 +0100581 parser = ASTParser.newParser(JLS3_INTERNAL);
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000582 parser.setSource(unit);
583 parser.setResolveBindings(resolveBindings);
584 parser.setBindingsRecovery(bindingRecovery);
585
586 // Parse compilation unit
587 ASTNode result = parser.createAST(null);
588
589 // Verify we get a compilation unit node and that binding are correct
590 assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
591 CompilationUnit compilationUnit = (CompilationUnit) result;
592 if (resolveBindings && compilationUnit.getProblems().length == 0) {
593 compilationUnit.accept(new NullBindingVerifier());
594 }
595 return result;
596 }
597
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000598 public ASTNode runJLS4Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2) {
599 return runJLS4Conversion(unit, resolveBindings, checkJLS2, false);
600 }
601
602 public ASTNode runJLS4Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2, boolean bindingRecovery) {
603
604 // Create parser
605 ASTParser parser;
606 if (checkJLS2) {
607 parser = ASTParser.newParser(astInternalJLS2());
608 parser.setSource(unit);
609 parser.setResolveBindings(resolveBindings);
610 parser.setBindingsRecovery(bindingRecovery);
611 parser.createAST(null);
612 }
613
Stephan Herrmann307ebe32013-05-02 02:46:05 +0200614 parser = ASTParser.newParser(JLS4_INTERNAL);
Stephan Herrmann4683caf2011-08-05 06:54:51 +0000615 parser.setSource(unit);
616 parser.setResolveBindings(resolveBindings);
617 parser.setBindingsRecovery(bindingRecovery);
618
619 // Parse compilation unit
620 ASTNode result = parser.createAST(null);
621
622 // Verify we get a compilation unit node and that binding are correct
623 assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
624 CompilationUnit compilationUnit = (CompilationUnit) result;
625 if (resolveBindings && compilationUnit.getProblems().length == 0) {
626 compilationUnit.accept(new NullBindingVerifier());
627 }
628 return result;
629 }
630
Stephan Herrmann5ab17b32013-04-04 22:06:05 +0200631 public ASTNode runJLS8Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2) {
632 return runJLS8Conversion(unit, resolveBindings, checkJLS2, false);
633 }
634
Stephan Herrmann9688da62017-10-14 22:09:57 +0200635 /**
636 * @deprecated references deprecated old AST level
637 */
Stephan Herrmann5ab17b32013-04-04 22:06:05 +0200638 public ASTNode runJLS8Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2, boolean bindingRecovery) {
639
640 // Create parser
641 ASTParser parser;
642 if (checkJLS2) {
643 parser = ASTParser.newParser(astInternalJLS2());
644 parser.setSource(unit);
645 parser.setResolveBindings(resolveBindings);
646 parser.setBindingsRecovery(bindingRecovery);
647 parser.createAST(null);
648 }
649
650 parser = ASTParser.newParser(AST.JLS8);
651 parser.setSource(unit);
652 parser.setResolveBindings(resolveBindings);
653 parser.setBindingsRecovery(bindingRecovery);
654
655 // Parse compilation unit
656 ASTNode result = parser.createAST(null);
657
658 // Verify we get a compilation unit node and that binding are correct
659 assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
660 CompilationUnit compilationUnit = (CompilationUnit) result;
661 if (resolveBindings && compilationUnit.getProblems().length == 0) {
662 compilationUnit.accept(new NullBindingVerifier());
663 }
664 return result;
665 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000666 public ASTNode runConversion(int astLevel, ICompilationUnit unit, int position, boolean resolveBindings) {
667
668 // Create parser
669 ASTParser parser = ASTParser.newParser(astLevel);
670 parser.setSource(unit);
671 parser.setFocalPosition(position);
672 parser.setResolveBindings(resolveBindings);
673
674 // Parse compilation unit
675 ASTNode result = parser.createAST(null);
676
677 // Verify we get a compilation unit node and that binding are correct
678 assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
679 CompilationUnit compilationUnit = (CompilationUnit) result;
680 if (resolveBindings && compilationUnit.getProblems().length == 0) {
681 compilationUnit.accept(new NullBindingVerifier());
682 }
683 return result;
684 }
685
686 public ASTNode runConversion(int astLevel, IClassFile classFile, int position, boolean resolveBindings) {
687
688 // Create parser
689 ASTParser parser = ASTParser.newParser(astLevel);
690 parser.setSource(classFile);
691 parser.setFocalPosition(position);
692 parser.setResolveBindings(resolveBindings);
693
694 // Parse compilation unit
695 ASTNode result = parser.createAST(null);
696
697 // Verify we get a compilation unit node and that binding are correct
698 assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
699 CompilationUnit compilationUnit = (CompilationUnit) result;
700 if (resolveBindings && compilationUnit.getProblems().length == 0) {
701 compilationUnit.accept(new NullBindingVerifier());
702 }
703 return result;
704 }
705
706 public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project) {
707 return runConversion(astLevel, source, unitName, project, false);
708 }
709
710 public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project, boolean resolveBindings) {
711 return runConversion(astLevel, source, unitName, project, null, resolveBindings);
712 }
713
Stephan Herrmann210dc3a2015-08-13 22:46:42 +0200714 public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project, Map<String, String> options, boolean resolveBindings) {
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000715 ASTParser parser = ASTParser.newParser(astLevel);
716 parser.setSource(source);
717 parser.setUnitName(unitName);
718 parser.setProject(project);
719 if (options != null) {
720 parser.setCompilerOptions(options);
721 }
722 parser.setResolveBindings(resolveBindings);
723 return parser.createAST(null);
724 }
725
Stephan Herrmann210dc3a2015-08-13 22:46:42 +0200726 public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project, Map<String, String> options) {
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000727 return runConversion(astLevel, source, unitName, project, options, false);
728 }
729
Stephan Herrmann210dc3a2015-08-13 22:46:42 +0200730 public ASTNode runConversion(char[] source, String unitName, IJavaProject project, Map<String, String> options, boolean resolveBindings) {
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000731 return runConversion(astInternalJLS2(), source, unitName, project, options, resolveBindings);
732 }
Stephan Herrmann210dc3a2015-08-13 22:46:42 +0200733 public ASTNode runConversion(char[] source, String unitName, IJavaProject project, Map<String, String> options) {
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000734 return runConversion(astInternalJLS2(), source, unitName, project, options);
735 }
736
737 protected ASTNode getASTNodeToCompare(org.eclipse.jdt.core.dom.CompilationUnit unit) {
738 ExpressionStatement statement = (ExpressionStatement) getASTNode(unit, 0, 0, 0);
739 return (ASTNode) ((MethodInvocation) statement.getExpression()).arguments().get(0);
740 }
741
742 protected ASTNode getASTNode(org.eclipse.jdt.core.dom.CompilationUnit unit, int typeIndex, int bodyIndex, int statementIndex) {
743 BodyDeclaration bodyDeclaration = (BodyDeclaration) getASTNode(unit, typeIndex, bodyIndex);
744 if (bodyDeclaration instanceof MethodDeclaration) {
745 MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
746 Block block = methodDeclaration.getBody();
747 return (ASTNode) block.statements().get(statementIndex);
748 } else if (bodyDeclaration instanceof TypeDeclaration) {
749 TypeDeclaration typeDeclaration = (TypeDeclaration) bodyDeclaration;
750 return (ASTNode) typeDeclaration.bodyDeclarations().get(statementIndex);
751 } else if (bodyDeclaration instanceof Initializer) {
752 Initializer initializer = (Initializer) bodyDeclaration;
753 Block block = initializer.getBody();
754 return (ASTNode) block.statements().get(statementIndex);
755 }
756 return null;
757 }
758
759 protected ASTNode getASTNode(org.eclipse.jdt.core.dom.CompilationUnit unit, int typeIndex, int bodyIndex) {
760 return (ASTNode) ((AbstractTypeDeclaration)unit.types().get(typeIndex)).bodyDeclarations().get(bodyIndex);
761 }
762
763 protected ASTNode getASTNode(org.eclipse.jdt.core.dom.CompilationUnit unit, int typeIndex) {
764 return (ASTNode) unit.types().get(typeIndex);
765 }
766
767 protected void checkSourceRange(int start, int length, String expectedContents, String source) {
768 assertTrue("length == 0", length != 0); //$NON-NLS-1$ //$NON-NLS-2$
769 assertTrue("start == -1", start != -1); //$NON-NLS-1$
770 String actualContentsString = source.substring(start, start + length);
771 assertSourceEquals("Unexpected source", Util.convertToIndependantLineDelimiter(expectedContents), Util.convertToIndependantLineDelimiter(actualContentsString));
772 }
773
774 protected void checkSourceRange(ASTNode node, String expectedContents, String source) {
775 assertNotNull("The node is null", node); //$NON-NLS-1$
776 assertTrue("The node(" + node.getClass() + ").getLength() == 0", node.getLength() != 0); //$NON-NLS-1$ //$NON-NLS-2$
777 assertTrue("The node.getStartPosition() == -1", node.getStartPosition() != -1); //$NON-NLS-1$
778 int length = node.getLength();
779 int start = node.getStartPosition();
780 String actualContentsString = source.substring(start, start + length);
781 assertSourceEquals("Unexpected source", Util.convertToIndependantLineDelimiter(expectedContents), Util.convertToIndependantLineDelimiter(actualContentsString));
782 }
783
784 protected void checkSourceRange(ASTNode node, String expectedContents, char[] source) {
Stephan Herrmann192b65f2011-01-27 12:07:40 +0000785 checkSourceRange(node, expectedContents, source, false);
786 }
787 protected void checkSourceRange(ASTNode node, String expectedContents, char[] source, boolean expectMalformed) {
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000788 assertNotNull("The node is null", node); //$NON-NLS-1$
789 assertTrue("The node(" + node.getClass() + ").getLength() == 0", node.getLength() != 0); //$NON-NLS-1$ //$NON-NLS-2$
790 assertTrue("The node.getStartPosition() == -1", node.getStartPosition() != -1); //$NON-NLS-1$
791 int length = node.getLength();
792 int start = node.getStartPosition();
793 char[] actualContents = new char[length];
794 System.arraycopy(source, start, actualContents, 0, length);
795 String actualContentsString = new String(actualContents);
796 assertSourceEquals("Unexpected source", Util.convertToIndependantLineDelimiter(expectedContents), Util.convertToIndependantLineDelimiter(actualContentsString));
Stephan Herrmann192b65f2011-01-27 12:07:40 +0000797 if (expectMalformed) {
798 assertTrue("Is not malformed", isMalformed(node));
799 } else {
800 assertFalse("Is malformed", isMalformed(node));
801 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000802 }
803
804 protected boolean isMalformed(ASTNode node) {
805 return (node.getFlags() & ASTNode.MALFORMED) != 0;
806 }
807
808 protected boolean isRecovered(ASTNode node) {
809 return (node.getFlags() & ASTNode.RECOVERED) != 0;
810 }
811
812 protected boolean isOriginal(ASTNode node) {
813 return (node.getFlags() & ASTNode.ORIGINAL) != 0;
814 }
815
816 protected void assertProblemsSize(CompilationUnit compilationUnit, int expectedSize) {
817 assertProblemsSize(compilationUnit, expectedSize, "");
818 }
819 protected void assertProblemsSize(CompilationUnit compilationUnit, int expectedSize, String expectedOutput) {
820 final IProblem[] problems = compilationUnit.getProblems();
821 final int length = problems.length;
822 if (length != expectedSize) {
823 checkProblemMessages(expectedOutput, problems, length);
824 assertEquals("Wrong size", expectedSize, length);
825 }
826 checkProblemMessages(expectedOutput, problems, length);
827 }
828
829 private void checkProblemMessages(String expectedOutput, final IProblem[] problems, final int length) {
830 if (length != 0) {
831 if (expectedOutput != null) {
832 StringBuffer buffer = new StringBuffer();
833 for (int i = 0; i < length; i++) {
834 buffer.append(problems[i].getMessage());
835 if (i < length - 1) {
836 buffer.append('\n');
837 }
838 }
839 String actualOutput = String.valueOf(buffer);
840 expectedOutput = Util.convertToIndependantLineDelimiter(expectedOutput);
841 actualOutput = Util.convertToIndependantLineDelimiter(actualOutput);
842 if (!expectedOutput.equals(actualOutput)) {
843 System.out.println(Util.displayString(actualOutput));
844 assertEquals("different output", expectedOutput, actualOutput);
845 }
846 }
847 }
848 }
Stephan Herrmannbe6c0ea2010-04-01 21:59:30 +0000849}