    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.classfile;
    020
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024/**
    025 * An annotation's element value pair.
    026 *
    027 * @since 6.0
    028 */
    029public class ElementValuePair {
    030
    031    static final ElementValuePair[] EMPTY_ARRAY = {};
    032
    033    private final ElementValue elementValue;
    034
    035    private final ConstantPool constantPool;
    036
    037    private final int elementNameIndex;
    038
    039    /**
    040     * Constructs an ElementValuePair.
    041     *
    042     * @param elementNameIndex the element name index.
    043     * @param elementValue the element value.
    044     * @param constantPool the constant pool.
    045     */
    046    public ElementValuePair(final int elementNameIndex, final ElementValue elementValue, final ConstantPool constantPool) {
    047        this.elementValue = elementValue;
    048        this.elementNameIndex = elementNameIndex;
    049        this.constantPool = constantPool;
    050    }
    051
    052    /**
    053     * Dumps this element value pair to a DataOutputStream.
    054     *
    055     * @param dos the output stream.
    056     * @throws IOException if an I/O error occurs.
    057     */
    058    protected void dump(final DataOutputStream dos) throws IOException {
    059        dos.writeShort(elementNameIndex); // u2 name of the element
    060        elementValue.dump(dos);
    061    }
    062
    063    /**
    064     * Gets the name index.
    065     *
    066     * @return the name index.
    067     */
    068    public int getNameIndex() {
    069        return elementNameIndex;
    070    }
    071
    072    /**
    073     * Gets the name string.
    074     *
    075     * @return the name string.
    076     */
    077    public String getNameString() {
    078        return constantPool.getConstantUtf8(elementNameIndex).getBytes();
    079    }
    080
    081    /**
    082     * Gets the value.
    083     *
    084     * @return the element value.
    085     */
    086    public final ElementValue getValue() {
    087        return elementValue;
    088    }
    089
    090    /**
    091     * Gets a short string representation.
    092     *
    093     * @return a short string representation.
    094     */
    095    public String toShortString() {
    096        final StringBuilder result = new StringBuilder();
    097        result.append(getNameString()).append("=").append(getValue().toShortString());
    098        return result.toString();
    099    }
    100}