169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 2004 Bill Burke. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.bytecode.annotation;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.lang.reflect.Method;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.ClassPool;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.ConstPool;
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.Descriptor;
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Enum constant value.
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @author Shigeru Chiba
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class EnumMemberValue extends MemberValue {
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int typeIndex, valueIndex;
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs an enum constant value.  The initial value is specified
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * by the constant pool entries at the given indexes.
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param type      the index of a CONSTANT_Utf8_info structure
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *                  representing the enum type.
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param value     the index of a CONSTANT_Utf8_info structure.
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *                  representing the enum value.
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public EnumMemberValue(int type, int value, ConstPool cp) {
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super('e', cp);
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.typeIndex = type;
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.valueIndex = value;
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs an enum constant value.
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The initial value is not specified.
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public EnumMemberValue(ConstPool cp) {
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super('e', cp);
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        typeIndex = valueIndex = 0;
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    Object getValue(ClassLoader cl, ClassPool cp, Method m)
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws ClassNotFoundException
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return getType(cl).getField(getValue()).get(null);
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (NoSuchFieldException e) {
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new ClassNotFoundException(getType() + "." + getValue());
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (IllegalAccessException e) {
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new ClassNotFoundException(getType() + "." + getValue());
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    Class getType(ClassLoader cl) throws ClassNotFoundException {
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return loadClass(cl, getType());
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Obtains the enum type name.
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return a fully-qualified type name.
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getType() {
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return Descriptor.toClassName(cp.getUtf8Info(typeIndex));
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Changes the enum type name.
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param typename a fully-qualified type name.
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setType(String typename) {
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        typeIndex = cp.addUtf8Info(Descriptor.of(typename));
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Obtains the name of the enum constant value.
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getValue() {
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return cp.getUtf8Info(valueIndex);
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Changes the name of the enum constant value.
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setValue(String name) {
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        valueIndex = cp.addUtf8Info(name);
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String toString() {
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getType() + "." + getValue();
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Writes the value.
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void write(AnnotationsWriter writer) throws IOException {
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        writer.enumConstValue(cp.getUtf8Info(typeIndex), getValue());
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Accepts a visitor.
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void accept(MemberValueVisitor visitor) {
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        visitor.visitEnumMemberValue(this);
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
126