    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.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.classfile.ClassElementValue;
    025import org.apache.bcel.classfile.ConstantUtf8;
    026import org.apache.bcel.classfile.ElementValue;
    027
    028/**
    029 * Generates class element values in annotations.
    030 *
    031 * @since 6.0
    032 */
    033public class ClassElementValueGen extends ElementValueGen {
    034    // For primitive types and string type, this points to the value entry in
    035    // the cpool
    036    // For 'class' this points to the class entry in the cpool
    037    private final int idx;
    038
    039    /**
    040     * Constructs a ClassElementValueGen.
    041     *
    042     * @param value the class element value.
    043     * @param cpool the constant pool generator.
    044     * @param copyPoolEntries whether to copy pool entries.
    045     */
    046    public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
    047        super(CLASS, cpool);
    048        if (copyPoolEntries) {
    049            // idx = cpool.addClass(value.getClassString());
    050            idx = cpool.addUtf8(value.getClassString());
    051        } else {
    052            idx = value.getIndex();
    053        }
    054    }
    055
    056    /**
    057     * Constructs a ClassElementValueGen.
    058     *
    059     * @param typeIdx the type index.
    060     * @param cpool the constant pool generator.
    061     */
    062    protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) {
    063        super(CLASS, cpool);
    064        this.idx = typeIdx;
    065    }
    066
    067    /**
    068     * Constructs a ClassElementValueGen.
    069     *
    070     * @param t the object type.
    071     * @param cpool the constant pool generator.
    072     */
    073    public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) {
    074        super(CLASS, cpool);
    075        // this.idx = cpool.addClass(t);
    076        idx = cpool.addUtf8(t.getSignature());
    077    }
    078
    079    @Override
    080    public void dump(final DataOutputStream dos) throws IOException {
    081        dos.writeByte(super.getElementValueType()); // u1 kind of value
    082        dos.writeShort(idx);
    083    }
    084
    085    /**
    086     * Gets the class string.
    087     *
    088     * @return the class string.
    089     */
    090    public String getClassString() {
    091        final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
    092        return cu8.getBytes();
    093        // ConstantClass c = (ConstantClass) getConstantPool().getConstant(idx);
    094        // ConstantUtf8 utf8 =
    095        // (ConstantUtf8) getConstantPool().getConstant(c.getNameIndex());
    096        // return utf8.getBytes();
    097    }
    098
    099    /**
    100     * Return immutable variant of this ClassElementValueGen.
    101     *
    102     * @return immutable variant of this ClassElementValueGen.
    103     */
    104    @Override
    105    public ElementValue getElementValue() {
    106        return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
    107    }
    108
    109    /**
    110     * Gets the index.
    111     *
    112     * @return the index.
    113     */
    114    public int getIndex() {
    115        return idx;
    116    }
    117
    118    @Override
    119    public String stringifyValue() {
    120        return getClassString();
    121    }
    122}