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