Skip to main content
summaryrefslogtreecommitdiffstats
blob: 234355022f047276b9cbfb6e34403d600bfd1804 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Technical University Berlin - extended API and implementation
 *     Stephan Herrmann - Contribution for
 *								Bug 400874 - [1.8][compiler] Inference infrastructure should evolve to meet JLS8 18.x (Part G of JSR335 spec)
 *								Bug 429384 - [1.8][null] implement conformance rules for null-annotated lower / upper type bounds
 *								Bug 441797 - [1.8] synchronize type annotations on capture and its wildcard
 *								Bug 456497 - [1.8][null] during inference nullness from target type is lost against weaker hint from applicability analysis
 *								Bug 456924 - StackOverflowError during compilation
 *								Bug 462790 - [null] NPE in Expression.computeConversion()
 *     Jesper S Møller - Contributions for bug 381345 : [1.8] Take care of the Java 8 major version
 *								Bug 527554 - [18.3] Compiler support for JEP 286 Local-Variable Type
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.lookup;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.RoleTypeCreator;

public class CaptureBinding extends TypeVariableBinding {

	public TypeBinding lowerBound;
	public WildcardBinding wildcard;
	public int captureID;

	/* information to compute unique binding key */
	public ReferenceBinding sourceType;
	public int start;
	public int end;
	public ASTNode cud; // to facilitate recaptures.

	TypeBinding pendingSubstitute; // for substitution of recursive captures, see https://bugs.eclipse.org/456924

	public CaptureBinding(WildcardBinding wildcard, ReferenceBinding sourceType, int start, int end, ASTNode cud, int captureID) {
		super(TypeConstants.WILDCARD_CAPTURE_NAME_PREFIX, wildcard.environment);
		this.wildcard = wildcard;
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat capture as public
		this.fPackage = wildcard.fPackage;
		this.sourceType = sourceType;
		this.start = start;
		this.end = end;
		this.captureID = captureID;
		this.tagBits |= TagBits.HasCapturedWildcard;
		this.cud = cud;
		if (wildcard.hasTypeAnnotations()) {
			// register an unannoted version before adding the annotated wildcard:
			CaptureBinding unannotated = (CaptureBinding) clone(null);
			unannotated.wildcard = (WildcardBinding) this.wildcard.unannotated();
			this.environment.getUnannotatedType(unannotated);
			this.id = unannotated.id; // transfer fresh id
			// now register this annotated type:
			this.environment.typeSystem.cacheDerivedType(this, unannotated, this);
			// propagate from wildcard to capture - use super version, because our own method propagates type annotations in the opposite direction:
			super.setTypeAnnotations(wildcard.getTypeAnnotations(), wildcard.environment.globalOptions.isAnnotationBasedNullAnalysisEnabled);
			if (wildcard.hasNullTypeAnnotations())
				this.tagBits |= TagBits.HasNullTypeAnnotation;
		} else {
			computeId(this.environment);
			if(wildcard.hasNullTypeAnnotations()) {
				this.tagBits |= (wildcard.tagBits & TagBits.AnnotationNullMASK) | TagBits.HasNullTypeAnnotation;
			}
		}
	}
	
	// for subclass CaptureBinding18
	protected CaptureBinding(ReferenceBinding sourceType, char[] sourceName, int start, int end, int captureID, LookupEnvironment environment) {
		super(sourceName, null, 0, environment);
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat capture as public
		this.sourceType = sourceType;
		this.start = start;
		this.end = end;
		this.captureID = captureID;
	}

	public CaptureBinding(CaptureBinding prototype) {
		super(prototype);
		this.wildcard = prototype.wildcard;
		this.sourceType = prototype.sourceType;
		this.start = prototype.start;
		this.end = prototype.end;
		this.captureID = prototype.captureID;
		this.lowerBound = prototype.lowerBound;
		this.tagBits |= (prototype.tagBits & TagBits.HasCapturedWildcard);
		this.cud = prototype.cud;
	}
	
	// Captures may get cloned and annotated during type inference.
	public TypeBinding clone(TypeBinding enclosingType) {
		return new CaptureBinding(this);
	}

