1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataOutputStream;
  25 import java.io.IOException;
  26 
  27 import com.sun.org.apache.bcel.internal.Const;
  28 
  29 /**
  30  * an annotation's element value pair
  31  *
  32  * @since 6.0
  33  */
  34 public class ElementValuePair
  35 {
  36     private final ElementValue elementValue;
  37 
  38     private final ConstantPool constantPool;
  39 
  40     private final int elementNameIndex;
  41 
  42     public ElementValuePair(final int elementNameIndex, final ElementValue elementValue,
  43             final ConstantPool constantPool)
  44     {
  45         this.elementValue = elementValue;
  46         this.elementNameIndex = elementNameIndex;
  47         this.constantPool = constantPool;
  48     }
  49 
  50     public String getNameString()
  51     {
  52         final ConstantUtf8 c = (ConstantUtf8) constantPool.getConstant(
  53                 elementNameIndex, Const.CONSTANT_Utf8);
  54         return c.getBytes();
  55     }
  56 
  57     public final ElementValue getValue()
  58     {
  59         return elementValue;
  60     }
  61 
  62     public int getNameIndex()
  63     {
  64         return elementNameIndex;
  65     }
  66 
  67     public String toShortString()
  68     {
  69         final StringBuilder result = new StringBuilder();
  70         result.append(getNameString()).append("=").append(
  71                 getValue().toShortString());
  72         return result.toString();
  73     }
  74 
  75     protected void dump(final DataOutputStream dos) throws IOException {
  76         dos.writeShort(elementNameIndex); // u2 name of the element
  77         elementValue.dump(dos);
  78     }
  79 }