package org.eclipse.jdt.internal.compiler.ast;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.ast.TypeReference.AnnotationPosition;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.*;
public class Argument extends LocalDeclaration {
private final static char[] SET = "set".toCharArray();
public Argument(char[] name, long posNom, TypeReference tr, int modifiers) {
super(name, (int) (posNom >>> 32), (int) posNom);
this.declarationSourceEnd = (int) posNom;
this.modifiers = modifiers;
this.type = tr;
if (tr != null) {
this.bits |= (tr.bits & ASTNode.HasTypeAnnotations);
}
this.bits |= (IsLocalDeclarationReachable | IsArgument);
}
public Argument(char[] name, long posNom, TypeReference tr, int modifiers, boolean typeElided) {
super(name, (int) (posNom >>> 32), (int) posNom);
this.declarationSourceEnd = (int) posNom;
this.modifiers = modifiers;
this.type = tr;
if (tr != null) {
this.bits |= (tr.bits & ASTNode.HasTypeAnnotations);
}
this.bits |= (IsLocalDeclarationReachable | IsArgument | IsTypeElided);
}
@Override
public boolean isRecoveredFromLoneIdentifier() {
return false;
}
public TypeBinding createBinding(MethodScope scope, TypeBinding typeBinding) {
if (this.binding == null) {
this.binding = new LocalVariableBinding(this, typeBinding, this.modifiers, scope);
} else if (!this.binding.type.isValidBinding()) {
AbstractMethodDeclaration methodDecl = scope.referenceMethod();
if (methodDecl != null) {
MethodBinding methodBinding = methodDecl.binding;
if (methodBinding != null) {
methodBinding.tagBits |= TagBits.HasUnresolvedArguments;
}
}
}
if ((this.binding.tagBits & TagBits.AnnotationResolved) == 0) {
resolveAnnotations(scope, this.annotations, this.binding, true);
if (scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_8) {
Annotation.isTypeUseCompatible(this.type, scope, this.annotations);
scope.validateNullAnnotation(this.binding.tagBits, this.type, this.annotations);
}
}
this.binding.declaration = this;
return this.binding.type;
}
public TypeBinding bind(MethodScope scope, TypeBinding typeBinding, boolean used) {
TypeBinding newTypeBinding = createBinding(scope, typeBinding);
Binding existingVariable = scope.getBinding(this.name, Binding.VARIABLE, this, false );
if (existingVariable != null && existingVariable.isValidBinding()){
final boolean localExists = existingVariable instanceof LocalVariableBinding;
if (localExists && this.hiddenVariableDepth == 0) {
if ((this.bits & ASTNode.ShadowsOuterLocal) != 0 && scope.isLambdaSubscope()) {
scope.problemReporter().lambdaRedeclaresArgument(this);
} else {
scope.problemReporter().redefineArgument(this);
}
} else {
boolean isSpecialArgument = false;
if (existingVariable instanceof FieldBinding) {
if (scope.isInsideConstructor()) {
isSpecialArgument = true;
} else {
AbstractMethodDeclaration methodDecl = scope.referenceMethod();
if (methodDecl != null && CharOperation.prefixEquals(SET, methodDecl.selector)) {
isSpecialArgument = true;
}
}
}
scope.problemReporter().localVariableHiding(this, existingVariable, isSpecialArgument);
}
}
scope.addLocalVariable(this.binding);
this.binding.useFlag = used ? LocalVariableBinding.USED : LocalVariableBinding.UNUSED;
return newTypeBinding;
}
@Override
public int getKind() {
return (this.bits & ASTNode.IsArgument) != 0 ? PARAMETER : LOCAL_VARIABLE;
}
@Override
public boolean isArgument() {
return true;
}
public boolean isVarArgs() {
return this.type != null && (this.type.bits & IsVarArgs) != 0;
}
public boolean hasElidedType() {
return (this.bits & IsTypeElided) != 0;
}
public boolean hasNullTypeAnnotation(AnnotationPosition position) {
return TypeReference.containsNullAnnotation(this.annotations) ||
(this.type != null && this.type.hasNullTypeAnnotation(position));
}
@Override
public StringBuffer print(int indent, StringBuffer output) {
printIndent(indent, output);
printModifiers(this.modifiers, output);
if (this.annotations != null) {
printAnnotations(this.annotations, output);
output.append(' ');
}
if (this.type == null) {
output.append("<no type> ");
} else {
this.type.print(0, output).append(' ');
}
return output.append(this.name);
}
@Override
public StringBuffer printStatement(int indent, StringBuffer output) {
return print(indent, output).append(';');
}
public TypeBinding resolveForCatch(BlockScope scope) {
TypeBinding exceptionType = this.type.resolveType(scope, true );
boolean hasError;
if (exceptionType == null) {
hasError = true;
} else {
hasError = false;
switch(exceptionType.kind()) {
case Binding.PARAMETERIZED_TYPE :
if (exceptionType.isBoundParameterizedType()) {
hasError = true;
scope.problemReporter().invalidParameterizedExceptionType(exceptionType, this);
}
break;
case Binding.TYPE_PARAMETER :
scope.problemReporter().invalidTypeVariableAsException(exceptionType, this);
hasError = true;
break;
}
if (exceptionType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangThrowable, true) == null && exceptionType.isValidBinding()) {
scope.problemReporter().cannotThrowType(this.type, exceptionType);
hasError = true;
}
}
Binding existingVariable = scope.getBinding(this.name, Binding.VARIABLE, this, false );
if (existingVariable != null && existingVariable.isValidBinding()){
if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
scope.problemReporter().redefineArgument(this);
} else {
scope.problemReporter().localVariableHiding(this, existingVariable, false);
}
}
if ((this.type.bits & ASTNode.IsUnionType) != 0) {
this.binding = new CatchParameterBinding(this, exceptionType, this.modifiers | ClassFileConstants.AccFinal, false);
this.binding.tagBits |= TagBits.MultiCatchParameter;
} else {
this.binding = new CatchParameterBinding(this, exceptionType, this.modifiers, false);
}
resolveAnnotations(scope, this.annotations, this.binding, true);
Annotation.isTypeUseCompatible(this.type, scope, this.annotations);
if (scope.compilerOptions().isAnnotationBasedNullAnalysisEnabled &&
(this.type.hasNullTypeAnnotation(AnnotationPosition.ANY) || TypeReference.containsNullAnnotation(this.annotations)))
{
scope.problemReporter().nullAnnotationUnsupportedLocation(this.type);
}
scope.addLocalVariable(this.binding);
this.binding.setConstant(Constant.NotAConstant);
if (hasError) return null;
return exceptionType;
}
@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {
if (visitor.visit(this, scope)) {
if (this.annotations != null) {
int annotationsLength = this.annotations.length;
for (int i = 0; i < annotationsLength; i++)
this.annotations[i].traverse(visitor, scope);
}
if (this.type != null)
this.type.traverse(visitor, scope);
}
visitor.endVisit(this, scope);
}
public void traverse(ASTVisitor visitor, ClassScope scope) {
if (visitor.visit(this, scope)) {
if (this.annotations != null) {
int annotationsLength = this.annotations.length;
for (int i = 0; i < annotationsLength; i++)
this.annotations[i].traverse(visitor, scope);
}
if (this.type != null)
this.type.traverse(visitor, scope);
}
visitor.endVisit(this, scope);
}
}