Source code

    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.ConstantUtf8;
    023import org.apache.bcel.classfile.ElementValue;
    024import org.apache.bcel.classfile.ElementValuePair;
    025
    026/**
    027 * @since 6.0
    028 */
    029public class ElementValuePairGen {
    030    private final int nameIdx;
    031
    032    private final ElementValueGen value;
    033
    034    private final ConstantPoolGen constantPoolGen;
    035
    036    public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
    037        this.constantPoolGen = cpool;
    038        // J5ASSERT:
    039        // Could assert nvp.getNameString() points to the same thing as
    040        // constantPoolGen.getConstant(nvp.getNameIndex())
    041        // if
    042        // (!nvp.getNameString().equals(((ConstantUtf8)constantPoolGen.getConstant(nvp.getNameIndex())).getBytes()))
    043        // {
    044        // throw new IllegalArgumentException("envp buggered");
    045        // }
    046        if (copyPoolEntries) {
    047            nameIdx = cpool.addUtf8(nvp.getNameString());
    048        } else {
    049            nameIdx = nvp.getNameIndex();
    050        }
    051        value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
    052    }
    053
    054    protected ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool) {
    055        this.nameIdx = idx;
    056        this.value = value;
    057        this.constantPoolGen = cpool;
    058    }
    059
    060    public ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool) {
    061        this.nameIdx = cpool.addUtf8(name);
    062        this.value = value;
    063        this.constantPoolGen = cpool;
    064    }
    065
    066    protected void dump(final DataOutputStream dos) throws IOException {
    067        dos.writeShort(nameIdx); // u2 name of the element
    068        value.dump(dos);
    069    }
    070
    071    /**
    072     * Retrieve an immutable version of this ElementNameValuePairGen
    073     */
    074    public ElementValuePair getElementNameValuePair() {
    075        final ElementValue immutableValue = value.getElementValue();
    076        return new ElementValuePair(nameIdx, immutableValue, constantPoolGen.getConstantPool());
    077    }
    078
    079    public int getNameIndex() {
    080        return nameIdx;
    081    }
    082
    083    public final String getNameString() {
    084        // ConstantString cu8 = (ConstantString)constantPoolGen.getConstant(nameIdx);
    085        return ((ConstantUtf8) constantPoolGen.getConstant(nameIdx)).getBytes();
    086    }
    087
    088    public final ElementValueGen getValue() {
    089        return value;
    090    }
    091
    092    @Override
    093    public String toString() {
    094        return "ElementValuePair:[" + getNameString() + "=" + value.stringifyValue() + "]";
    095    }
    096}