001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.lookup;
011:
012: import org.eclipse.jdt.internal.compiler.ast.*;
013: import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
014: import org.eclipse.jdt.internal.compiler.impl.Constant;
015:
016: public class ElementValuePair {
017: char[] name;
018: public Object value;
019: public MethodBinding binding;
020:
021: public static Object getValue(Expression expression) {
022: if (expression == null)
023: return null;
024: Constant constant = expression.constant;
025: // literals would hit this case.
026: if (constant != null && constant != Constant.NotAConstant)
027: return constant;
028:
029: if (expression instanceof Annotation)
030: return ((Annotation) expression).getCompilerAnnotation();
031: if (expression instanceof ArrayInitializer) {
032: Expression[] exprs = ((ArrayInitializer) expression).expressions;
033: int length = exprs == null ? 0 : exprs.length;
034: Object[] values = new Object[length];
035: for (int i = 0; i < length; i++)
036: values[i] = getValue(exprs[i]);
037: return values;
038: }
039: if (expression instanceof ClassLiteralAccess)
040: return ((ClassLiteralAccess) expression).targetType;
041: if (expression instanceof Reference) {
042: FieldBinding fieldBinding = null;
043: if (expression instanceof FieldReference) {
044: fieldBinding = ((FieldReference) expression)
045: .fieldBinding();
046: } else if (expression instanceof NameReference) {
047: Binding binding = ((NameReference) expression).binding;
048: if (binding != null && binding.kind() == Binding.FIELD)
049: fieldBinding = (FieldBinding) binding;
050: }
051: if (fieldBinding != null
052: && (fieldBinding.modifiers & ClassFileConstants.AccEnum) > 0)
053: return fieldBinding;
054: }
055: // something that isn't a compile time constant.
056: return null;
057: }
058:
059: public ElementValuePair(char[] name, Expression expression,
060: MethodBinding binding) {
061: this (name, ElementValuePair.getValue(expression), binding);
062: }
063:
064: public ElementValuePair(char[] name, Object value,
065: MethodBinding binding) {
066: this .name = name;
067: this .value = value;
068: this .binding = binding;
069: }
070:
071: /**
072: * @return the name of the element value pair.
073: */
074: public char[] getName() {
075: return this .name;
076: }
077:
078: /**
079: * @return the method binding that defined this member value pair or null if no such binding exists.
080: */
081: public MethodBinding getMethodBinding() {
082: return this .binding;
083: }
084:
085: /**
086: * Return {@link TypeBinding} for member value of type {@link java.lang.Class}
087: * Return {@link org.eclipse.jdt.internal.compiler.impl.Constant} for member of primitive type or String
088: * Return {@link FieldBinding} for enum constant
089: * Return {@link AnnotationBinding} for annotation instance
090: * Return <code>Object[]</code> for member value of array type.
091: * @return the value of this member value pair or null if the value is missing or is not a compile-time constant
092: */
093: public Object getValue() {
094: return this .value;
095: }
096:
097: void setMethodBinding(MethodBinding binding) {
098: // lazily set after annotation type was resolved
099: this .binding = binding;
100: }
101:
102: void setValue(Object value) {
103: // can be modified after the initialization if holding an unresolved ref
104: this.value = value;
105: }
106: }
|