View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * Represents a class element value in an annotation.
26   *
27   * @since 6.0
28   */
29  public class ClassElementValue extends ElementValue {
30      // For primitive types and string type, this points to the value entry in
31      // the cpool
32      // For 'class' this points to the class entry in the cpool
33      private final int idx;
34  
35      /**
36       * Constructs a ClassElementValue.
37       *
38       * @param type the type.
39       * @param idx the index.
40       * @param cpool the constant pool.
41       */
42      public ClassElementValue(final int type, final int idx, final ConstantPool cpool) {
43          super(type, cpool);
44          this.idx = idx;
45      }
46  
47      @Override
48      public void dump(final DataOutputStream dos) throws IOException {
49          dos.writeByte(super.getType()); // u1 kind of value
50          dos.writeShort(idx);
51      }
52  
53      /**
54       * Gets the class string.
55       *
56       * @return the class string.
57       */
58      public String getClassString() {
59          return super.getConstantPool().getConstantUtf8(idx).getBytes();
60      }
61  
62      /**
63       * Gets the index.
64       *
65       * @return the index.
66       */
67      public int getIndex() {
68          return idx;
69      }
70  
71      @Override
72      public String stringifyValue() {
73          return super.getConstantPool().getConstantUtf8(idx).getBytes();
74      }
75  }