View | Details | Raw Unified | Return to bug 186410 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java (+1 lines)
Lines 27-32 Link Here
27
	return new Class[] {
27
	return new Class[] {
28
		ASTConverterTest.class,		
28
		ASTConverterTest.class,		
29
		ASTConverterTest2.class,
29
		ASTConverterTest2.class,
30
		ASTConverterBugsTest.class,		
30
		ASTConverterJavadocTest.class,
31
		ASTConverterJavadocTest.class,
31
		ASTConverter15Test.class,
32
		ASTConverter15Test.class,
32
		ASTConverter16Test.class,
33
		ASTConverter16Test.class,
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java (+158 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.dom;
12
13
import java.io.IOException;
14
15
import junit.framework.Test;
16
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.jdt.core.ICompilationUnit;
19
import org.eclipse.jdt.core.dom.AST;
20
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
21
import org.eclipse.jdt.core.dom.CompilationUnit;
22
import org.eclipse.jdt.core.dom.FieldDeclaration;
23
import org.eclipse.jdt.core.dom.ITypeBinding;
24
import org.eclipse.jdt.core.dom.Type;
25
26
public class ASTConverterBugsTest extends ConverterTestSetup {
27
	
28
	public void setUpSuite() throws Exception {
29
		PROJECT_SETUP = true; // do not copy Converter* directories
30
		super.setUpSuite();
31
		setUpJCLClasspathVariables("1.4");
32
		waitUntilIndexesReady();
33
	}
34
35
	public ASTConverterBugsTest(String name) {
36
		super(name);
37
	}
38
39
	public static Test suite() {
40
		return buildModelTestSuite(ASTConverterBugsTest.class);
41
	}
42
	
43
	/**
44
	 * @bug 186410: [dom] StackOverflowError due to endless superclass bindings hierarchy
45
	 * @test Ensures that the superclass of "java.lang.Object" class is null even when it's a recovered binding
46
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=186410"
47
	 */
48
	public void testBug186410() throws CoreException, IOException {
49
		try {
50
			createJavaProject("P", new String[] {""}, new String[0], "");
51
			createFile("P/A.java",
52
				"public class A {\n" +
53
				"	void method(){}\n" +
54
				"}"
55
			);
56
			ICompilationUnit cuA = getCompilationUnit("P/A.java");
57
			CompilationUnit unitA = (CompilationUnit) runConversion(AST.JLS3, cuA, true, false, true);
58
			AbstractTypeDeclaration typeA = (AbstractTypeDeclaration)unitA.types().get(0);
59
			ITypeBinding objectType = typeA.resolveBinding().getSuperclass();
60
			assertEquals("Unexpected superclass", "Object", objectType .getName());
61
			ITypeBinding objectSuperclass = objectType.getSuperclass();
62
			assertNull("java.lang.Object should  not have any superclass", objectSuperclass);
63
		} finally {
64
			deleteProject("P");
65
		}
66
	}
67
	public void testBug186410b() throws CoreException, IOException {
68
		try {
69
			createJavaProject("P", new String[] {""}, new String[0], "");
70
			createFile("P/A.java",
71
				"public class A {\n" +
72
				"	Object field;\n" +
73
				"}"
74
			);
75
			ICompilationUnit cuA = getCompilationUnit("P/A.java");
76
			CompilationUnit unitA = (CompilationUnit) runConversion(AST.JLS3, cuA, true, false, true);
77
			AbstractTypeDeclaration type = (AbstractTypeDeclaration)unitA.types().get(0);
78
			FieldDeclaration field = (FieldDeclaration) type.bodyDeclarations().get(0);
79
			Type fieldType = field.getType();
80
			ITypeBinding typeBinding = fieldType.resolveBinding();
81
			ITypeBinding objectType = typeBinding.createArrayType(2).getElementType();
82
			assertEquals("Unexpected superclass", "Object", objectType.getName());
83
			ITypeBinding objectSuperclass = objectType.getSuperclass();
84
			assertNull("java.lang.Object should  not have any superclass", objectSuperclass);
85
		} finally {
86
			deleteProject("P");
87
		}
88
	}
89
	
90
	/**
91
	 * @bug 209510: [dom] Recovered type binding for "java.lang.Object" information are not complete
92
	 * @test Ensures that getPackage() and getQualifiedName() works properly for the "java.lang.Object" recovered binding
93
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=209510"
94
	 */
95
	public void testBug209510a() throws CoreException, IOException {
96
		try {
97
			createJavaProject("P", new String[] {""}, new String[0], "");
98
			createFile("P/A.java",
99
				"public class A {\n" +
100
				"	void method(){}\n" +
101
				"}"
102
			);
103
			ICompilationUnit cuA = getCompilationUnit("P/A.java");
104
			CompilationUnit unitA = (CompilationUnit) runConversion(AST.JLS3, cuA, true, false, true);
105
			AbstractTypeDeclaration typeA = (AbstractTypeDeclaration)unitA.types().get(0);
106
			ITypeBinding objectType = typeA.resolveBinding().getSuperclass();
107
			assertTrue("'java.lang.Object' should be recovered!", objectType.isRecovered());
108
			assertEquals("Unexpected package for recovered 'java.lang.Object'", "java.lang", objectType .getPackage().getName());
109
			assertEquals("Unexpected qualified name for recovered 'java.lang.Object'", "java.lang.Object", objectType .getQualifiedName());
110
		} finally {
111
			deleteProject("P");
112
		}
113
	}
114
	public void testBug209510b() throws CoreException, IOException {
115
		try {
116
			createJavaProject("P", new String[] {""}, new String[0], "");
117
			createFile("P/A.java",
118
				"public class A {\n" +
119
				"	Object field;\n" +
120
				"}"
121
			);
122
			ICompilationUnit cuA = getCompilationUnit("P/A.java");
123
			CompilationUnit unitA = (CompilationUnit) runConversion(AST.JLS3, cuA, true, false, true);
124
			AbstractTypeDeclaration type = (AbstractTypeDeclaration)unitA.types().get(0);
125
			FieldDeclaration field = (FieldDeclaration) type.bodyDeclarations().get(0);
126
			Type fieldType = field.getType();
127
			ITypeBinding typeBinding = fieldType.resolveBinding();
128
			ITypeBinding arrayType = typeBinding.createArrayType(2);
129
			assertTrue("'java.lang.Object' should be recovered!", arrayType.isRecovered());
130
			assertNull("Unexpected package for recovered 'array of java.lang.Object'", arrayType .getPackage());
131
			assertEquals("Unexpected qualified name for recovered 'java.lang.Object'", "java.lang.Object[][]", arrayType .getQualifiedName());
132
		} finally {
133
			deleteProject("P");
134
		}
135
	}
136
	public void testBug209510c() throws CoreException, IOException {
137
		try {
138
			createJavaProject("P", new String[] {""}, new String[0], "");
139
			createFile("P/A.java",
140
				"public class A {\n" +
141
				"	Object[] array;\n" +
142
				"}"
143
			);
144
			ICompilationUnit cuA = getCompilationUnit("P/A.java");
145
			CompilationUnit unitA = (CompilationUnit) runConversion(AST.JLS3, cuA, true, false, true);
146
			AbstractTypeDeclaration type = (AbstractTypeDeclaration)unitA.types().get(0);
147
			FieldDeclaration field = (FieldDeclaration) type.bodyDeclarations().get(0);
148
			Type fieldType = field.getType();
149
			ITypeBinding arrayType = fieldType.resolveBinding();
150
			assertTrue("'java.lang.Object' should be recovered!", arrayType.isRecovered());
151
			assertNull("Unexpected package for recovered 'array of java.lang.Object'", arrayType .getPackage());
152
			assertEquals("Unexpected qualified name for recovered 'java.lang.Object'", "java.lang.Object[]", arrayType .getQualifiedName());
153
		} finally {
154
			deleteProject("P");
155
		}
156
	}
157
	
158
}
(-)dom/org/eclipse/jdt/core/dom/RecoveredTypeBinding.java (-26 / +67 lines)
Lines 15-22 Link Here
15
15
16
import org.eclipse.jdt.core.IJavaElement;
16
import org.eclipse.jdt.core.IJavaElement;
17
import org.eclipse.jdt.core.JavaModelException;
17
import org.eclipse.jdt.core.JavaModelException;
18
import org.eclipse.jdt.core.compiler.CharOperation;
18
import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
19
import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
20
import org.eclipse.jdt.internal.compiler.lookup.Binding;
19
import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
21
import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
22
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
20
import org.eclipse.jdt.internal.compiler.util.Util;
23
import org.eclipse.jdt.internal.compiler.util.Util;
21
import org.eclipse.jdt.internal.core.CompilationUnit;
24
import org.eclipse.jdt.internal.core.CompilationUnit;
22
import org.eclipse.jdt.internal.core.PackageFragment;
25
import org.eclipse.jdt.internal.core.PackageFragment;
Lines 32-38 Link Here
32
	private int dimensions;
35
	private int dimensions;
33
	private RecoveredTypeBinding innerTypeBinding;
36
	private RecoveredTypeBinding innerTypeBinding;
34
	private ITypeBinding[] typeArguments;
37
	private ITypeBinding[] typeArguments;
35
	private org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding;
38
	private org.eclipse.jdt.internal.compiler.lookup.TypeBinding binding;
36
39
37
	RecoveredTypeBinding(BindingResolver resolver, VariableDeclaration variableDeclaration) {
40
	RecoveredTypeBinding(BindingResolver resolver, VariableDeclaration variableDeclaration) {
38
		this.variableDeclaration = variableDeclaration;
41
		this.variableDeclaration = variableDeclaration;
Lines 44-53 Link Here
44
		}
47
		}
45
	}
48
	}
46
49
47
	RecoveredTypeBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding) {
50
	RecoveredTypeBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.TypeBinding typeBinding) {
48
		this.resolver = resolver;
51
		this.resolver = resolver;
49
		this.dimensions = referenceBinding.dimensions();
52
		this.dimensions = typeBinding.dimensions();
50
		this.referenceBinding = referenceBinding;
53
		this.binding = typeBinding;
51
	}
