    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.bcel.generic;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021
    022import org.apache.bcel.classfile.ClassElementValue;
    023import org.apache.bcel.classfile.ConstantUtf8;
    024import org.apache.bcel.classfile.ElementValue;
    025
    026/**
    027 * @since 6.0
    028 */
    029public class ClassElementValueGen extends ElementValueGen {
    030    // For primitive types and string type, this points to the value entry in
    031    // the cpool
    032    // For 'class' this points to the class entry in the cpool
    033    private final int idx;
    034
    035    public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
    036        super(CLASS, cpool);
    037        if (copyPoolEntries) {
    038            // idx = cpool.addClass(value.getClassString());
    039            idx = cpool.addUtf8(value.getClassString());
    040        } else {
    041            idx = value.getIndex();
    042        }
    043    }
    044
    045    protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) {
    046        super(CLASS, cpool);
    047        this.idx = typeIdx;
    048    }
    049
    050    public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) {
    051        super(CLASS, cpool);
    052        // this.idx = cpool.addClass(t);
    053        idx = cpool.addUtf8(t.getSignature());
    054    }
    055
    056    @Override
    057    public void dump(final DataOutputStream dos) throws IOException {
    058        dos.writeByte(super.getElementValueType()); // u1 kind of value
    059        dos.writeShort(idx);
    060    }
    061
    062    public String getClassString() {
    063        final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
    064        return cu8.getBytes();
    065        // ConstantClass c = (ConstantClass) getConstantPool().getConstant(idx);
    066        // ConstantUtf8 utf8 =
    067        // (ConstantUtf8) getConstantPool().getConstant(c.getNameIndex());
    068        // return utf8.getBytes();
    069    }
    070
    071    /**
    072     * Return immutable variant of this ClassElementValueGen
    073     */
    074    @Override
    075    public ElementValue getElementValue() {
    076        return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
    077    }
    078
    079    public int getIndex() {
    080        return idx;
    081    }
    082
    083    @Override
    084    public String stringifyValue() {
    085        return getClassString();
    086    }
    087}