package org.eclipse.jdt.internal.compiler.lookup;
import java.util.Arrays;
import java.util.Set;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching;
import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching.CheckMode;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
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.jdt.internal.compiler.lookup.TypeConstants.BoundCheckStatus;
public class TypeVariableBinding extends ReferenceBinding {
public Binding declaringElement;
public int rank;
public TypeBinding firstBound;
public ReferenceBinding superclass;
public ReferenceBinding[] superInterfaces;
public char[] genericTypeSignature;
LookupEnvironment environment;
public TypeVariableBinding(char[] sourceName, Binding declaringElement, int rank, LookupEnvironment environment) {
this.sourceName = sourceName;
this.declaringElement = declaringElement;
this.rank = rank;
this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature;
this.tagBits |= TagBits.HasTypeVariable;
this.environment = environment;
this.typeBits = TypeIds.BitUninitialized;
computeId(environment);
}
protected TypeVariableBinding(char[] sourceName, LookupEnvironment environment) {
this.sourceName = sourceName;
this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature;
this.tagBits |= TagBits.HasTypeVariable;
this.environment = environment;
this.typeBits = TypeIds.BitUninitialized;
}
public TypeVariableBinding(TypeVariableBinding prototype) {
super(prototype);
this.declaringElement = prototype.declaringElement;
this.rank = prototype.rank;
this.firstBound = prototype.firstBound;
this.superclass = prototype.superclass;
if (prototype.superInterfaces != null) {
int len = prototype.superInterfaces.length;
if (len > 0)
System.arraycopy(prototype.superInterfaces, 0, this.superInterfaces = new ReferenceBinding[len], 0, len);
else
this.superInterfaces = Binding.NO_SUPERINTERFACES;
}
this.genericTypeSignature = prototype.genericTypeSignature;
this.environment = prototype.environment;
prototype.tagBits |= TagBits.HasAnnotatedVariants;
this.tagBits &= ~TagBits.HasAnnotatedVariants;
}
public TypeConstants.BoundCheckStatus boundCheck(Substitution substitution, TypeBinding argumentType, Scope scope, ASTNode location) {
TypeConstants.BoundCheckStatus code = internalBoundCheck(substitution, argumentType, scope, location);
if (code == BoundCheckStatus.MISMATCH) {
if (argumentType instanceof TypeVariableBinding && scope != null) {
TypeBinding bound = ((TypeVariableBinding)argumentType).firstBound;
if (bound instanceof ParameterizedTypeBinding) {
BoundCheckStatus code2 = boundCheck(substitution, bound.capture(scope, -1, -1), scope, location);
return code.betterOf(code2);
}
}
}
return code;
}
private TypeConstants.BoundCheckStatus internalBoundCheck(Substitution substitution, TypeBinding argumentType, Scope scope, ASTNode location) {
if (argumentType == TypeBinding.NULL || TypeBinding.equalsEquals(argumentType, this)) {
return BoundCheckStatus.OK;
}
boolean hasSubstitution = substitution != null;
if (!(argumentType instanceof ReferenceBinding || argumentType.isArrayType()))
return BoundCheckStatus.MISMATCH;
if (this.superclass == null)
return BoundCheckStatus.OK;
BoundCheckStatus nullStatus = BoundCheckStatus.OK;
boolean checkNullAnnotations = scope.environment().usesNullTypeAnnotations() && (location == null || (location.bits & ASTNode.InsideJavadoc) == 0);
if (argumentType.kind() == Binding.WILDCARD_TYPE) {
WildcardBinding wildcard = (WildcardBinding) argumentType;
switch(wildcard.boundKind) {
case Wildcard.EXTENDS :
boolean checkedAsOK = false;
TypeBinding wildcardBound = wildcard.bound;
if (TypeBinding.equalsEquals(wildcardBound, this))
checkedAsOK = true;
boolean isArrayBound = wildcardBound.isArrayType();
if (!wildcardBound.isInterface()) {
TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, this.superclass) : this.superclass;
if (!checkedAsOK) {
if (substitutedSuperType.id != TypeIds.T_JavaLangObject) {
if (isArrayBound) {
if (!wildcardBound.isCompatibleWith(substitutedSuperType, scope))
return BoundCheckStatus.MISMATCH;
} else {
TypeBinding match = wildcardBound.findSuperTypeOriginatingFrom(substitutedSuperType);
if (match != null) {
if (substitutedSuperType.isProvablyDistinct(match)) {
return BoundCheckStatus.MISMATCH;
}
} else {
match = substitutedSuperType.findSuperTypeOriginatingFrom(wildcardBound);
if (match != null) {
if (match.isProvablyDistinct(wildcardBound)) {
return BoundCheckStatus.MISMATCH;
}
} else {
if (denotesRelevantSuperClass(wildcardBound) && denotesRelevantSuperClass(substitutedSuperType)) {
return BoundCheckStatus.MISMATCH;
}
if (Scope.greaterLowerBound(new TypeBinding[] {substitutedSuperType, wildcardBound}, scope, this.environment) == null) {
return BoundCheckStatus.MISMATCH;
}
}
}
}
}
}
if (checkNullAnnotations && argumentType.hasNullTypeAnnotations()) {
nullStatus = nullBoundCheck(scope, argumentType, substitutedSuperType, substitution, location, nullStatus);
}
}
boolean mustImplement = isArrayBound || ((ReferenceBinding)wildcardBound).isFinal();
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, this.superInterfaces[i]) : this.superInterfaces[i];
if (!checkedAsOK) {
if (isArrayBound) {
if (!wildcardBound.isCompatibleWith(substitutedSuperType, scope))
return BoundCheckStatus.MISMATCH;
} else {
TypeBinding match = wildcardBound.findSuperTypeOriginatingFrom(substitutedSuperType);
if (match != null) {
if (substitutedSuperType.isProvablyDistinct(match)) {
return BoundCheckStatus.MISMATCH;
}
} else if (mustImplement) {
return BoundCheckStatus.MISMATCH;
}
}
}
if (checkNullAnnotations && argumentType.hasNullTypeAnnotations()) {
nullStatus = nullBoundCheck(scope, argumentType, substitutedSuperType, substitution, location, nullStatus);
}
}
if (nullStatus != null)
return nullStatus;
break;
case Wildcard.SUPER :
if (wildcard.bound.isTypeVariable() && ((TypeVariableBinding)wildcard.bound).superclass.id == TypeIds.T_JavaLangObject) {
return nullBoundCheck(scope, argumentType, null, substitution, location, nullStatus);
} else {
TypeBinding bound = wildcard.bound;
if (checkNullAnnotations && this.environment.containsNullTypeAnnotation(wildcard.typeAnnotations))
bound = this.environment.createAnnotatedType(bound.withoutToplevelNullAnnotation(), wildcard.getTypeAnnotations());
BoundCheckStatus status = boundCheck(substitution, bound, scope, null);
if (status == BoundCheckStatus.NULL_PROBLEM && location != null)
scope.problemReporter().nullityMismatchTypeArgument(this, wildcard, location);
return status;
}
case Wildcard.UNBOUND :
if (checkNullAnnotations && argumentType.hasNullTypeAnnotations()) {
return nullBoundCheck(scope, argumentType, null, substitution, location, nullStatus);
}
break;
}
return BoundCheckStatus.OK;
}
boolean unchecked = false;
if (this.superclass.id != TypeIds.T_JavaLangObject) {
TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, this.superclass) : this.superclass;
if (TypeBinding.notEquals(substitutedSuperType, argumentType)) {
if (!argumentType.isCompatibleWith(substitutedSuperType, scope)) {
return BoundCheckStatus.MISMATCH;
}
TypeBinding match = argumentType.findSuperTypeOriginatingFrom(substitutedSuperType);
if (match != null){
if (match.isRawType() && substitutedSuperType.isBoundParameterizedType())
unchecked = true;
}
}
if (checkNullAnnotations) {
nullStatus = nullBoundCheck(scope, argumentType, substitutedSuperType, substitution, location, nullStatus);
}
}
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, this.superInterfaces[i]) : this.superInterfaces[i];
if (TypeBinding.notEquals(substitutedSuperType, argumentType)) {
if (!argumentType.isCompatibleWith(substitutedSuperType, scope)) {
return BoundCheckStatus.MISMATCH;
}
TypeBinding match = argumentType.findSuperTypeOriginatingFrom(substitutedSuperType);
if (match != null){
if (match.isRawType() && substitutedSuperType.isBoundParameterizedType())
unchecked = true;
}
}
if (checkNullAnnotations) {
nullStatus = nullBoundCheck(scope, argumentType, substitutedSuperType, substitution, location, nullStatus);
}
}
if (checkNullAnnotations && nullStatus != BoundCheckStatus.NULL_PROBLEM) {
long nullBits = this.tagBits & TagBits.AnnotationNullMASK;
if (nullBits != 0 && nullBits != (argumentType.tagBits & TagBits.AnnotationNullMASK)) {
if (location != null)
scope.problemReporter().nullityMismatchTypeArgument(this, argumentType, location);
nullStatus = BoundCheckStatus.NULL_PROBLEM;
}
}
return unchecked ? BoundCheckStatus.UNCHECKED : nullStatus != null ? nullStatus : BoundCheckStatus.OK;
}
private BoundCheckStatus nullBoundCheck(Scope scope, TypeBinding argumentType, TypeBinding substitutedSuperType, Substitution substitution, ASTNode location, BoundCheckStatus previousStatus) {
if (NullAnnotationMatching.analyse(this, argumentType, substitutedSuperType, substitution, -1, null, CheckMode.BOUND_CHECK).isAnyMismatch()) {
if (location != null)
scope.problemReporter().nullityMismatchTypeArgument(this, argumentType, location);
return BoundCheckStatus.NULL_PROBLEM;
}
return previousStatus;
}
boolean denotesRelevantSuperClass(TypeBinding type) {
if (!type.isTypeVariable() && !type.isInterface() && type.id != TypeIds.T_JavaLangObject)
return true;
ReferenceBinding aSuperClass = type.superclass();
return aSuperClass != null && aSuperClass.id != TypeIds.T_JavaLangObject && !aSuperClass.isTypeVariable();
}
public int boundsCount() {
if (this.firstBound == null)
return 0;
if (this.firstBound.isInterface())
return this.superInterfaces.length;
return this.superInterfaces.length + 1;
}
@Override
public boolean canBeInstantiated() {
return false;
}
@Override
public void collectSubstitutes(Scope scope, TypeBinding actualType, InferenceContext inferenceContext, int constraint) {
if (this.declaringElement != inferenceContext.genericMethod) return;
switch (actualType.kind()) {
case Binding.BASE_TYPE :
if (actualType == TypeBinding.NULL) return;
TypeBinding boxedType = scope.environment().computeBoxingType(actualType);
if (boxedType == actualType) return;
actualType = boxedType;
break;
case Binding.POLY_TYPE:
case Binding.WILDCARD_TYPE :
return;
}
int variableConstraint;
switch(constraint) {
case TypeConstants.CONSTRAINT_EQUAL :
variableConstraint = TypeConstants.CONSTRAINT_EQUAL;
break;
case TypeConstants.CONSTRAINT_EXTENDS :
variableConstraint = TypeConstants.CONSTRAINT_SUPER;
break;
default:
variableConstraint =TypeConstants.CONSTRAINT_EXTENDS;
break;
}
inferenceContext.recordSubstitute(this, actualType, variableConstraint);
}
@Override
public char[] computeUniqueKey(boolean isLeaf) {
StringBuffer buffer = new StringBuffer();
Binding declaring = this.declaringElement;
if (!isLeaf && declaring.kind() == Binding.METHOD) {
MethodBinding methodBinding = (MethodBinding) declaring;
ReferenceBinding declaringClass = methodBinding.declaringClass;
buffer.append(declaringClass.computeUniqueKey(false));
buffer.append(':');
MethodBinding[] methods = declaringClass.methods();
if (methods != null)
for (int i = 0, length = methods.length; i < length; i++) {
MethodBinding binding = methods[i];
if (binding == methodBinding) {
buffer.append(i);
break;
}
}
} else {
buffer.append(declaring.computeUniqueKey(false));
buffer.append(':');
}
buffer.append(genericTypeSignature());
int length = buffer.length();
char[] uniqueKey = new char[length];
buffer.getChars(0, length, uniqueKey, 0);
return uniqueKey;
}
@Override
public char[] constantPoolName() {
if (this.firstBound != null) {
return this.firstBound.constantPoolName();
}
return this.superclass.constantPoolName();
}
@Override
public TypeBinding clone(TypeBinding enclosingType) {
return new TypeVariableBinding(this);
}
@Override
public String annotatedDebugName() {
StringBuffer buffer = new StringBuffer(10);
buffer.append(super.annotatedDebugName());
if (!this.inRecursiveFunction) {
this.inRecursiveFunction = true;
try {
if (this.superclass != null && TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
buffer.append(" extends ").append(this.superclass.annotatedDebugName());
}
if (this.superInterfaces != null && this.superInterfaces != Binding.NO_SUPERINTERFACES) {
if (TypeBinding.notEquals(this.firstBound, this.superclass)) {
buffer.append(" extends ");
}
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
if (i > 0 || TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
buffer.append(" & ");
}
buffer.append(this.superInterfaces[i].annotatedDebugName());
}
}
} finally {
this.inRecursiveFunction = false;
}
}
return buffer.toString();
}
@Override
public String debugName() {
if (this.hasTypeAnnotations())
return super.annotatedDebugName();
return new String(this.sourceName);
}
@Override
public TypeBinding erasure() {
if (this.firstBound != null) {
return this.firstBound.erasure();
}
return this.superclass;
}
public char[] genericSignature() {
StringBuffer sig = new StringBuffer(10);
sig.append(this.sourceName).append(':');
int interfaceLength = this.superInterfaces == null ? 0 : this.superInterfaces.length;
if (interfaceLength == 0 || TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
if (this.superclass != null)
sig.append(this.superclass.genericTypeSignature());
}
for (int i = 0; i < interfaceLength; i++) {
sig.append(':').append(this.superInterfaces[i].genericTypeSignature());
}
int sigLength = sig.length();
char[] genericSignature = new char[sigLength];
sig.getChars(0, sigLength, genericSignature, 0);
return genericSignature;
}
@Override
public char[] genericTypeSignature() {
if (this.genericTypeSignature != null) return this.genericTypeSignature;
return this.genericTypeSignature = CharOperation.concat('T', this.sourceName, ';');
}
TypeBound[] getTypeBounds(InferenceVariable variable, InferenceSubstitution theta) {
int n = boundsCount();
if (n == 0)
return NO_TYPE_BOUNDS;
TypeBound[] bounds = new TypeBound[n];
int idx = 0;
if (!this.firstBound.isInterface())
bounds[idx++] = TypeBound.createBoundOrDependency(theta, this.firstBound, variable);
for (int i = 0; i < this.superInterfaces.length; i++)
bounds[idx++] = TypeBound.createBoundOrDependency(theta, this.superInterfaces[i], variable);
return bounds;
}
boolean hasOnlyRawBounds() {
if (this.superclass != null && TypeBinding.equalsEquals(this.firstBound, this.superclass))
if (!this.superclass.isRawType())
return false;
if (this.superInterfaces != null)
for (int i = 0, l = this.superInterfaces.length; i < l; i++)
if (!this.superInterfaces[i].isRawType())
return false;
return true;
}
@Override
public boolean hasTypeBit(int bit) {
if (this.typeBits == TypeIds.BitUninitialized) {
this.typeBits = 0;
if (this.superclass != null && this.superclass.hasTypeBit(~TypeIds.BitUninitialized))
this.typeBits |= (this.superclass.typeBits & TypeIds.InheritableBits);
if (this.superInterfaces != null)
for (int i = 0, l = this.superInterfaces.length; i < l; i++)
if (this.superInterfaces[i].hasTypeBit(~TypeIds.BitUninitialized))
this.typeBits |= (this.superInterfaces[i].typeBits & TypeIds.InheritableBits);
}
return (this.typeBits & bit) != 0;
}
public boolean isErasureBoundTo(TypeBinding type) {
if (TypeBinding.equalsEquals(this.superclass.erasure(), type))
return true;
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
if (TypeBinding.equalsEquals(this.superInterfaces[i].erasure(), type))
return true;
}
return false;
}
@Override
public boolean isHierarchyConnected() {
return (this.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0;
}
public boolean isInterchangeableWith(TypeVariableBinding otherVariable, Substitution substitute) {
if (TypeBinding.equalsEquals(this, otherVariable))
return true;
int length = this.superInterfaces.length;
if (length != otherVariable.superInterfaces.length)
return false;
if (TypeBinding.notEquals(this.superclass, Scope.substitute(substitute, otherVariable.superclass)))
return false;
next : for (int i = 0; i < length; i++) {
TypeBinding superType = Scope.substitute(substitute, otherVariable.superInterfaces[i]);
for (int j = 0; j < length; j++)
if (TypeBinding.equalsEquals(superType, this.superInterfaces[j]))
continue next;
return false;
}
return true;
}
@Override
public boolean isSubtypeOf(TypeBinding other, boolean simulatingBugJDK8026527) {
if (isSubTypeOfRTL(other))
return true;
if (this.firstBound != null && this.firstBound.isSubtypeOf(other, simulatingBugJDK8026527))
return true;
if (this.superclass != null && this.superclass.isSubtypeOf(other, simulatingBugJDK8026527))
return true;
if (this.superInterfaces != null)
for (int i = 0, l = this.superInterfaces.length; i < l; i++)
if (this.superInterfaces[i].isSubtypeOf(other, false))
return true;
return other.id == TypeIds.T_JavaLangObject;
}
boolean inRecursiveFunction = false;
@Override
public boolean enterRecursiveFunction() {
if (this.inRecursiveFunction)
return false;
this.inRecursiveFunction = true;
return true;
}
@Override
public void exitRecursiveFunction() {
this.inRecursiveFunction = false;
}
boolean inRecursiveProjectionFunction = false;
public boolean enterRecursiveProjectionFunction() {
if (this.inRecursiveProjectionFunction)
return false;
this.inRecursiveProjectionFunction = true;
return true;
}
public void exitRecursiveProjectionFunction() {
this.inRecursiveProjectionFunction = false;
}
@Override
public boolean isProperType(boolean admitCapture18) {
if (this.inRecursiveFunction)
return true;
this.inRecursiveFunction = true;
try {
if (this.superclass != null && !this.superclass.isProperType(admitCapture18)) {
return false;
}
if (this.superInterfaces != null)
for (int i = 0, l = this.superInterfaces.length; i < l; i++)
if (!this.superInterfaces[i].isProperType(admitCapture18)) {
return false;
}
return true;
} finally {
this.inRecursiveFunction = false;
}
}
@Override
TypeBinding substituteInferenceVariable(InferenceVariable var, TypeBinding substituteType) {
if (this.inRecursiveFunction) return this;
this.inRecursiveFunction = true;
try {
boolean haveSubstitution = false;
ReferenceBinding currentSuperclass = this.superclass;
if (currentSuperclass != null) {
currentSuperclass = (ReferenceBinding) currentSuperclass.substituteInferenceVariable(var, substituteType);
haveSubstitution |= TypeBinding.notEquals(currentSuperclass, this.superclass);
}
ReferenceBinding[] currentSuperInterfaces = null;
if (this.superInterfaces != null) {
int length = this.superInterfaces.length;
if (haveSubstitution)
System.arraycopy(this.superInterfaces, 0, currentSuperInterfaces=new ReferenceBinding[length], 0, length);
for (int i = 0; i < length; i++) {
ReferenceBinding currentSuperInterface = this.superInterfaces[i];
if (currentSuperInterface != null) {
currentSuperInterface = (ReferenceBinding) currentSuperInterface.substituteInferenceVariable(var, substituteType);
if (TypeBinding.notEquals(currentSuperInterface, this.superInterfaces[i])) {
if (currentSuperInterfaces == null)
System.arraycopy(this.superInterfaces, 0, currentSuperInterfaces=new ReferenceBinding[length], 0, length);
currentSuperInterfaces[i] = currentSuperInterface;
haveSubstitution = true;
}
}
}
}
if (haveSubstitution) {
TypeVariableBinding newVar = new TypeVariableBinding(this.sourceName, this.declaringElement, this.rank, this.environment);
newVar.superclass = currentSuperclass;
newVar.superInterfaces = currentSuperInterfaces;
newVar.tagBits = this.tagBits;
return newVar;
}
return this;
} finally {
this.inRecursiveFunction = false;
}
}
@Override
public boolean isTypeVariable() {
return true;
}
@Override
public int kind() {
return Binding.TYPE_PARAMETER;
}
@Override
public boolean mentionsAny(TypeBinding[] parameters, int idx) {
if (this.inRecursiveFunction)
return false;
this.inRecursiveFunction = true;
try {
if (super.mentionsAny(parameters, idx))
return true;
if (this.superclass != null && this.superclass.mentionsAny(parameters, idx))
return true;
if (this.superInterfaces != null)
for (int j = 0; j < this.superInterfaces.length; j++) {
if (this.superInterfaces[j].mentionsAny(parameters, idx))
return true;
}
return false;
} finally {
this.inRecursiveFunction = false;
}
}
@Override
void collectInferenceVariables(Set<InferenceVariable> variables) {
if (this.inRecursiveFunction)
return;
this.inRecursiveFunction = true;
try {
if (this.superclass != null)
this.superclass.collectInferenceVariables(variables);
if (this.superInterfaces != null)
for (int j = 0; j < this.superInterfaces.length; j++) {
this.superInterfaces[j].collectInferenceVariables(variables);
}
} finally {
this.inRecursiveFunction = false;
}
}
public TypeBinding[] otherUpperBounds() {
if (this.firstBound == null)
return Binding.NO_TYPES;
if (TypeBinding.equalsEquals(this.firstBound, this.superclass))
return this.superInterfaces;
int otherLength = this.superInterfaces.length - 1;
if (otherLength > 0) {
TypeBinding[] otherBounds;
System.arraycopy(this.superInterfaces, 1, otherBounds = new TypeBinding[otherLength], 0, otherLength);
return otherBounds;
}
return Binding.NO_TYPES;
}
@Override
public char[] readableName() {
return this.sourceName;
}
ReferenceBinding resolve() {
if ((this.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0)
return this;
long nullTagBits = this.tagBits & TagBits.AnnotationNullMASK;
TypeBinding oldSuperclass = this.superclass, oldFirstInterface = null;
if (this.superclass != null) {
ReferenceBinding resolveType = (ReferenceBinding) BinaryTypeBinding.resolveType(this.superclass, this.environment, true );
this.tagBits |= resolveType.tagBits & TagBits.ContainsNestedTypeReferences;
long superNullTagBits = resolveType.tagBits & TagBits.AnnotationNullMASK;
if (superNullTagBits != 0L) {
if (nullTagBits == 0L) {
if ((superNullTagBits & TagBits.AnnotationNonNull) != 0) {
nullTagBits = superNullTagBits;
}
} else {
}
}
this.setSuperClass(resolveType);
}
ReferenceBinding[] interfaces = this.superInterfaces;
int length;
if ((length = interfaces.length) != 0) {
oldFirstInterface = interfaces[0];
for (int i = length; --i >= 0;) {
ReferenceBinding resolveType = (ReferenceBinding) BinaryTypeBinding.resolveType(interfaces[i], this.environment, true );
this.tagBits |= resolveType.tagBits & TagBits.ContainsNestedTypeReferences;
long superNullTagBits = resolveType.tagBits & TagBits.AnnotationNullMASK;
if (superNullTagBits != 0L) {
if (nullTagBits == 0L) {
if ((superNullTagBits & TagBits.AnnotationNonNull) != 0) {
nullTagBits = superNullTagBits;
}
} else {
}
}
interfaces[i] = resolveType;
}
}
if (nullTagBits != 0)
this.tagBits |= nullTagBits | TagBits.HasNullTypeAnnotation;
if (this.firstBound != null) {
if (TypeBinding.equalsEquals(this.firstBound, oldSuperclass)) {
this.setFirstBound(this.superclass);
} else if (TypeBinding.equalsEquals(this.firstBound, oldFirstInterface)) {
this.setFirstBound(interfaces[0]);
}
}
this.modifiers &= ~ExtraCompilerModifiers.AccUnresolved;
return this;
}
@Override
public void setTypeAnnotations(AnnotationBinding[] annotations, boolean evalNullAnnotations) {
if (getClass() == TypeVariableBinding.class) {
this.environment.typeSystem.forceRegisterAsDerived(this);
} else {
this.environment.getUnannotatedType(this);
}
super.setTypeAnnotations(annotations, evalNullAnnotations);
}
@Override
public char[] shortReadableName() {
return readableName();
}
@Override
public ReferenceBinding superclass() {
return this.superclass;
}
@Override
public ReferenceBinding[] superInterfaces() {
return this.superInterfaces;
}
@Override
public String toString() {
if (this.hasTypeAnnotations())
return annotatedDebugName();
StringBuffer buffer = new StringBuffer(10);
buffer.append('<').append(this.sourceName);
if (this.superclass != null && TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
buffer.append(" extends ").append(this.superclass.debugName());
}
if (this.superInterfaces != null && this.superInterfaces != Binding.NO_SUPERINTERFACES) {
if (TypeBinding.notEquals(this.firstBound, this.superclass)) {
buffer.append(" extends ");
}
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
if (i > 0 || TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
buffer.append(" & ");
}
buffer.append(this.superInterfaces[i].debugName());
}
}
buffer.append('>');
return buffer.toString();
}
@Override
public char[] nullAnnotatedReadableName(CompilerOptions options, boolean shortNames) {
StringBuffer nameBuffer = new StringBuffer(10);
appendNullAnnotation(nameBuffer, options);
nameBuffer.append(this.sourceName());
if (!this.inRecursiveFunction) {
this.inRecursiveFunction = true;
try {
if (this.superclass != null && TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
nameBuffer.append(" extends ").append(this.superclass.nullAnnotatedReadableName(options, shortNames));
}
if (this.superInterfaces != null && this.superInterfaces != Binding.NO_SUPERINTERFACES) {
if (TypeBinding.notEquals(this.firstBound, this.superclass)) {
nameBuffer.append(" extends ");
}
for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
if (i > 0 || TypeBinding.equalsEquals(this.firstBound, this.superclass)) {
nameBuffer.append(" & ");
}
nameBuffer.append(this.superInterfaces[i].nullAnnotatedReadableName(options, shortNames));
}
}
} finally {
this.inRecursiveFunction = false;
}
}
int nameLength = nameBuffer.length();
char[] readableName = new char[nameLength];
nameBuffer.getChars(0, nameLength, readableName, 0);
return readableName;
}
@Override
protected void appendNullAnnotation(StringBuffer nameBuffer, CompilerOptions options) {
int oldSize = nameBuffer.length();
super.appendNullAnnotation(nameBuffer, options);
if (oldSize == nameBuffer.length()) {
if (hasNullTypeAnnotations()) {
TypeVariableBinding[] typeVariables = null;
if (this.declaringElement instanceof ReferenceBinding) {
typeVariables = ((ReferenceBinding) this.declaringElement).typeVariables();
} else if (this.declaringElement instanceof MethodBinding) {
typeVariables = ((MethodBinding) this.declaringElement).typeVariables();
}
if (typeVariables != null && typeVariables.length > this.rank) {
TypeVariableBinding prototype = typeVariables[this.rank];
if (prototype != this)
prototype.appendNullAnnotation(nameBuffer, options);
}
}
}
}
@Override
public TypeBinding unannotated() {
return this.hasTypeAnnotations() ? this.environment.getUnannotatedType(this) : this;
}
@Override
public TypeBinding withoutToplevelNullAnnotation() {
if (!hasNullTypeAnnotations())
return this;
TypeBinding unannotated = this.environment.getUnannotatedType(this);
AnnotationBinding[] newAnnotations = this.environment.filterNullTypeAnnotations(this.typeAnnotations);
if (newAnnotations.length > 0)
return this.environment.createAnnotatedType(unannotated, newAnnotations);
return unannotated;
}
public TypeBinding upperBound() {
if (this.firstBound != null) {
return this.firstBound;
}
return this.superclass;
}
public TypeBinding[] allUpperBounds() {
if (this.superclass == null)
return this.superInterfaces;
if (this.superInterfaces == null || this.superInterfaces.length == 0)
return new TypeBinding[] { this.superclass };
int nInterfaces = this.superInterfaces.length;
TypeBinding[] all = Arrays.copyOf(this.superInterfaces, nInterfaces+1);
all[nInterfaces] = this.superclass;
return all;
}
public void evaluateNullAnnotations(Scope scope, TypeParameter parameter) {
long nullTagBits = NullAnnotationMatching.validNullTagBits(this.tagBits);
if (this.firstBound != null && this.firstBound.isValidBinding()) {
long superNullTagBits = NullAnnotationMatching.validNullTagBits(this.firstBound.tagBits);
if (superNullTagBits != 0L) {
if (nullTagBits == 0L) {
if ((superNullTagBits & TagBits.AnnotationNonNull) != 0) {
nullTagBits = superNullTagBits;
}
} else if (superNullTagBits != nullTagBits) {
if(parameter != null)
this.firstBound = nullMismatchOnBound(parameter, this.firstBound, superNullTagBits, nullTagBits, scope);
}
}
}
ReferenceBinding[] interfaces = this.superInterfaces;
int length;
if (interfaces != null && (length = interfaces.length) != 0) {
for (int i = length; --i >= 0;) {
ReferenceBinding resolveType = interfaces[i];
long superNullTagBits = NullAnnotationMatching.validNullTagBits(resolveType.tagBits);
if (superNullTagBits != 0L) {
if (nullTagBits == 0L) {
if ((superNullTagBits & TagBits.AnnotationNonNull) != 0) {
nullTagBits = superNullTagBits;
}
} else if (superNullTagBits != nullTagBits) {
if(parameter != null)
interfaces[i] = (ReferenceBinding) nullMismatchOnBound(parameter, resolveType, superNullTagBits, nullTagBits, scope);
}
}
}
}
if (nullTagBits != 0)
this.tagBits |= nullTagBits | TagBits.HasNullTypeAnnotation;
}
private TypeBinding nullMismatchOnBound(TypeParameter parameter, TypeBinding boundType, long superNullTagBits, long nullTagBits, Scope scope) {
TypeReference bound = findBound(boundType, parameter);
Annotation ann = bound.findAnnotation(superNullTagBits);
if (ann != null) {
scope.problemReporter().contradictoryNullAnnotationsOnBounds(ann, nullTagBits);
this.tagBits &= ~TagBits.AnnotationNullMASK;
} else {
return boundType.withoutToplevelNullAnnotation();
}
return boundType;
}
private TypeReference findBound(TypeBinding bound, TypeParameter parameter) {
if (parameter.type != null && TypeBinding.equalsEquals(parameter.type.resolvedType, bound))
return parameter.type;
TypeReference[] bounds = parameter.bounds;
if (bounds != null) {
for (int i = 0; i < bounds.length; i++) {
if (TypeBinding.equalsEquals(bounds[i].resolvedType, bound))
return bounds[i];
}
}
return null;
}
public TypeBinding setFirstBound(TypeBinding firstBound) {
this.firstBound = firstBound;
if ((this.tagBits & TagBits.HasAnnotatedVariants) != 0) {
TypeBinding [] annotatedTypes = getDerivedTypesForDeferredInitialization();
for (int i = 0, length = annotatedTypes == null ? 0 : annotatedTypes.length; i < length; i++) {
TypeVariableBinding annotatedType = (TypeVariableBinding) annotatedTypes[i];
annotatedType.firstBound = firstBound;
}
}
if (firstBound != null && firstBound.hasNullTypeAnnotations())
this.tagBits |= TagBits.HasNullTypeAnnotation;
return firstBound;
}
public ReferenceBinding setSuperClass(ReferenceBinding superclass) {
this.superclass = superclass;
if ((this.tagBits & TagBits.HasAnnotatedVariants) != 0) {
TypeBinding [] annotatedTypes = getDerivedTypesForDeferredInitialization();
for (int i = 0, length = annotatedTypes == null ? 0 : annotatedTypes.length; i < length; i++) {
TypeVariableBinding annotatedType = (TypeVariableBinding) annotatedTypes[i];
annotatedType.superclass = superclass;
}
}
return superclass;
}
public ReferenceBinding [] setSuperInterfaces(ReferenceBinding[] superInterfaces) {
this.superInterfaces = superInterfaces;
if ((this.tagBits & TagBits.HasAnnotatedVariants) != 0) {
TypeBinding [] annotatedTypes = getDerivedTypesForDeferredInitialization();
for (int i = 0, length = annotatedTypes == null ? 0 : annotatedTypes.length; i < length; i++) {
TypeVariableBinding annotatedType = (TypeVariableBinding) annotatedTypes[i];
annotatedType.superInterfaces = superInterfaces;
}
}
return superInterfaces;
}
protected TypeBinding[] getDerivedTypesForDeferredInitialization() {
return this.environment.getAnnotatedTypes(this);
}
public TypeBinding combineTypeAnnotations(TypeBinding substitute) {
if (hasTypeAnnotations()) {
if (hasRelevantTypeUseNullAnnotations()) {
substitute = substitute.withoutToplevelNullAnnotation();
}
if (this.typeAnnotations != Binding.NO_ANNOTATIONS)
return this.environment.createAnnotatedType(substitute, this.typeAnnotations);
}
return substitute;
}
private boolean hasRelevantTypeUseNullAnnotations() {
TypeVariableBinding[] parameters;
if (this.declaringElement instanceof ReferenceBinding) {
parameters = ((ReferenceBinding)this.declaringElement).original().typeVariables();
} else if (this.declaringElement instanceof MethodBinding) {
parameters = ((MethodBinding)this.declaringElement).original().typeVariables;
} else {
throw new IllegalStateException("Unexpected declaring element:"+String.valueOf(this.declaringElement.readableName()));
}
TypeVariableBinding parameter = parameters[this.rank];
long currentNullBits = this.tagBits & TagBits.AnnotationNullMASK;
long declarationNullBits = parameter.tagBits & TagBits.AnnotationNullMASK;
return (currentNullBits & ~declarationNullBits) != 0;
}
@Override
public boolean acceptsNonNullDefault() {
return false;
}
@Override
public long updateTagBits() {
if (!this.inRecursiveFunction) {
this.inRecursiveFunction = true;
try {
if (this.superclass != null)
this.tagBits |= this.superclass.updateTagBits();
if (this.superInterfaces != null)
for (TypeBinding superIfc : this.superInterfaces)
this.tagBits |= superIfc.updateTagBits();
} finally {
this.inRecursiveFunction = false;
}
}
return super.updateTagBits();
}
@Override
public boolean isFreeTypeVariable() {
return this.environment.usesNullTypeAnnotations()
&& this.environment.globalOptions.pessimisticNullAnalysisForFreeTypeVariablesEnabled
&& (this.tagBits & TagBits.AnnotationNullMASK) == 0;
}
@Override
public ReferenceBinding upwardsProjection(Scope scope, TypeBinding[] mentionedTypeVariables) {
return this;
}
@Override
public ReferenceBinding downwardsProjection(Scope scope, TypeBinding[] mentionedTypeVariables) {
return this;
}
}