package org.eclipse.jdt.internal.compiler.lookup;
import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
public class ParameterizedMethodBinding extends MethodBinding {
protected MethodBinding originalMethod;
public ParameterizedMethodBinding(final ParameterizedTypeBinding parameterizedDeclaringClass, MethodBinding originalMethod) {
super(
originalMethod.modifiers,
originalMethod.selector,
originalMethod.returnType,
originalMethod.parameters,
originalMethod.thrownExceptions,
parameterizedDeclaringClass);
this.originalMethod = originalMethod;
this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
this.parameterNonNullness = originalMethod.parameterNonNullness;
this.defaultNullness = originalMethod.defaultNullness;
final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
Substitution substitution = null;
final int length = originalVariables.length;
final boolean isStatic = originalMethod.isStatic();
if (length == 0) {
this.typeVariables = Binding.NO_TYPE_VARIABLES;
if (!isStatic) substitution = parameterizedDeclaringClass;
} else {
final TypeVariableBinding[] substitutedVariables = new TypeVariableBinding[length];
for (int i = 0; i < length; i++) {
TypeVariableBinding originalVariable = originalVariables[i];
substitutedVariables[i] = new TypeVariableBinding(originalVariable.sourceName, this, originalVariable.rank, parameterizedDeclaringClass.environment);
substitutedVariables[i].tagBits |= (originalVariable.tagBits & (TagBits.AnnotationNullMASK|TagBits.HasNullTypeAnnotation));
}
this.typeVariables = substitutedVariables;
substitution = new Substitution() {
@Override
public LookupEnvironment environment() {
return parameterizedDeclaringClass.environment;
}
@Override
public boolean isRawSubstitution() {
return !isStatic && parameterizedDeclaringClass.isRawSubstitution();
}
@Override
public TypeBinding substitute(TypeVariableBinding typeVariable) {
if (typeVariable.rank < length && TypeBinding.equalsEquals(originalVariables[typeVariable.rank], typeVariable)) {
TypeBinding substitute = substitutedVariables[typeVariable.rank];
return typeVariable.hasTypeAnnotations() ? environment().createAnnotatedType(substitute, typeVariable.getTypeAnnotations()) : substitute;
}
if (!isStatic)
return parameterizedDeclaringClass.substitute(typeVariable);
return typeVariable;
}
};
for (int i = 0; i < length; i++) {
TypeVariableBinding originalVariable = originalVariables[i];
TypeVariableBinding substitutedVariable = substitutedVariables[i];
TypeBinding substitutedSuperclass = Scope.substitute(substitution, originalVariable.superclass);
ReferenceBinding[] substitutedInterfaces = Scope.substitute(substitution, originalVariable.superInterfaces);
if (originalVariable.firstBound != null) {
TypeBinding firstBound;
firstBound = TypeBinding.equalsEquals(originalVariable.firstBound, originalVariable.superclass)
? substitutedSuperclass
: substitutedInterfaces[0];
substitutedVariable.setFirstBound(firstBound);
}
switch (substitutedSuperclass.kind()) {
case Binding.ARRAY_TYPE :
substitutedVariable.setSuperClass(parameterizedDeclaringClass.environment.getResolvedJavaBaseType(TypeConstants.JAVA_LANG_OBJECT, null));
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
break;
default:
if (substitutedSuperclass.isInterface()) {
substitutedVariable.setSuperClass(parameterizedDeclaringClass.environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null));
int interfaceCount = substitutedInterfaces.length;
System.arraycopy(substitutedInterfaces, 0, substitutedInterfaces = new ReferenceBinding[interfaceCount+1], 1, interfaceCount);
substitutedInterfaces[0] = (ReferenceBinding) substitutedSuperclass;
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
} else {
substitutedVariable.setSuperClass((ReferenceBinding) substitutedSuperclass);
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
}
}
}
}
if (substitution != null) {
this.returnType = Scope.substitute(substitution, this.returnType);
this.parameters = Scope.substitute(substitution, this.parameters);
this.thrownExceptions = Scope.substitute(substitution, this.thrownExceptions);
if (this.thrownExceptions == null) this.thrownExceptions = Binding.NO_EXCEPTIONS;
if (parameterizedDeclaringClass.environment.globalOptions.isAnnotationBasedNullAnalysisEnabled) {
long returnNullBits = NullAnnotationMatching.validNullTagBits(this.returnType.tagBits);
if (returnNullBits != 0L) {
this.tagBits &= ~TagBits.AnnotationNullMASK;
this.tagBits |= returnNullBits;
}
int parametersLen = this.parameters.length;
for (int i=0; i<parametersLen; i++) {
long paramTagBits = NullAnnotationMatching.validNullTagBits(this.parameters[i].tagBits);
if (paramTagBits != 0) {
if (this.parameterNonNullness == null)
this.parameterNonNullness = new Boolean[parametersLen];
this.parameterNonNullness[i] = Boolean.valueOf(paramTagBits == TagBits.AnnotationNonNull);
}
}
}
}
checkMissingType: {
if ((this.tagBits & TagBits.HasMissingType) != 0)
break checkMissingType;
if ((this.returnType.tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
for (int i = 0, max = this.parameters.length; i < max; i++) {
if ((this.parameters[i].tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
}
for (int i = 0, max = this.thrownExceptions.length; i < max; i++) {
if ((this.thrownExceptions[i].tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
}
}
}
public ParameterizedMethodBinding(final ReferenceBinding declaringClass, MethodBinding originalMethod, char[][] alternateParamaterNames, final LookupEnvironment environment) {
super(
originalMethod.modifiers,
originalMethod.selector,
originalMethod.returnType,
originalMethod.parameters,
originalMethod.thrownExceptions,
declaringClass);
this.originalMethod = originalMethod;
this.tagBits = originalMethod.tagBits & ~TagBits.HasMissingType;
this.parameterNonNullness = originalMethod.parameterNonNullness;
this.defaultNullness = originalMethod.defaultNullness;
final TypeVariableBinding[] originalVariables = originalMethod.typeVariables;
Substitution substitution = null;
final int length = originalVariables.length;
if (length == 0) {
this.typeVariables = Binding.NO_TYPE_VARIABLES;
} else {
final TypeVariableBinding[] substitutedVariables = new TypeVariableBinding[length];
for (int i = 0; i < length; i++) {
TypeVariableBinding originalVariable = originalVariables[i];
substitutedVariables[i] = new TypeVariableBinding(
alternateParamaterNames == null ?
originalVariable.sourceName :
alternateParamaterNames[i],
this,
originalVariable.rank,
environment);
substitutedVariables[i].tagBits |= (originalVariable.tagBits & (TagBits.AnnotationNullMASK|TagBits.HasNullTypeAnnotation));
}
this.typeVariables = substitutedVariables;
substitution = new Substitution() {
@Override
public LookupEnvironment environment() {
return environment;
}
@Override
public boolean isRawSubstitution() {
return false;
}
@Override
public TypeBinding substitute(TypeVariableBinding typeVariable) {
if (typeVariable.rank < length && TypeBinding.equalsEquals(originalVariables[typeVariable.rank], typeVariable)) {
TypeBinding substitute = substitutedVariables[typeVariable.rank];
return typeVariable.hasTypeAnnotations() ? environment().createAnnotatedType(substitute, typeVariable.getTypeAnnotations()) : substitute;
}
return typeVariable;
}
};
for (int i = 0; i < length; i++) {
TypeVariableBinding originalVariable = originalVariables[i];
TypeVariableBinding substitutedVariable = substitutedVariables[i];
TypeBinding substitutedSuperclass = Scope.substitute(substitution, originalVariable.superclass);
ReferenceBinding[] substitutedInterfaces = Scope.substitute(substitution, originalVariable.superInterfaces);
if (originalVariable.firstBound != null) {
TypeBinding firstBound;
firstBound = TypeBinding.equalsEquals(originalVariable.firstBound, originalVariable.superclass)
? substitutedSuperclass
: substitutedInterfaces[0];
substitutedVariable.setFirstBound(firstBound);
}
switch (substitutedSuperclass.kind()) {
case Binding.ARRAY_TYPE :
substitutedVariable.setSuperClass(environment.getResolvedJavaBaseType(TypeConstants.JAVA_LANG_OBJECT, null));
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
break;
default:
if (substitutedSuperclass.isInterface()) {
substitutedVariable.setSuperClass(environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null));
int interfaceCount = substitutedInterfaces.length;
System.arraycopy(substitutedInterfaces, 0, substitutedInterfaces = new ReferenceBinding[interfaceCount+1], 1, interfaceCount);
substitutedInterfaces[0] = (ReferenceBinding) substitutedSuperclass;
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
} else {
substitutedVariable.setSuperClass((ReferenceBinding) substitutedSuperclass);
substitutedVariable.setSuperInterfaces(substitutedInterfaces);
}
}
}
}
if (substitution != null) {
this.returnType = Scope.substitute(substitution, this.returnType);
this.parameters = Scope.substitute(substitution, this.parameters);
this.thrownExceptions = Scope.substitute(substitution, this.thrownExceptions);
if (this.thrownExceptions == null) this.thrownExceptions = Binding.NO_EXCEPTIONS;
}
checkMissingType: {
if ((this.tagBits & TagBits.HasMissingType) != 0)
break checkMissingType;
if ((this.returnType.tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
for (int i = 0, max = this.parameters.length; i < max; i++) {
if ((this.parameters[i].tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
}
for (int i = 0, max = this.thrownExceptions.length; i < max; i++) {
if ((this.thrownExceptions[i].tagBits & TagBits.HasMissingType) != 0) {
this.tagBits |= TagBits.HasMissingType;
break checkMissingType;
}
}
}
}
public ParameterizedMethodBinding() {
}
public static ParameterizedMethodBinding instantiateGetClass(TypeBinding receiverType, MethodBinding originalMethod, Scope scope) {
ParameterizedMethodBinding method = new ParameterizedMethodBinding();
method.modifiers = originalMethod.modifiers;
method.selector = originalMethod.selector;
method.declaringClass = originalMethod.declaringClass;
method.typeVariables = Binding.NO_TYPE_VARIABLES;
method.originalMethod = originalMethod;
method.parameters = originalMethod.parameters;
method.thrownExceptions = originalMethod.thrownExceptions;
method.tagBits = originalMethod.tagBits;
ReferenceBinding genericClassType = scope.getJavaLangClass();
LookupEnvironment environment = scope.environment();
TypeBinding rawType = environment.convertToRawType(receiverType.erasure(), false );
if (environment.usesNullTypeAnnotations())
rawType = environment.createAnnotatedType(rawType, new AnnotationBinding[] { environment.getNonNullAnnotation() });
method.returnType = environment.createParameterizedType(
genericClassType,
new TypeBinding[] { environment.createWildcard(genericClassType, 0, rawType, null , Wildcard.EXTENDS) },
null);
if (environment.globalOptions.isAnnotationBasedNullAnalysisEnabled) {
if (environment.usesNullTypeAnnotations())
method.returnType = environment.createAnnotatedType(method.returnType, new AnnotationBinding[] { environment.getNonNullAnnotation() });
else
method.tagBits |= TagBits.AnnotationNonNull;
}
if ((method.returnType.tagBits & TagBits.HasMissingType) != 0) {
method.tagBits |= TagBits.HasMissingType;
}
return method;
}
@Override
public boolean hasSubstitutedParameters() {
return this.parameters != this.originalMethod.parameters;
}
@Override
public boolean hasSubstitutedReturnType() {
return this.returnType != this.originalMethod.returnType;
}
@Override
public MethodBinding original() {
return this.originalMethod.original();
}
@Override
public MethodBinding shallowOriginal() {
return this.originalMethod;
}
}