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    public ElementValuePair(final int elementNameIndex, final ElementValue elementValue, final ConstantPool constantPool) {
040        this.elementValue = elementValue;
041        this.elementNameIndex = elementNameIndex;
042        this.constantPool = constantPool;
043    }
044
045    protected void dump(final DataOutputStream dos) throws IOException {
046        dos.writeShort(elementNameIndex); // u2 name of the element
047        elementValue.dump(dos);
048    }
049
050    public int getNameIndex() {
051        return elementNameIndex;
052    }
053
054    public String getNameString() {
055        return constantPool.getConstantUtf8(elementNameIndex).getBytes();
056    }
057
058    public final ElementValue getValue() {
059        return elementValue;
060    }
061
062    public String toShortString() {
063        final StringBuilder result = new StringBuilder();
064        result.append(getNameString()).append("=").append(getValue().toShortString());
065        return result.toString();
066    }
067}