	/*
	 * sourceTypeKey ! wildcardKey position semi-colon
	 * p.X { capture of ? } --> !*123; (Lp/X; in declaring type except if leaf)
	 * p.X { capture of ? extends p.Y } --> !+Lp/Y;123; (Lp/X; in declaring type except if leaf)
	 */
	public char[] computeUniqueKey(boolean isLeaf) {
		StringBuffer buffer = new StringBuffer();
		if (isLeaf) {
			buffer.append(this.sourceType.computeUniqueKey(false/*not a leaf*/));
			buffer.append('&');
		}
		buffer.append(TypeConstants.WILDCARD_CAPTURE);
		buffer.append(this.wildcard.computeUniqueKey(false/*not a leaf*/));
		buffer.append(this.end);
		buffer.append(';');
		int length = buffer.length();
		char[] uniqueKey = new char[length];
		buffer.getChars(0, length, uniqueKey, 0);
		return uniqueKey;
	}

	public String debugName() {

		if (this.wildcard != null) {
			StringBuffer buffer = new StringBuffer(10);
			AnnotationBinding [] annotations = getTypeAnnotations();
			for (int i = 0, length = annotations == null ? 0 : annotations.length; i < length; i++) {
				buffer.append(annotations[i]);
				buffer.append(' ');
			}
			buffer
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_PREFIX)
				.append(this.captureID)
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_SUFFIX)
				.append(this.wildcard.debugName());
			return buffer.toString();
		}
		return super.debugName();
	}

	public char[] genericTypeSignature() {
		// captures have no signature per JVMS 4.7.9.1, approximate one by erasure:
		if (this.inRecursiveFunction) {
			// catch "capture#1 of X<capture#1 ...>":
			// prefer answering "Ljava.lang.Object;" instead of throwing StackOverflowError:
			return CharOperation.concat(new char[] {'L'}, CharOperation.concatWith(TypeConstants.JAVA_LANG_OBJECT, '.'), new char[] {';'});
		}
		this.inRecursiveFunction = true;
		try {
			return erasure().genericTypeSignature();
		} finally {
			this.inRecursiveFunction = false;
		}
	}

	/**
	 * Initialize capture bounds using substituted supertypes
	 * e.g. given X<U, V extends X<U, V>>,     capture(X<E,?>) = X<E,capture>, where capture extends X<E,capture>
	 */
	public void initializeBounds(Scope scope, ParameterizedTypeBinding capturedParameterizedType) {
		boolean is18plus = scope.compilerOptions().complianceLevel >= ClassFileConstants.JDK1_8;
		TypeVariableBinding wildcardVariable = this.wildcard.typeVariable();
		if (wildcardVariable == null) {
			// error resilience when capturing Zork<?>
			// no substitution for wildcard bound (only formal bounds from type variables are to be substituted: 104082)
			TypeBinding originalWildcardBound = this.wildcard.bound;
			switch (this.wildcard.boundKind) {
				case Wildcard.EXTENDS :
					// still need to capture bound supertype as well so as not to expose wildcards to the outside (111208)
					TypeBinding capturedWildcardBound = is18plus
							? originalWildcardBound // as spec'd
							: originalWildcardBound.capture(scope, this.start, this.end); // for compatibility with old behavior at 1.7-
					if (originalWildcardBound.isInterface()) {
						this.setSuperClass(scope.getJavaLangObject());
						this.setSuperInterfaces(new ReferenceBinding[] { (ReferenceBinding) capturedWildcardBound });
					} else {
						// the wildcard bound should be a subtype of variable superclass
						// it may occur that the bound is less specific, then consider glb (202404)
						if (capturedWildcardBound.isArrayType() || TypeBinding.equalsEquals(capturedWildcardBound, this)) {
							this.setSuperClass(scope.getJavaLangObject());
						} else {
							this.setSuperClass((ReferenceBinding) capturedWildcardBound);
						}
						this.setSuperInterfaces(Binding.NO_SUPERINTERFACES);
					}
					this.setFirstBound(capturedWildcardBound);
					if ((capturedWildcardBound.tagBits & TagBits.HasTypeVariable) == 0)
						this.tagBits &= ~TagBits.HasTypeVariable;
					break;
				case Wildcard.UNBOUND :
					this.setSuperClass(scope.getJavaLangObject());
					this.setSuperInterfaces(Binding.NO_SUPERINTERFACES);
					this.tagBits &= ~TagBits.HasTypeVariable;
					break;
				case Wildcard.SUPER :
					this.setSuperClass(scope.getJavaLangObject());
					this.setSuperInterfaces(Binding.NO_SUPERINTERFACES);
					this.lowerBound = this.wildcard.bound;
					if ((originalWildcardBound.tagBits & TagBits.HasTypeVariable) == 0)
						this.tagBits &= ~TagBits.HasTypeVariable;
					break;
			}
			return;
		}
		ReferenceBinding originalVariableSuperclass = wildcardVariable.superclass;
		ReferenceBinding substitutedVariableSuperclass = (ReferenceBinding) Scope.substitute(capturedParameterizedType, originalVariableSuperclass);
		// prevent cyclic capture: given X<T>, capture(X<? extends T> could yield a circular type
		if (TypeBinding.equalsEquals(substitutedVariableSuperclass, this)) substitutedVariableSuperclass = originalVariableSuperclass;

		ReferenceBinding[] originalVariableInterfaces = wildcardVariable.superInterfaces();
		ReferenceBinding[] substitutedVariableInterfaces = Scope.substitute(capturedParameterizedType, originalVariableInterfaces);
		if (substitutedVariableInterfaces != originalVariableInterfaces) {
			// prevent cyclic capture: given X<T>, capture(X<? extends T> could yield a circular type
			for (int i = 0, length = substitutedVariableInterfaces.length; i < length; i++) {
				if (TypeBinding.equalsEquals(substitutedVariableInterfaces[i], this)) substitutedVariableInterfaces[i] = originalVariableInterfaces[i];
			}
		}
		// no substitution for wildcard bound (only formal bounds from type variables are to be substituted: 104082)
		TypeBinding originalWildcardBound = this.wildcard.bound;

		switch (this.wildcard.boundKind) {
			case Wildcard.EXTENDS :
				// still need to capture bound supertype as well so as not to expose wildcards to the outside (111208)
				TypeBinding capturedWildcardBound = is18plus
							? originalWildcardBound // as spec'd
							: originalWildcardBound.capture(scope, this.start, this.end); // for compatibility with old behavior at 1.7-
//{ObjectTeams: is the bound a role type requiring wrapping?
				if (capturedWildcardBound.isRole())
					capturedWildcardBound = RoleTypeCreator.maybeWrapUnqualifiedRoleType(scope, capturedWildcardBound.enclosingType(), capturedWildcardBound, scope.methodScope().referenceMethod(), scope.problemReporter());
// SH}
				if (originalWildcardBound.isInterface()) {
					this.setSuperClass(substitutedVariableSuperclass);
					// merge wildcard bound into variable superinterfaces using glb
					if (substitutedVariableInterfaces == Binding.NO_SUPERINTERFACES) {
						this.setSuperInterfaces(new ReferenceBinding[] { (ReferenceBinding) capturedWildcardBound });
					} else {
						int length = substitutedVariableInterfaces.length;
						System.arraycopy(substitutedVariableInterfaces, 0, substitutedVariableInterfaces = new ReferenceBinding[length+1], 1, length);
						substitutedVariableInterfaces[0] =  (ReferenceBinding) capturedWildcardBound;
						this.setSuperInterfaces(Scope.greaterLowerBound(substitutedVariableInterfaces));
					}
				} else {
					// the wildcard bound should be a subtype of variable superclass
					// it may occur that the bound is less specific, then consider glb (202404)
					if (capturedWildcardBound.isArrayType() || TypeBinding.equalsEquals(capturedWildcardBound, this)) {
						this.setSuperClass(substitutedVariableSuperclass);
					} else {
						this.setSuperClass((ReferenceBinding) capturedWildcardBound);
						if (this.superclass.isSuperclassOf(substitutedVariableSuperclass)) {
							this.setSuperClass(substitutedVariableSuperclass);
						}
						// TODO: there are cases were we need to compute glb(capturedWildcardBound, substitutedVariableSuperclass)
						//       but then when glb (perhaps triggered inside setFirstBound()) fails, how to report the error??
					}
					this.setSuperInterfaces(substitutedVariableInterfaces);
				}
				this.setFirstBound(capturedWildcardBound);
				if ((capturedWildcardBound.tagBits & TagBits.HasTypeVariable) == 0)
					this.tagBits &= ~TagBits.HasTypeVariable;
				break;
			case Wildcard.UNBOUND :
				this.setSuperClass(substitutedVariableSuperclass);
				this.setSuperInterfaces(substitutedVariableInterfaces);
				this.tagBits &= ~TagBits.HasTypeVariable;
				break;
			case Wildcard.SUPER :
				this.setSuperClass(substitutedVariableSuperclass);
				if (TypeBinding.equalsEquals(wildcardVariable.firstBound, substitutedVariableSuperclass) || TypeBinding.equalsEquals(originalWildcardBound, substitutedVariableSuperclass)) {
					this.setFirstBound(substitutedVariableSuperclass);
				}
				this.setSuperInterfaces(substitutedVariableInterfaces);
				this.lowerBound = originalWildcardBound;
				if ((originalWildcardBound.tagBits & TagBits.HasTypeVariable) == 0)
					this.tagBits &= ~TagBits.HasTypeVariable;
				break;
		}
		if(scope.environment().usesNullTypeAnnotations()) {
			evaluateNullAnnotations(scope, null);
		}
	}

	/**
	 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#isCapture()
	 */
	public boolean isCapture() {
		return true;
	}

	/**
	 * @see TypeBinding#isEquivalentTo(TypeBinding)
	 */
	public boolean isEquivalentTo(TypeBinding otherType) {
	    if (equalsEquals(this, otherType)) return true;
	    if (otherType == null) return false;
		// capture of ? extends X[]
		if (this.firstBound != null && this.firstBound.isArrayType()) {
			if (this.firstBound.isCompatibleWith(otherType))
				return true;
		}
		switch (otherType.kind()) {
			case Binding.WILDCARD_TYPE :
			case Binding.INTERSECTION_TYPE :
				return ((WildcardBinding) otherType).boundCheck(this);
		}
		return false;
	}

	@Override
	public boolean isProperType(boolean admitCapture18) {
		if (this.lowerBound != null && !this.lowerBound.isProperType(admitCapture18))
			return false;
		if (this.wildcard != null && !this.wildcard.isProperType(admitCapture18))
			return false;
		return super.isProperType(admitCapture18);
	}

	public char[] readableName() {
		if (this.wildcard != null) {
			StringBuffer buffer = new StringBuffer(10);
			buffer
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_PREFIX)
				.append(this.captureID)
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_SUFFIX)
				.append(this.wildcard.readableName());
			int length = buffer.length();
			char[] name = new char[length];
			buffer.getChars(0, length, name, 0);
			return name;
		}
		return super.readableName();
	}
	
	public char[] signableName() {
		if (this.wildcard != null) {
			StringBuffer buffer = new StringBuffer(10);
			buffer
				.append(TypeConstants.WILDCARD_CAPTURE_SIGNABLE_NAME_SUFFIX)
				.append(this.wildcard.readableName());
			int length = buffer.length();
			char[] name = new char[length];
			buffer.getChars(0, length, name, 0);
			return name;
		}
		return super.readableName();
	}

	public char[] shortReadableName() {
		if (this.wildcard != null) {
			StringBuffer buffer = new StringBuffer(10);
			buffer
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_PREFIX)
				.append(this.captureID)
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_SUFFIX)
				.append(this.wildcard.shortReadableName());
			int length = buffer.length();
			char[] name = new char[length];
			buffer.getChars(0, length, name, 0);
			return name;
		}
		return super.shortReadableName();
	}
	
	@Override
	public char[] nullAnnotatedReadableName(CompilerOptions options, boolean shortNames) {
	    StringBuffer nameBuffer = new StringBuffer(10);
		appendNullAnnotation(nameBuffer, options);
		nameBuffer.append(this.sourceName());
		if (!this.inRecursiveFunction) { // CaptureBinding18 can be recursive indeed
			this.inRecursiveFunction = true;
			try {
				if (this.wildcard != null) {
					nameBuffer.append("of "); //$NON-NLS-1$
					nameBuffer.append(this.wildcard.withoutToplevelNullAnnotation().nullAnnotatedReadableName(options, shortNames));
				} else if (this.lowerBound != null) {
					nameBuffer.append(" super "); //$NON-NLS-1$
					nameBuffer.append(this.lowerBound.nullAnnotatedReadableName(options, shortNames));
				} else if (this.firstBound != null) {
					nameBuffer.append(" extends "); //$NON-NLS-1$
					nameBuffer.append(this.firstBound.nullAnnotatedReadableName(options, shortNames));
					TypeBinding[] otherUpperBounds = this.otherUpperBounds();
					if (otherUpperBounds != NO_TYPES)
						nameBuffer.append(" & ..."); //$NON-NLS-1$ // only hint at more bounds, we currently don't evaluate null annotations on otherUpperBounds
				}
			} finally {
				this.inRecursiveFunction = false;
			}
		}
		int nameLength = nameBuffer.length();
		char[] readableName = new char[nameLength];
		nameBuffer.getChars(0, nameLength, readableName, 0);
	    return readableName;
	}

	@Override
	public TypeBinding withoutToplevelNullAnnotation() {
		if (!hasNullTypeAnnotations())
			return this;
		if (this.wildcard != null && this.wildcard.hasNullTypeAnnotations()) {
			WildcardBinding newWildcard = (WildcardBinding) this.wildcard.withoutToplevelNullAnnotation();
			if (newWildcard != this.wildcard) { //$IDENTITY-COMPARISON$	
				
				CaptureBinding newCapture = (CaptureBinding) this.environment.getUnannotatedType(this).clone(null);
				if (newWildcard.hasTypeAnnotations())
					newCapture.tagBits |= TagBits.HasTypeAnnotations;
				newCapture.wildcard = newWildcard;
				
				// manually transfer the following two, because we are not in a context where we can call initializeBounds():
				newCapture.superclass = this.superclass;
				newCapture.superInterfaces = this.superInterfaces;

				AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
				return this.environment.createAnnotatedType(newCapture, newAnnotations);
			}
		}
		return super.withoutToplevelNullAnnotation();
	}

	@Override
