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.lookup;
 
  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;
 
  17 public class ElementValuePair {
 
  20         public MethodBinding binding;
 
  22 public static Object getValue(Expression expression) {
 
  23         if (expression == null)
 
  25         Constant constant = expression.constant;
 
  26         // literals would hit this case.
 
  27         if (constant != null && constant != Constant.NotAConstant)
 
  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]);
 
  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;
 
  51                 if (fieldBinding != null && (fieldBinding.modifiers & ClassFileConstants.AccEnum) > 0)
 
  54         // something that isn't a compile time constant.
 
  58 public ElementValuePair(char[] name, Expression expression, MethodBinding binding) {
 
  59         this(name, ElementValuePair.getValue(expression), binding);
 
  62 public ElementValuePair(char[] name, Object value, MethodBinding binding) {
 
  65         this.binding = binding;
 
  69  * @return the name of the element value pair.
 
  71 public char[] getName() {
 
  76  * @return the method binding that defined this member value pair or null if no such binding exists.
 
  78 public MethodBinding getMethodBinding() {
 
  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
 
  90 public Object getValue() {
 
  94 void setMethodBinding(MethodBinding binding) {
 
  95         // lazily set after annotation type was resolved
 
  96         this.binding = binding;
 
  99 void setValue(Object value) {
 
 100         // can be modified after the initialization if holding an unresolved ref
 
 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();