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.classfile;
019
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.Const;
024
025/**
026 * an annotation's element value pair
027 * 
028 * @version $Id: ElementValuePair
029 * @since 6.0
030 */
031public class ElementValuePair
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,
040            final ConstantPool constantPool)
041    {
042        this.elementValue = elementValue;
043        this.elementNameIndex = elementNameIndex;
044        this.constantPool = constantPool;
045    }
046
047    public String getNameString()
048    {
049        final ConstantUtf8 c = (ConstantUtf8) constantPool.getConstant(
050                elementNameIndex, Const.CONSTANT_Utf8);
051        return c.getBytes();
052    }
053
054    public final ElementValue getValue()
055    {
056        return elementValue;
057    }
058
059    public int getNameIndex()
060    {
061        return elementNameIndex;
062    }
063
064    public String toShortString()
065    {
066        final StringBuilder result = new StringBuilder();
067        result.append(getNameString()).append("=").append(
068                getValue().toShortString());
069        return result.toString();
070    }
071
072    protected void dump(final DataOutputStream dos) throws IOException {
073        dos.writeShort(elementNameIndex); // u2 name of the element
074        elementValue.dump(dos);
075    }
076}