1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.ASTVisitor;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
17 public class Argument extends LocalDeclaration {
19 // prefix for setter method (to recognize special hiding argument)
20 private final static char[] SET = "set".toCharArray(); //$NON-NLS-1$
22 public Argument(char[] name, long posNom, TypeReference tr, int modifiers) {
24 super(name, (int) (posNom >>> 32), (int) posNom);
25 this.declarationSourceEnd = (int) posNom;
26 this.modifiers = modifiers;
28 this.bits |= IsLocalDeclarationReachableMASK;
31 public void bind(MethodScope scope, TypeBinding typeBinding, boolean used) {
33 if (this.type != null)
34 this.type.resolvedType = typeBinding; // TODO (philippe) no longer necessary as when binding got resolved, it was recorded already (SourceTypeBinding#resolveTypesFor(MethodBinding))
35 // record the resolved type into the type reference
36 int modifierFlag = this.modifiers;
38 Binding existingVariable = scope.getBinding(name, Binding.VARIABLE, this, false /*do not resolve hidden field*/);
39 if (existingVariable != null && existingVariable.isValidBinding()){
40 if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
41 scope.problemReporter().redefineArgument(this);
44 boolean isSpecialArgument = false;
45 if (existingVariable instanceof FieldBinding) {
46 if (scope.isInsideConstructor()) {
47 isSpecialArgument = true; // constructor argument
49 AbstractMethodDeclaration methodDecl = scope.referenceMethod();
50 if (methodDecl != null && CharOperation.prefixEquals(SET, methodDecl.selector)) {
51 isSpecialArgument = true; // setter argument
55 scope.problemReporter().localVariableHiding(this, existingVariable, isSpecialArgument);
58 scope.addLocalVariable(
60 new LocalVariableBinding(this, typeBinding, modifierFlag, true));
61 resolveAnnotations(scope, this.annotations, this.binding);
62 //true stand for argument instead of just local
63 this.binding.declaration = this;
64 this.binding.useFlag = used ? LocalVariableBinding.USED : LocalVariableBinding.UNUSED;
68 * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
70 public int getKind() {
74 public boolean isVarArgs() {
75 return (this.type.bits & IsVarArgs) != 0;
78 public StringBuffer print(int indent, StringBuffer output) {
80 printIndent(indent, output);
81 printModifiers(this.modifiers, output);
82 if (this.annotations != null) printAnnotations(this.annotations, output);
85 output.append("<no type> "); //$NON-NLS-1$
87 type.print(0, output).append(' ');
89 return output.append(this.name);
92 public StringBuffer printStatement(int indent, StringBuffer output) {
94 return print(indent, output).append(';');
97 public TypeBinding resolveForCatch(BlockScope scope) {
99 // resolution on an argument of a catch clause
100 // provide the scope with a side effect : insertion of a LOCAL
101 // that represents the argument. The type must be from JavaThrowable
103 TypeBinding exceptionType = this.type.resolveType(scope, true /* check bounds*/);
104 if (exceptionType == null) return null;
105 if (exceptionType.isGenericType() || exceptionType.isParameterizedType()) {
106 scope.problemReporter().invalidParameterizedExceptionType(exceptionType, this);
109 if (exceptionType.isTypeVariable()) {
110 scope.problemReporter().invalidTypeVariableAsException(exceptionType, this);
113 TypeBinding throwable = scope.getJavaLangThrowable();
114 if (!exceptionType.isCompatibleWith(throwable)) {
115 scope.problemReporter().typeMismatchError(exceptionType, throwable, this);
119 Binding existingVariable = scope.getBinding(name, Binding.VARIABLE, this, false /*do not resolve hidden field*/);
120 if (existingVariable != null && existingVariable.isValidBinding()){
121 if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
122 scope.problemReporter().redefineArgument(this);
125 scope.problemReporter().localVariableHiding(this, existingVariable, false);
128 this.binding = new LocalVariableBinding(this, exceptionType, modifiers, false); // argument decl, but local var (where isArgument = false)
129 resolveAnnotations(scope, this.annotations, this.binding);
131 scope.addLocalVariable(binding);
132 binding.setConstant(NotAConstant);
133 return exceptionType;
136 public void traverse(ASTVisitor visitor, BlockScope scope) {
138 if (visitor.visit(this, scope)) {
140 type.traverse(visitor, scope);
141 if (initialization != null)
142 initialization.traverse(visitor, scope);
144 visitor.endVisit(this, scope);