Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
Stephan Herrmann | f59da28 | 2020-04-10 18:10:58 +0200 | [diff] [blame] | 2 | * Copyright (c) 2000, 2020 IBM Corporation and others. |
Stephan Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 6 | * which accompanies this distribution, and is available at |
Stephan Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.core.tests.dom; |
| 15 | |
| 16 | import java.io.IOException; |
Stephan Herrmann | 63010de | 2013-05-01 20:08:26 +0200 | [diff] [blame] | 17 | import java.util.Iterator; |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.eclipse.core.runtime.IPath; |
| 22 | import org.eclipse.core.runtime.Path; |
| 23 | import org.eclipse.jdt.core.IClassFile; |
| 24 | import org.eclipse.jdt.core.ICompilationUnit; |
| 25 | import org.eclipse.jdt.core.IJavaProject; |
| 26 | import org.eclipse.jdt.core.JavaCore; |
| 27 | import org.eclipse.jdt.core.JavaModelException; |
| 28 | import org.eclipse.jdt.core.compiler.IProblem; |
| 29 | import org.eclipse.jdt.core.dom.*; |
| 30 | import org.eclipse.jdt.core.tests.util.Util; |
| 31 | |
Stephan Herrmann | 5d44b3a | 2015-03-17 18:08:14 +0100 | [diff] [blame] | 32 | @SuppressWarnings("rawtypes") |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 33 | public abstract class ConverterTestSetup extends AbstractASTTests { |
Stephan Herrmann | b9e1052 | 2012-03-15 21:24:23 +0100 | [diff] [blame] | 34 | /** |
Stephan Herrmann | 307ebe3 | 2013-05-02 02:46:05 +0200 | [diff] [blame] | 35 | * Internal synonym for deprecated constant AST.JSL3 |
Stephan Herrmann | b9e1052 | 2012-03-15 21:24:23 +0100 | [diff] [blame] | 36 | * to alleviate deprecation warnings. |
| 37 | * @deprecated |
| 38 | */ |
| 39 | /*package*/ static final int JLS3_INTERNAL = AST.JLS3; |
Stephan Herrmann | 307ebe3 | 2013-05-02 02:46:05 +0200 | [diff] [blame] | 40 | |
| 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 Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 48 | /** |
| 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 Herrmann | 307ebe3 | 2013-05-02 02:46:05 +0200 | [diff] [blame] | 55 | static int getJLS3() { |
| 56 | return JLS3_INTERNAL; |
| 57 | } |
| 58 | |
| 59 | static int getJLS4() { |
| 60 | return JLS4_INTERNAL; |
| 61 | } |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 62 | |
| 63 | static int getJLS8() { |
| 64 | return JLS8_INTERNAL; |
Stephan Herrmann | c4606e8 | 2020-04-11 14:28:09 +0200 | [diff] [blame^] | 65 | } |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 66 | protected AST ast; |
Stephan Herrmann | 5ab17b3 | 2013-04-04 22:06:05 +0200 | [diff] [blame] | 67 | public static List TEST_SUITES = null; |
| 68 | public static boolean PROJECT_SETUP = false; |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 69 | |
| 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 103 | this.deleteProject("Converter17"); //$NON-NLS-1$ |
Stephan Herrmann | effbeb0 | 2013-04-04 21:43:04 +0200 | [diff] [blame] | 104 | this.deleteProject("Converter18"); //$NON-NLS-1$ |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 105 | this.deleteProject("Converter9"); //$NON-NLS-1$ |
Stephan Herrmann | 5e087fb | 2018-05-12 15:02:06 +0200 | [diff] [blame] | 106 | this.deleteProject("Converter10"); //$NON-NLS-1$ |
Stephan Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 107 | this.deleteProject("Converter11"); //$NON-NLS-1$ |
Stephan Herrmann | 37b6226 | 2019-09-05 20:02:13 +0200 | [diff] [blame] | 108 | this.deleteProject("Converter13"); //$NON-NLS-1$ |
Stephan Herrmann | f59da28 | 2020-04-10 18:10:58 +0200 | [diff] [blame] | 109 | this.deleteProject("Converter14"); //$NON-NLS-1$ |
Stephan Herrmann | 396f334 | 2013-03-14 14:31:57 +0100 | [diff] [blame] | 110 | PROJECT_SETUP = false; |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 111 | } 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 117 | this.deleteProject("Converter17"); //$NON-NLS-1$ |
Stephan Herrmann | effbeb0 | 2013-04-04 21:43:04 +0200 | [diff] [blame] | 118 | this.deleteProject("Converter18"); //$NON-NLS-1$ |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 119 | this.deleteProject("Converter9"); //$NON-NLS-1$ |
Stephan Herrmann | 5e087fb | 2018-05-12 15:02:06 +0200 | [diff] [blame] | 120 | this.deleteProject("Converter10"); //$NON-NLS-1$ |
Stephan Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 121 | this.deleteProject("Converter11"); //$NON-NLS-1$ |
Stephan Herrmann | 37b6226 | 2019-09-05 20:02:13 +0200 | [diff] [blame] | 122 | this.deleteProject("Converter13"); //$NON-NLS-1$ |
Stephan Herrmann | f59da28 | 2020-04-10 18:10:58 +0200 | [diff] [blame] | 123 | this.deleteProject("Converter14"); //$NON-NLS-1$ |
Stephan Herrmann | 396f334 | 2013-03-14 14:31:57 +0100 | [diff] [blame] | 124 | PROJECT_SETUP = false; |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | super.tearDownSuite(); |
| 129 | } |
| 130 | |
Stephan Herrmann | 4d07945 | 2014-03-27 18:01:49 +0100 | [diff] [blame] | 131 | public void setUpJCLClasspathVariables(String compliance, boolean useFullJCL) throws JavaModelException, IOException { |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 132 | if (useFullJCL) { |
| 133 | super.setUpJCLClasspathVariables(compliance, useFullJCL); |
| 134 | return; |
| 135 | } |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 136 | 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 145 | } else if ("1.7".equals(compliance)) { |
| 146 | if (JavaCore.getClasspathVariable("CONVERTER_JCL17_LIB") == null) { |
| 147 | setupExternalJCL("converterJclMin1.7"); |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 148 | JavaCore.setClasspathVariables( |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 149 | new String[] {"CONVERTER_JCL17_LIB", "CONVERTER_JCL17_SRC", "CONVERTER_JCL17_SRCROOT"}, |
| 150 | new IPath[] {getConverterJCLPath("1.7"), getConverterJCLSourcePath("1.7"), getConverterJCLRootSourcePath()}, |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 151 | null); |
| 152 | } |
Stephan Herrmann | effbeb0 | 2013-04-04 21:43:04 +0200 | [diff] [blame] | 153 | } 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 Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 161 | } 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 Herrmann | 5e087fb | 2018-05-12 15:02:06 +0200 | [diff] [blame] | 169 | } 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 Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 177 | } 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 Herrmann | 7e648ad | 2019-03-21 15:13:29 +0100 | [diff] [blame] | 185 | } else if ("12".equals(compliance)) { |
| 186 | if (JavaCore.getClasspathVariable("CONVERTER_JCL12_LIB") == null) { |
| 187 | setupExternalJCL("converterJclMin12"); |
| 188 | JavaCore.setClasspathVariables( |
Stephan Herrmann | 5f5a839 | 2019-10-16 20:36:39 +0200 | [diff] [blame] | 189 | new String[] {"CONVERTER_JCL12_LIB", "CONVERTER_JCL12_SRC", "CONVERTER_JCL12_SRCROOT"}, |
Stephan Herrmann | 7e648ad | 2019-03-21 15:13:29 +0100 | [diff] [blame] | 190 | new IPath[] {getConverterJCLPath("12"), getConverterJCLSourcePath("12"), getConverterJCLRootSourcePath()}, |
| 191 | null); |
| 192 | } |
Stephan Herrmann | 5f5a839 | 2019-10-16 20:36:39 +0200 | [diff] [blame] | 193 | } 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 Herrmann | f59da28 | 2020-04-10 18:10:58 +0200 | [diff] [blame] | 201 | } 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 209 | } 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 Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 215 | } |
| 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 228 | setUpJavaProject("Converter17", "1.7"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | effbeb0 | 2013-04-04 21:43:04 +0200 | [diff] [blame] | 229 | setUpJavaProject("Converter18", "1.8"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 230 | setUpJavaProject("Converter9", "9"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | 5e087fb | 2018-05-12 15:02:06 +0200 | [diff] [blame] | 231 | setUpJavaProject("Converter10", "10"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | a441c70 | 2018-10-14 17:51:04 +0200 | [diff] [blame] | 232 | setUpJavaProject("Converter11", "11"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | 37b6226 | 2019-09-05 20:02:13 +0200 | [diff] [blame] | 233 | setUpJavaProject("Converter13", "13"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | f59da28 | 2020-04-10 18:10:58 +0200 | [diff] [blame] | 234 | setUpJavaProject("Converter14", "14"); //$NON-NLS-1$ //$NON-NLS-2$ |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 235 | waitUntilIndexesReady(); // needed to find secondary types |
| 236 | PROJECT_SETUP = true; |
| 237 | } |
| 238 | } |
| 239 | |
Stephan Herrmann | 63010de | 2013-05-01 20:08:26 +0200 | [diff] [blame] | 240 | protected void assertExtraDimensionsEqual(String message, List dimensions, String expected) { |
| 241 | StringBuffer buffer = new StringBuffer(); |
| 242 | Iterator iter = dimensions.iterator(); |
| 243 | while(iter.hasNext()) { |
Stephan Herrmann | af4fd6e | 2014-01-02 21:06:15 +0100 | [diff] [blame] | 244 | Dimension dim = (Dimension) iter.next(); |
Stephan Herrmann | 59cd3cc | 2013-05-01 23:38:47 +0200 | [diff] [blame] | 245 | buffer.append(convertAnnotationsList(dim.annotations())); |
Stephan Herrmann | 63010de | 2013-05-01 20:08:26 +0200 | [diff] [blame] | 246 | if (iter.hasNext()) { |
| 247 | buffer.append("[] "); |
| 248 | } else { |
| 249 | buffer.append("[]"); |
| 250 | } |
| 251 | } |
| 252 | assertEquals(message, expected, buffer.toString()); |
| 253 | } |
| 254 | |
Stephan Herrmann | 59cd3cc | 2013-05-01 23:38:47 +0200 | [diff] [blame] | 255 | 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 Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 266 | 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 Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 490 | public void endVisit(ModuleDeclaration node) { |
| 491 | assertNotNull(node+" should have a binding", node.resolveBinding()); |
| 492 | super.endVisit(node); |
| 493 | } |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 494 | 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 Herrmann | b9e1052 | 2012-03-15 21:24:23 +0100 | [diff] [blame] | 581 | parser = ASTParser.newParser(JLS3_INTERNAL); |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 582 | 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 Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 598 | 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 Herrmann | 307ebe3 | 2013-05-02 02:46:05 +0200 | [diff] [blame] | 614 | parser = ASTParser.newParser(JLS4_INTERNAL); |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 615 | 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 Herrmann | 5ab17b3 | 2013-04-04 22:06:05 +0200 | [diff] [blame] | 631 | public ASTNode runJLS8Conversion(ICompilationUnit unit, boolean resolveBindings, boolean checkJLS2) { |
| 632 | return runJLS8Conversion(unit, resolveBindings, checkJLS2, false); |
| 633 | } |
| 634 | |
Stephan Herrmann | 9688da6 | 2017-10-14 22:09:57 +0200 | [diff] [blame] | 635 | /** |
| 636 | * @deprecated references deprecated old AST level |
| 637 | */ |
Stephan Herrmann | 5ab17b3 | 2013-04-04 22:06:05 +0200 | [diff] [blame] | 638 | 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 Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 666 | 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 Herrmann | 210dc3a | 2015-08-13 22:46:42 +0200 | [diff] [blame] | 714 | public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project, Map<String, String> options, boolean resolveBindings) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 715 | 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 Herrmann | 210dc3a | 2015-08-13 22:46:42 +0200 | [diff] [blame] | 726 | public ASTNode runConversion(int astLevel, char[] source, String unitName, IJavaProject project, Map<String, String> options) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 727 | return runConversion(astLevel, source, unitName, project, options, false); |
| 728 | } |
| 729 | |
Stephan Herrmann | 210dc3a | 2015-08-13 22:46:42 +0200 | [diff] [blame] | 730 | public ASTNode runConversion(char[] source, String unitName, IJavaProject project, Map<String, String> options, boolean resolveBindings) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 731 | return runConversion(astInternalJLS2(), source, unitName, project, options, resolveBindings); |
| 732 | } |
Stephan Herrmann | 210dc3a | 2015-08-13 22:46:42 +0200 | [diff] [blame] | 733 | public ASTNode runConversion(char[] source, String unitName, IJavaProject project, Map<String, String> options) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 734 | 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 Herrmann | 192b65f | 2011-01-27 12:07:40 +0000 | [diff] [blame] | 785 | checkSourceRange(node, expectedContents, source, false); |
| 786 | } |
| 787 | protected void checkSourceRange(ASTNode node, String expectedContents, char[] source, boolean expectMalformed) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 788 | 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 Herrmann | 192b65f | 2011-01-27 12:07:40 +0000 | [diff] [blame] | 797 | if (expectMalformed) { |
| 798 | assertTrue("Is not malformed", isMalformed(node)); |
| 799 | } else { |
| 800 | assertFalse("Is malformed", isMalformed(node)); |
| 801 | } |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 802 | } |
| 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 Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 849 | } |