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  package org.apache.bcel.classfile;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  /**
23   * An annotation's element value pair.
24   *
25   * @since 6.0
26   */
27  public class ElementValuePair {
28  
29      static final ElementValuePair[] EMPTY_ARRAY = {};
30  
31      private final ElementValue elementValue;
32  
33      private final ConstantPool constantPool;
34  
35      private final int elementNameIndex;
36  
37      public ElementValuePair(final int elementNameIndex, final ElementValue elementValue, final ConstantPool constantPool) {
38          this.elementValue = elementValue;
39          this.elementNameIndex = elementNameIndex;
40          this.constantPool = constantPool;
41      }
42  
43      protected void dump(final DataOutputStream dos) throws IOException {
44          dos.writeShort(elementNameIndex); // u2 name of the element
45          elementValue.dump(dos);
46      }
47  
48      public int getNameIndex() {
49          return elementNameIndex;
50      }
51  
52      public String getNameString() {
53          return constantPool.getConstantUtf8(elementNameIndex).getBytes();
54      }
55  
56      public final ElementValue getValue() {
57          return elementValue;
58      }
59  
60      public String toShortString() {
61          final StringBuilder result = new StringBuilder();
62          result.append(getNameString()).append("=").append(getValue().toShortString());
63          return result.toString();
64      }
65  }