//{ObjectTeams: cross the OT package, make protected:
	protected
// SH}
	TypeBinding substituteInferenceVariable(InferenceVariable var, TypeBinding substituteType) {
		if (this.pendingSubstitute != null)
			return this.pendingSubstitute;
		try {
			TypeBinding substitutedWildcard = this.wildcard.substituteInferenceVariable(var, substituteType);
			if (substitutedWildcard != this.wildcard) {  //$IDENTITY-COMPARISON$
				CaptureBinding substitute = (CaptureBinding) clone(enclosingType());
			    substitute.wildcard = (WildcardBinding) substitutedWildcard;
			    this.pendingSubstitute = substitute;
			    if (this.lowerBound != null)
			    	substitute.lowerBound = this.lowerBound.substituteInferenceVariable(var, substituteType);
			    if (this.firstBound != null)
			    	substitute.firstBound = this.firstBound.substituteInferenceVariable(var, substituteType);
			    if (this.superclass != null)
			    	substitute.superclass = (ReferenceBinding) this.superclass.substituteInferenceVariable(var, substituteType);
			    if (this.superInterfaces != null) {
			    	int length = this.superInterfaces.length;
			    	substitute.superInterfaces = new ReferenceBinding[length];
			    	for (int i = 0; i < length; i++)
			    		substitute.superInterfaces[i] = (ReferenceBinding) this.superInterfaces[i].substituteInferenceVariable(var, substituteType);
			    }
			    return substitute;
			}
			return this;
		} finally {
			this.pendingSubstitute = null;
		}
	}
	
	@Override
	public void setTypeAnnotations(AnnotationBinding[] annotations, boolean evalNullAnnotations) {
		super.setTypeAnnotations(annotations, evalNullAnnotations);
		if (annotations != Binding.NO_ANNOTATIONS && this.wildcard != null) {
			// keep annotations in sync, propagate from capture to its wildcard:
			this.wildcard = (WildcardBinding) this.wildcard.environment.createAnnotatedType(this.wildcard, annotations);
		}
	}

	@Override
	public TypeBinding uncapture(Scope scope) {
		return this.wildcard;
	}

	public ReferenceBinding downwardsProjection(Scope scope, TypeBinding[] mentionedTypeVariables) {
		ReferenceBinding result = null;
		if (enterRecursiveProjectionFunction()) {
			for (int i = 0; i < mentionedTypeVariables.length; ++i) {
				if (TypeBinding.equalsEquals(this, mentionedTypeVariables[i])) {
					if (this.lowerBound != null) {
						result = (ReferenceBinding) this.lowerBound.downwardsProjection(scope, mentionedTypeVariables);
					}
					break;
				}
			}
			exitRecursiveProjectionFunction();
		}
		return result;
	}

	/*
	 * CaptureBinding needs even more propagation, because we are creating a naked type
	 * (during CaptureBinding(WildcardBinding,ReferenceBinding,int,int,ASTNode,int)
	 * that has no firstBound / superclass / superInterfaces set.
	 */
	@Override
	protected TypeBinding[] getDerivedTypesForDeferredInitialization() {
		TypeBinding[] derived = this.environment.typeSystem.getDerivedTypes(this);
		if (derived.length > 0) {
			int count = 0;
			for (int i = 0; i < derived.length; i++) {
				if (derived[i] != null && derived[i].id == this.id)
					derived[count++] = derived[i];
			}
			if (count < derived.length)
				System.arraycopy(derived, 0, derived = new TypeBinding[count], 0, count);
		}
		return derived;
	}

	public String toString() {
		if (this.wildcard != null) {
			StringBuffer buffer = new StringBuffer(10);
			AnnotationBinding [] annotations = getTypeAnnotations();
			for (int i = 0, length = annotations == null ? 0 : annotations.length; i < length; i++) {
				buffer.append(annotations[i]);
				buffer.append(' ');
			}
			buffer
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_PREFIX)
				.append(this.captureID)
				.append(TypeConstants.WILDCARD_CAPTURE_NAME_SUFFIX)
				.append(this.wildcard);
			return buffer.toString();
		}
		return super.toString();
	}

