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