Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / ElementValuePair.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
12
13 import net.sourceforge.phpdt.internal.compiler.ast.*;
14 import net.sourceforge.phpdt.internal.compiler.classfmt.ClassFileConstants;
15 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
16
17 public class ElementValuePair {
18         char[] name;
19         public Object value;
20         public MethodBinding binding;
21
22 public static Object getValue(Expression expression) {
23         if (expression == null)
24                 return null;
25         Constant constant = expression.constant;
26         // literals would hit this case.
27         if (constant != null && constant != Constant.NotAConstant)
28                 return constant;
29
30         if (expression instanceof Annotation)
31                 return ((Annotation) expression).getCompilerAnnotation();
32         if (expression instanceof ArrayInitializer) {
33                 Expression[] exprs = ((ArrayInitializer) expression).expressions;
34                 int length = exprs == null ? 0 : exprs.length;
35                 Object[] values = new Object[length];
36                 for (int i = 0; i < length; i++)
37                         values[i] = getValue(exprs[i]);
38                 return values;
39         }
40         if (expression instanceof ClassLiteralAccess)
41                 return ((ClassLiteralAccess) expression).targetType;
42         if (expression instanceof Reference) {
43                 FieldBinding fieldBinding = null;
44                 if (expression instanceof FieldReference) {
45                         fieldBinding = ((FieldReference) expression).fieldBinding();
46                 } else if (expression instanceof NameReference) {
47                         Binding binding = ((NameReference) expression).binding;
48                         if (binding != null && binding.kind() == Binding.FIELD)
49                                 fieldBinding = (FieldBinding) binding;
50                 }
51                 if (fieldBinding != null && (fieldBinding.modifiers & ClassFileConstants.AccEnum) > 0)
52                         return fieldBinding;
53         }
54         // something that isn't a compile time constant.
55         return null;
56 }
57
58 public ElementValuePair(char[] name, Expression expression, MethodBinding binding) {
59         this(name, ElementValuePair.getValue(expression), binding);
60 }
61
62 public ElementValuePair(char[] name, Object value, MethodBinding binding) {
63         this.name = name;
64         this.value = value;
65         this.binding = binding;
66 }
67
68 /**
69  * @return the name of the element value pair.
70  */
71 public char[] getName() {
72         return this.name;
73 }
74
75 /**
76  * @return the method binding that defined this member value pair or null if no such binding exists.
77  */
78 public MethodBinding getMethodBinding() {
79         return this.binding;
80 }
81
82 /**
83  * Return {@link TypeBinding} for member value of type {@link java.lang.Class}
84  * Return {@link org.eclipse.jdt.internal.compiler.impl.Constant} for member of primitive type or String
85  * Return {@link FieldBinding} for enum constant
86  * Return {@link AnnotationBinding} for annotation instance
87  * Return <code>Object[]</code> for member value of array type.
88  * @return the value of this member value pair or null if the value is missing or is not a compile-time constant
89  */
90 public Object getValue() {
91         return this.value;
92 }
93
94 void setMethodBinding(MethodBinding binding) {
95         // lazily set after annotation type was resolved
96         this.binding = binding;
97 }
98
99 void setValue(Object value) {
100         // can be modified after the initialization if holding an unresolved ref
101         this.value = value;
102 }
103
104 public String toString() {
105         StringBuffer buffer = new StringBuffer(5);
106         buffer.append(this.name).append(" = "); //$NON-NLS-1$
107         buffer.append(this.value);
108         return buffer.toString();
109 }
110 }