1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
16 import net.sourceforge.phpdt.internal.compiler.lookup.Binding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.ElementValuePair;
19 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
24 * MemberValuePair node
26 public class MemberValuePair extends ASTNode {
29 public Expression value;
30 public MethodBinding binding;
32 * The representation of this pair in the type system.
34 public ElementValuePair compilerElementPair = null;
36 public MemberValuePair(char[] token, int sourceStart, int sourceEnd, Expression value) {
38 this.sourceStart = sourceStart;
39 this.sourceEnd = sourceEnd;
41 if (value instanceof ArrayInitializer) {
42 value.bits |= IsAnnotationDefaultValue;
47 * @see org.eclipse.jdt.internal.compiler.ast.ASTNode#print(int, java.lang.StringBuffer)
49 public StringBuffer print(int indent, StringBuffer output) {
52 .append(" = "); //$NON-NLS-1$
53 value.print(0, output);
57 public void resolveTypeExpecting(BlockScope scope, TypeBinding requiredType) {
59 if (this.value == null) {
60 this.compilerElementPair = new ElementValuePair(this.name, this.value, this.binding);
63 if (requiredType == null) {
64 // fault tolerance: keep resolving
65 if (this.value instanceof ArrayInitializer) {
66 this.value.resolveTypeExpecting(scope, null);
68 this.value.resolveType(scope);
70 this.compilerElementPair = new ElementValuePair(this.name, this.value, this.binding);
74 this.value.setExpectedType(requiredType); // needed in case of generic method invocation
75 TypeBinding valueType;
76 if (this.value instanceof ArrayInitializer) {
77 ArrayInitializer initializer = (ArrayInitializer) this.value;
78 valueType = initializer.resolveTypeExpecting(scope, this.binding.returnType);
79 } else if (this.value instanceof ArrayAllocationExpression) {
80 scope.problemReporter().annotationValueMustBeArrayInitializer(this.binding.declaringClass, this.name, this.value);
81 this.value.resolveType(scope);
82 valueType = null; // no need to pursue
84 valueType = this.value.resolveType(scope);
86 this.compilerElementPair = new ElementValuePair(this.name, this.value, this.binding);
87 if (valueType == null)
90 TypeBinding leafType = requiredType.leafComponentType();
91 if (!((this.value.isConstantValueOfTypeAssignableToType(valueType, requiredType)
92 || (requiredType.isBaseType() && BaseTypeBinding.isWidening(requiredType.id, valueType.id)))
93 || valueType.isCompatibleWith(requiredType))) {
95 if (!(requiredType.isArrayType()
96 && requiredType.dimensions() == 1
97 && (this.value.isConstantValueOfTypeAssignableToType(valueType, leafType)
98 || (leafType.isBaseType() && BaseTypeBinding.isWidening(leafType.id, valueType.id)))
99 || valueType.isCompatibleWith(leafType))) {
101 if (leafType.isAnnotationType() && !valueType.isAnnotationType()) {
102 scope.problemReporter().annotationValueMustBeAnnotation(this.binding.declaringClass, this.name, this.value, leafType);
104 scope.problemReporter().typeMismatchError(valueType, requiredType, this.value, null);
106 return; // may allow to proceed to find more errors at once
109 scope.compilationUnitScope().recordTypeConversion(requiredType.leafComponentType(), valueType.leafComponentType());
110 this.value.computeConversion(scope, requiredType, valueType);
113 // annotation methods can only return base types, String, Class, enum type, annotation types and arrays of these
114 checkAnnotationMethodType: {
115 switch (leafType.erasure().id) {
124 case T_JavaLangString :
125 if (this.value instanceof ArrayInitializer) {
126 ArrayInitializer initializer = (ArrayInitializer) this.value;
127 final Expression[] expressions = initializer.expressions;
128 if (expressions != null) {
129 for (int i =0, max = expressions.length; i < max; i++) {
130 Expression expression = expressions[i];
131 if (expression.resolvedType == null) continue; // fault-tolerance
132 if (expression.constant == Constant.NotAConstant) {
133 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, expressions[i], false);
137 } else if (this.value.constant == Constant.NotAConstant) {
138 if (valueType.isArrayType()) {
139 scope.problemReporter().annotationValueMustBeArrayInitializer(this.binding.declaringClass, this.name, this.value);
141 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, this.value, false);
144 break checkAnnotationMethodType;
145 case T_JavaLangClass :
146 if (this.value instanceof ArrayInitializer) {
147 ArrayInitializer initializer = (ArrayInitializer) this.value;
148 final Expression[] expressions = initializer.expressions;
149 if (expressions != null) {
150 for (int i =0, max = expressions.length; i < max; i++) {
151 Expression currentExpression = expressions[i];
152 if (!(currentExpression instanceof ClassLiteralAccess)) {
153 scope.problemReporter().annotationValueMustBeClassLiteral(this.binding.declaringClass, this.name, currentExpression);
157 } else if (!(this.value instanceof ClassLiteralAccess)) {
158 scope.problemReporter().annotationValueMustBeClassLiteral(this.binding.declaringClass, this.name, this.value);
160 break checkAnnotationMethodType;
162 if (leafType.isEnum()) {
163 if (this.value instanceof NullLiteral) {
164 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, this.value, true);
165 } else if (this.value instanceof ArrayInitializer) {
166 ArrayInitializer initializer = (ArrayInitializer) this.value;
167 final Expression[] expressions = initializer.expressions;
168 if (expressions != null) {
169 for (int i =0, max = expressions.length; i < max; i++) {
170 Expression currentExpression = expressions[i];
171 if (currentExpression instanceof NullLiteral) {
172 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, currentExpression, true);
173 } else if (currentExpression instanceof NameReference) {
174 NameReference nameReference = (NameReference) currentExpression;
175 final Binding nameReferenceBinding = nameReference.binding;
176 if (nameReferenceBinding.kind() == Binding.FIELD) {
177 FieldBinding fieldBinding = (FieldBinding) nameReferenceBinding;
178 if (!fieldBinding.declaringClass.isEnum()) {
179 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, currentExpression, true);
185 } else if (this.value instanceof NameReference) {
186 NameReference nameReference = (NameReference) this.value;
187 final Binding nameReferenceBinding = nameReference.binding;
188 if (nameReferenceBinding.kind() == Binding.FIELD) {
189 FieldBinding fieldBinding = (FieldBinding) nameReferenceBinding;
190 if (!fieldBinding.declaringClass.isEnum()) {
191 if (!fieldBinding.type.isArrayType()) {
192 scope.problemReporter().annotationValueMustBeConstant(this.binding.declaringClass, this.name, this.value, true);
194 scope.problemReporter().annotationValueMustBeArrayInitializer(this.binding.declaringClass, this.name, this.value);
199 break checkAnnotationMethodType;
201 if (leafType.isAnnotationType()) {
202 if (!valueType.leafComponentType().isAnnotationType()) { // check annotation type and also reject null literal
203 scope.problemReporter().annotationValueMustBeAnnotation(this.binding.declaringClass, this.name, this.value, leafType);
204 } else if (this.value instanceof ArrayInitializer) {
205 ArrayInitializer initializer = (ArrayInitializer) this.value;
206 final Expression[] expressions = initializer.expressions;
207 if (expressions != null) {
208 for (int i =0, max = expressions.length; i < max; i++) {
209 Expression currentExpression = expressions[i];
210 if (currentExpression instanceof NullLiteral || !(currentExpression instanceof Annotation)) {
211 scope.problemReporter().annotationValueMustBeAnnotation(this.binding.declaringClass, this.name, currentExpression, leafType);
215 } else if (!(this.value instanceof Annotation)) {
216 scope.problemReporter().annotationValueMustBeAnnotation(this.binding.declaringClass, this.name, this.value, leafType);
218 break checkAnnotationMethodType;
223 public void traverse(ASTVisitor visitor, BlockScope scope) {
224 if (visitor.visit(this, scope)) {
225 if (this.value != null) {
226 this.value.traverse(visitor, scope);
229 visitor.endVisit(this, scope);