54
	}
52
55
53
	RecoveredTypeBinding(BindingResolver resolver, Type type) {
56
	RecoveredTypeBinding(BindingResolver resolver, Type type) {
Lines 147-158 Link Here
147
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getElementType()
150
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getElementType()
148
	 */
151
	 */
149
	public ITypeBinding getElementType() {
152
	public ITypeBinding getElementType() {
150
		if (this.referenceBinding != null) {
153
		if (this.binding != null) {
151
			if (this.referenceBinding.isArrayType()) {
154
			if (this.binding.isArrayType()) {
152
				ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding;
155
				ArrayBinding arrayBinding = (ArrayBinding) this.binding;
153
				return new RecoveredTypeBinding(this.resolver, arrayBinding.leafComponentType);
156
				return new RecoveredTypeBinding(this.resolver, arrayBinding.leafComponentType);
154
			} else {
157
			} else {
155
				return new RecoveredTypeBinding(this.resolver, this.referenceBinding);
158
				return new RecoveredTypeBinding(this.resolver, this.binding);
156
			}
159
			}
157
		}
160
		}
158
		if (this.innerTypeBinding != null) {
161
		if (this.innerTypeBinding != null) {
Lines 205-223 Link Here
205
	private String getInternalName() {
208
	private String getInternalName() {
206
		if (this.innerTypeBinding != null) {
209
		if (this.innerTypeBinding != null) {
207
			return this.innerTypeBinding.getInternalName();
210
			return this.innerTypeBinding.getInternalName();
208
		} else if (this.referenceBinding != null) {
211
		}
209
			org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding typeBinding = null;
212
		ReferenceBinding referenceBinding = getReferenceBinding();
210
			if (this.referenceBinding.isArrayType()) {
213
		if (referenceBinding != null) {
211
				ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding;
214
			return new String(referenceBinding.compoundName[referenceBinding.compoundName.length - 1]);
212
				if (arrayBinding.leafComponentType instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) {
213
					typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) arrayBinding.leafComponentType;
214
				}
215
			} else if (this.referenceBinding instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) {
216
				typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) this.referenceBinding;
217
			}
218
			if (typeBinding != null) {
219
				return new String(typeBinding.compoundName[typeBinding.compoundName.length - 1]);
220
			}
221
		}
215
		}
222
		return this.getTypeNameFrom(getType());
216
		return this.getTypeNameFrom(getType());
223
	}
217
	}
Lines 226-231 Link Here
226
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getPackage()
220
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getPackage()
227
	 */
221
	 */
228
	public IPackageBinding getPackage() {
222
	public IPackageBinding getPackage() {
223
		if (this.binding != null) {
224
			switch (this.binding.kind()) {
225
				case Binding.BASE_TYPE :
226
				case Binding.ARRAY_TYPE :
227
				case Binding.TYPE_PARAMETER : // includes capture scenario
228
				case Binding.WILDCARD_TYPE :
229
				case Binding.INTERSECTION_TYPE:
230
					return null;
231
			}
232
			IPackageBinding packageBinding = this.resolver.getPackageBinding(this.binding.getPackage());
233
			if (packageBinding != null) return packageBinding;
234
		}
235
		if (this.innerTypeBinding != null && this.dimensions > 0) {
236
			return null;
237
		}
229
		CompilationUnitScope scope = this.resolver.scope();
238
		CompilationUnitScope scope = this.resolver.scope();
230
		if (scope != null) {
239
		if (scope != null) {
231
			return this.resolver.getPackageBinding(scope.getCurrentPackage());
240
			return this.resolver.getPackageBinding(scope.getCurrentPackage());
Lines 237-249 Link Here
237
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getQualifiedName()
246
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getQualifiedName()
238
	 */
247
	 */
239
	public String getQualifiedName() {
248
	public String getQualifiedName() {
240
		return this.getName();
249
		ReferenceBinding referenceBinding = getReferenceBinding();
250
		if (referenceBinding != null) {
251
			StringBuffer buffer = new StringBuffer();
252
			char[] brackets = new char[this.dimensions * 2];
253
			for (int i = this.dimensions * 2 - 1; i >= 0; i -= 2) {
254
				brackets[i] = ']';
255
				brackets[i - 1] = '[';
256
			}
257
			buffer.append(CharOperation.toString(referenceBinding.compoundName));
258
			buffer.append(brackets);
259
			return String.valueOf(buffer);
260
		} else { 
261
			return getName();
262
		}
263
	}
264
265
	private ReferenceBinding getReferenceBinding() {
266
		if (this.binding != null) {
267
			if (this.binding.isArrayType()) {
268
				ArrayBinding arrayBinding = (ArrayBinding) this.binding;
269
				if (arrayBinding.leafComponentType instanceof ReferenceBinding) {
270
					return (ReferenceBinding) arrayBinding.leafComponentType;
271
				}
272
			} else if (this.binding instanceof ReferenceBinding) {
273
				return (ReferenceBinding) this.binding;
274
			}
275
		} else if (this.innerTypeBinding != null) {
276
			return this.innerTypeBinding.getReferenceBinding();
277
		}
278
		return null;
241
	}
279
	}
242
280
243
	/* (non-Javadoc)
281
	/* (non-Javadoc)
244
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getSuperclass()
282
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getSuperclass()
245
	 */
283
	 */
246
	public ITypeBinding getSuperclass() {
284
	public ITypeBinding getSuperclass() {
285
		if (getQualifiedName().equals("java.lang.Object")) {	//$NON-NLS-1$
286
			return null;
287
		}
247
		return this.resolver.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
288
		return this.resolver.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
248
	}
289
	}
249
290
Lines 251-257 Link Here
251
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeArguments()
292
	 * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeArguments()
252
	 */
293
	 */
253
	public ITypeBinding[] getTypeArguments() {
294
	public ITypeBinding[] getTypeArguments() {
254
		if (this.referenceBinding != null) {
295
		if (this.binding != null) {
255
			return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS;
296
			return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS;
256
		}
297
		}
257
		if (this.typeArguments != null) {
298
		if (this.typeArguments != null) {
Lines 523-531 Link Here
523
		} else if (this.currentType != null) {
564
		} else if (this.currentType != null) {
524
			buffer.append("currentType") //$NON-NLS-1$
565
			buffer.append("currentType") //$NON-NLS-1$
525
			      .append(this.currentType.toString());
566
			      .append(this.currentType.toString());
526
		} else if (this.referenceBinding != null) {
567
		} else if (this.binding != null) {
527
			buffer.append("referenceBinding") //$NON-NLS-1$
568
			buffer.append("typeBinding") //$NON-NLS-1$
528
				  .append(this.referenceBinding.computeUniqueKey());
569
				  .append(this.binding.computeUniqueKey());
529
		} else if (variableDeclaration != null) {
570
		} else if (variableDeclaration != null) {
530
			buffer
571
			buffer
531
				.append("variableDeclaration") //$NON-NLS-1$
572
				.append("variableDeclaration") //$NON-NLS-1$
(-)dom/org/eclipse/jdt/core/dom/ITypeBinding.java (-3 / +5 lines)
Lines 370-379 Link Here
370
370
371
	/**
371
	/**
372
	 * Returns the binding for the package in which this type is declared.
372
	 * Returns the binding for the package in which this type is declared.
373
	 * 
374
	 * <p>The package of a recovered type reference binding is the package of the
375
	 * enclosing type.</p>
376
	 *
373
	 *
374
	 * <p>The package of a recovered type reference binding is either
375
	 * the package of the enclosing type, or, if the type name is the name of a
376
	 * {@linkplain AST#resolveWellKnownType(String) well-known type},
377
	 * the package of the matching well-known type.</p>
378
	 * 
377
	 * @return the binding for the package in which this class, interface,
379
	 * @return the binding for the package in which this class, interface,
378
	 * enum, or annotation type is declared, or <code>null</code> if this type
380
	 * enum, or annotation type is declared, or <code>null</code> if this type
379
	 * binding represents a primitive type, an array type, the null type,
381
	 * binding represents a primitive type, an array type, the null type,

Return to bug 186410