//{ObjectTeams: wrapping of role types in various fields of this capture:
	public TypeBinding maybeWrapQualifiedRoleType(Scope scope, Expression anchorExpr, ASTNode typedNode, TypeBinding originalBinding) {
		boolean hasWrapped = false;
		if (this.inRecursiveFunction)
			return originalBinding;

		this.inRecursiveFunction = true;
		try {
	    	TypeBinding wrappedBound = RoleTypeCreator.maybeWrapQualifiedRoleType(scope, anchorExpr, this.firstBound, typedNode);
	    	if (TypeBinding.notEquals(wrappedBound, this.firstBound))
	        	hasWrapped = true;
	        
	    	TypeBinding newSuper = RoleTypeCreator.maybeWrapQualifiedRoleType(scope, anchorExpr, this.superclass, typedNode);
	        if (TypeBinding.notEquals(newSuper, this.superclass))
				hasWrapped = true;
	        
	        ReferenceBinding[] newSuperIfcs = new ReferenceBinding[this.superInterfaces.length]; 
	    	for (int i=0; i<this.superInterfaces.length; i++) {
	            newSuperIfcs[i] = (ReferenceBinding) RoleTypeCreator.maybeWrapQualifiedRoleType(scope, anchorExpr, this.superInterfaces[i], typedNode);
	            if (TypeBinding.notEquals(newSuperIfcs[i], this.superInterfaces[i]))
	            	hasWrapped = true;
	    	}
	    	if (hasWrapped) {
	    		CaptureBinding newCapture = scope.environment().createCapturedWildcard(this.wildcard, this.sourceType, this.start, this.end, this.cud, scope.compilationUnitScope().nextCaptureID());
	    		newCapture.firstBound = wrappedBound;
	    		newCapture.superclass = (ReferenceBinding)newSuper;
	    		newCapture.superInterfaces = newSuperIfcs;
	    		int dimensions = originalBinding.dimensions();
	    		if (dimensions > 0)
					return this.environment.createArrayType(newCapture, dimensions);
	    		return newCapture;
	    	}
	    	return originalBinding; // may be an ArrayBinding with this as leafComponentType
		} finally {
			this.inRecursiveFunction = false;
		}
	}
// SH}
}

Back to the top