added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / Argument.java
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
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.lookup.*;
16
17 public class Argument extends LocalDeclaration {
18         
19         // prefix for setter method (to recognize special hiding argument)
20         private final static char[] SET = "set".toCharArray(); //$NON-NLS-1$
21         
22         public Argument(char[] name, long posNom, TypeReference tr, int modifiers) {
23
24                 super(name, (int) (posNom >>> 32), (int) posNom);
25                 this.declarationSourceEnd = (int) posNom;
26                 this.modifiers = modifiers;
27                 type = tr;
28                 this.bits |= IsLocalDeclarationReachableMASK;
29         }
30
31         public void bind(MethodScope scope, TypeBinding typeBinding, boolean used) {
32
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;
37
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);
42                                 return;
43                         }
44                         boolean isSpecialArgument = false;
45                         if (existingVariable instanceof FieldBinding) {
46                                 if (scope.isInsideConstructor()) {
47                                         isSpecialArgument = true; // constructor argument
48                                 } else {
49                                         AbstractMethodDeclaration methodDecl = scope.referenceMethod();
50                                         if (methodDecl != null && CharOperation.prefixEquals(SET, methodDecl.selector)) {
51                                                 isSpecialArgument = true; // setter argument
52                                         }
53                                 }
54                         }
55                         scope.problemReporter().localVariableHiding(this, existingVariable, isSpecialArgument);
56                 }
57
58                 scope.addLocalVariable(
59                         this.binding =
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;
65         }
66
67         /**
68          * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
69          */
70         public int getKind() {
71                 return PARAMETER;
72         }
73
74         public boolean isVarArgs() {
75                 return (this.type.bits & IsVarArgs) != 0;
76         }
77                 
78         public StringBuffer print(int indent, StringBuffer output) {
79
80                 printIndent(indent, output);
81                 printModifiers(this.modifiers, output);
82                 if (this.annotations != null) printAnnotations(this.annotations, output);
83                 
84                 if (type == null) {
85                         output.append("<no type> "); //$NON-NLS-1$
86                 } else {
87                         type.print(0, output).append(' '); 
88                 }
89                 return output.append(this.name);
90         }
91
92         public StringBuffer printStatement(int indent, StringBuffer output) {
93
94                 return print(indent, output).append(';');
95         }       
96
97         public TypeBinding resolveForCatch(BlockScope scope) {
98
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
102
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);
107                         return null;
108                 }
109                 if (exceptionType.isTypeVariable()) {
110                         scope.problemReporter().invalidTypeVariableAsException(exceptionType, this);
111                         return null;
112                 }               
113                 TypeBinding throwable = scope.getJavaLangThrowable();
114                 if (!exceptionType.isCompatibleWith(throwable)) {
115                         scope.problemReporter().typeMismatchError(exceptionType, throwable, this);
116                         return null;
117                 }
118                 
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);
123                                 return null;
124                         }
125                         scope.problemReporter().localVariableHiding(this, existingVariable, false);
126                 }
127
128                 this.binding = new LocalVariableBinding(this, exceptionType, modifiers, false); // argument decl, but local var  (where isArgument = false)
129                 resolveAnnotations(scope, this.annotations, this.binding);
130                 
131                 scope.addLocalVariable(binding);
132                 binding.setConstant(NotAConstant);
133                 return exceptionType;
134         }
135
136         public void traverse(ASTVisitor visitor, BlockScope scope) {
137                 
138                 if (visitor.visit(this, scope)) {
139                         if (type != null)
140                                 type.traverse(visitor, scope);
141                         if (initialization != null)
142                                 initialization.traverse(visitor, scope);
143                 }
144                 visitor.endVisit(this, scope);
145         }
146 }