KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > Argument


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.ASTVisitor;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class Argument extends LocalDeclaration {
19     
20     // prefix for setter method (to recognize special hiding argument)
21
private final static char[] SET = "set".toCharArray(); //$NON-NLS-1$
22

23     public Argument(char[] name, long posNom, TypeReference tr, int modifiers) {
24
25         super(name, (int) (posNom >>> 32), (int) posNom);
26         this.declarationSourceEnd = (int) posNom;
27         this.modifiers = modifiers;
28         type = tr;
29         this.bits |= IsLocalDeclarationReachable;
30     }
31
32     public void bind(MethodScope scope, TypeBinding typeBinding, boolean used) {
33
34         // record the resolved type into the type reference
35
Binding existingVariable = scope.getBinding(name, Binding.VARIABLE, this, false /*do not resolve hidden field*/);
36         if (existingVariable != null && existingVariable.isValidBinding()){
37             if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
38                 scope.problemReporter().redefineArgument(this);
39             } else {
40                 boolean isSpecialArgument = false;
41                 if (existingVariable instanceof FieldBinding) {
42                     if (scope.isInsideConstructor()) {
43                         isSpecialArgument = true; // constructor argument
44
} else {
45                         AbstractMethodDeclaration methodDecl = scope.referenceMethod();
46                         if (methodDecl != null && CharOperation.prefixEquals(SET, methodDecl.selector)) {
47                             isSpecialArgument = true; // setter argument
48
}
49                     }
50                 }
51                 scope.problemReporter().localVariableHiding(this, existingVariable, isSpecialArgument);
52             }
53         }
54
55         if (this.binding == null) {
56             this.binding = new LocalVariableBinding(this, typeBinding, this.modifiers, true);
57         }
58         scope.addLocalVariable(this.binding);
59         resolveAnnotations(scope, this.annotations, this.binding);
60         //true stand for argument instead of just local
61
this.binding.declaration = this;
62         this.binding.useFlag = used ? LocalVariableBinding.USED : LocalVariableBinding.UNUSED;
63     }
64
65     /**
66      * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
67      */

68     public int getKind() {
69         return PARAMETER;
70     }
71
72     public boolean isVarArgs() {
73         return this.type != null && (this.type.bits & IsVarArgs) != 0;
74     }
75         
76     public StringBuffer JavaDoc print(int indent, StringBuffer JavaDoc output) {
77
78         printIndent(indent, output);
79         printModifiers(this.modifiers, output);
80         if (this.annotations != null) printAnnotations(this.annotations, output);
81         
82         if (type == null) {
83             output.append("<no type> "); //$NON-NLS-1$
84
} else {
85             type.print(0, output).append(' ');
86         }
87         return output.append(this.name);
88     }
89
90     public StringBuffer JavaDoc printStatement(int indent, StringBuffer JavaDoc output) {
91
92         return print(indent, output).append(';');
93     }
94
95     public TypeBinding resolveForCatch(BlockScope scope) {
96
97         // resolution on an argument of a catch clause
98
// provide the scope with a side effect : insertion of a LOCAL
99
// that represents the argument. The type must be from JavaThrowable
100

101         TypeBinding exceptionType = this.type.resolveType(scope, true /* check bounds*/);
102         if (exceptionType == null) return null;
103         boolean hasError = false;
104         if (exceptionType.isBoundParameterizedType()) {
105             scope.problemReporter().invalidParameterizedExceptionType(exceptionType, this);
106             hasError = true;
107             // fall thru to create the variable - avoids additional errors because the variable is missing
108
}
109         if (exceptionType.isTypeVariable()) {
110             scope.problemReporter().invalidTypeVariableAsException(exceptionType, this);
111             hasError = true;
112             // fall thru to create the variable - avoids additional errors because the variable is missing
113
}
114         if (exceptionType.isArrayType() && ((ArrayBinding) exceptionType).leafComponentType == TypeBinding.VOID) {
115             scope.problemReporter().variableTypeCannotBeVoidArray(this);
116             hasError = true;
117             // fall thru to create the variable - avoids additional errors because the variable is missing
118
}
119         if (exceptionType.findSuperTypeErasingTo(TypeIds.T_JavaLangThrowable, true) == null) {
120             scope.problemReporter().cannotThrowType(this.type, exceptionType);
121             hasError = true;
122             // fall thru to create the variable - avoids additional errors because the variable is missing
123
}
124         
125         Binding existingVariable = scope.getBinding(name, Binding.VARIABLE, this, false /*do not resolve hidden field*/);
126         if (existingVariable != null && existingVariable.isValidBinding()){
127             if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
128                 scope.problemReporter().redefineArgument(this);
129             } else {
130                 scope.problemReporter().localVariableHiding(this, existingVariable, false);
131             }
132         }
133
134         this.binding = new LocalVariableBinding(this, exceptionType, modifiers, false); // argument decl, but local var (where isArgument = false)
135
resolveAnnotations(scope, this.annotations, this.binding);
136         
137         scope.addLocalVariable(binding);
138         binding.setConstant(Constant.NotAConstant);
139         if (hasError) return null;
140         return exceptionType;
141     }
142
143     public void traverse(ASTVisitor visitor, BlockScope scope) {
144         
145         if (visitor.visit(this, scope)) {
146             if (this.annotations != null) {
147                 int annotationsLength = this.annotations.length;
148                 for (int i = 0; i < annotationsLength; i++)
149                     this.annotations[i].traverse(visitor, scope);
150             }
151             if (type != null)
152                 type.traverse(visitor, scope);
153         }
154         visitor.endVisit(this, scope);
155     }
156     public void traverse(ASTVisitor visitor, ClassScope scope) {
157         
158         if (visitor.visit(this, scope)) {
159             if (this.annotations != null) {
160                 int annotationsLength = this.annotations.length;
161                 for (int i = 0; i < annotationsLength; i++)
162                     this.annotations[i].traverse(visitor, scope);
163             }
164             if (type != null)
165                 type.traverse(visitor, scope);
166         }
167         visitor.endVisit(this, scope);
168     }
169 }
170
Popular Tags