001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.io.DataInput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024
025import org.apache.bcel.classfile.AnnotationElementValue;
026import org.apache.bcel.classfile.AnnotationEntry;
027import org.apache.bcel.classfile.ArrayElementValue;
028import org.apache.bcel.classfile.ClassElementValue;
029import org.apache.bcel.classfile.ElementValue;
030import org.apache.bcel.classfile.EnumElementValue;
031import org.apache.bcel.classfile.SimpleElementValue;
032
033/**
034 * Generates element values in annotations.
035 *
036 * @since 6.0
037 */
038public abstract class ElementValueGen {
039
040    /** Element value type: string. */
041    public static final int STRING = 's';
042
043    /** Element value type: enum constant. */
044    public static final int ENUM_CONSTANT = 'e';
045
046    /** Element value type: class. */
047    public static final int CLASS = 'c';
048
049    /** Element value type: annotation. */
050    public static final int ANNOTATION = '@';
051
052    /** Element value type: array. */
053    public static final int ARRAY = '[';
054
055    /** Element value type: primitive int. */
056    public static final int PRIMITIVE_INT = 'I';
057
058    /** Element value type: primitive byte. */
059    public static final int PRIMITIVE_BYTE = 'B';
060
061    /** Element value type: primitive char. */
062    public static final int PRIMITIVE_CHAR = 'C';
063
064    /** Element value type: primitive double. */
065    public static final int PRIMITIVE_DOUBLE = 'D';
066
067    /** Element value type: primitive float. */
068    public static final int PRIMITIVE_FLOAT = 'F';
069
070    /** Element value type: primitive long. */
071    public static final int PRIMITIVE_LONG = 'J';
072
073    /** Element value type: primitive short. */
074    public static final int PRIMITIVE_SHORT = 'S';
075
076    /** Element value type: primitive boolean. */
077    public static final int PRIMITIVE_BOOLEAN = 'Z';
078
079    /**
080     * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
081     *
082     * @param value the element value to copy.
083     * @param cpool the constant pool generator.
084     * @param copyPoolEntries whether to copy pool entries.
085     * @return a copy of the element value.
086     */
087    public static ElementValueGen copy(final ElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
088        switch (value.getElementValueType()) {
089        case 'B': // byte
090        case 'C': // char
091        case 'D': // double
092        case 'F': // float
093        case 'I': // int
094        case 'J': // long
095        case 'S': // short
096        case 'Z': // boolean
097        case 's': // String
098            return new SimpleElementValueGen((SimpleElementValue) value, cpool, copyPoolEntries);
099        case 'e': // Enum constant
100            return new EnumElementValueGen((EnumElementValue) value, cpool, copyPoolEntries);
101        case '@': // Annotation
102            return new AnnotationElementValueGen((AnnotationElementValue) value, cpool, copyPoolEntries);
103        case '[': // Array
104            return new ArrayElementValueGen((ArrayElementValue) value, cpool, copyPoolEntries);
105        case 'c': // Class
106            return new ClassElementValueGen((ClassElementValue) value, cpool, copyPoolEntries);
107        default:
108            throw new UnsupportedOperationException("Not implemented yet! (" + value.getElementValueType() + ")");
109        }
110    }
111
112    /**
113     * Reads an element value from a DataInput.
114     *
115     * @param dis the data input stream.
116     * @param cpGen the constant pool.
117     * @return the element value read.
118     * @throws IOException if an I/O error occurs.
119     */
120    public static ElementValueGen readElementValue(final DataInput dis, final ConstantPoolGen cpGen) throws IOException {
121        final int type = dis.readUnsignedByte();
122        switch (type) {
123        case 'B': // byte
124            return new SimpleElementValueGen(PRIMITIVE_BYTE, dis.readUnsignedShort(), cpGen);
125        case 'C': // char
126            return new SimpleElementValueGen(PRIMITIVE_CHAR, dis.readUnsignedShort(), cpGen);
127        case 'D': // double
128            return new SimpleElementValueGen(PRIMITIVE_DOUBLE, dis.readUnsignedShort(), cpGen);
129        case 'F': // float
130            return new SimpleElementValueGen(PRIMITIVE_FLOAT, dis.readUnsignedShort(), cpGen);
131        case 'I': // int
132            return new SimpleElementValueGen(PRIMITIVE_INT, dis.readUnsignedShort(), cpGen);
133        case 'J': // long
134            return new SimpleElementValueGen(PRIMITIVE_LONG, dis.readUnsignedShort(), cpGen);
135        case 'S': // short
136            return new SimpleElementValueGen(PRIMITIVE_SHORT, dis.readUnsignedShort(), cpGen);
137        case 'Z': // boolean
138            return new SimpleElementValueGen(PRIMITIVE_BOOLEAN, dis.readUnsignedShort(), cpGen);
139        case 's': // String
140            return new SimpleElementValueGen(STRING, dis.readUnsignedShort(), cpGen);
141        case 'e': // Enum constant
142            return new EnumElementValueGen(dis.readUnsignedShort(), dis.readUnsignedShort(), cpGen);
143        case 'c': // Class
144            return new ClassElementValueGen(dis.readUnsignedShort(), cpGen);
145        case '@': // Annotation
146            // TODO: isRuntimeVisible ??????????
147            // FIXME
148            return new AnnotationElementValueGen(ANNOTATION, new AnnotationEntryGen(AnnotationEntry.read(dis, cpGen.getConstantPool(), true), cpGen, false),
149                cpGen);
150        case '[': // Array
151            final int numArrayVals = dis.readUnsignedShort();
152            final ElementValue[] evalues = new ElementValue[numArrayVals];
153            for (int j = 0; j < numArrayVals; j++) {
154                evalues[j] = ElementValue.readElementValue(dis, cpGen.getConstantPool());
155            }
156            return new ArrayElementValueGen(ARRAY, evalues, cpGen);
157        default:
158            throw new IllegalArgumentException("Unexpected element value kind in annotation: " + type);
159        }
160    }
161
162    /**
163     * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
164     */
165    @Deprecated
166    protected int type;
167
168    /**
169     * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
170     */
171    @Deprecated
172    protected ConstantPoolGen cpGen;
173
174    /**
175     * Constructs an ElementValueGen.
176     *
177     * @param type the element value type.
178     * @param cpGen the constant pool.
179     */
180    protected ElementValueGen(final int type, final ConstantPoolGen cpGen) {
181        this.type = type;
182        this.cpGen = cpGen;
183    }
184
185    /**
186     * Dumps this element value to a DataOutputStream.
187     *
188     * @param dos the output stream.
189     * @throws IOException if an I/O error occurs.
190     */
191    public abstract void dump(DataOutputStream dos) throws IOException;
192
193    /**
194     * Gets the constant pool.
195     *
196     * @return the constant pool.
197     */
198    protected ConstantPoolGen getConstantPool() {
199        return cpGen;
200    }
201
202    /**
203     * Subtypes return an immutable variant of the ElementValueGen.
204     *
205     * @return an immutable variant of the ElementValueGen.
206     */
207    public abstract ElementValue getElementValue();
208
209    /**
210     * Gets the element value type.
211     *
212     * @return the element value type.
213     */
214    public int getElementValueType() {
215        return type;
216    }
217
218    /**
219     * Returns a string representation of the element value.
220     *
221     * @return a string representation of the element value.
222     */
223    public abstract String stringifyValue();
224}