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.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * @since 6.0
26   */
27  public abstract class ElementValue
28  {
29      /**
30       * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
31       */
32      @java.lang.Deprecated
33      protected int type; // TODO should be final
34  
35      /**
36       * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
37       */
38      @java.lang.Deprecated
39      protected ConstantPool cpool; // TODO should be final
40  
41      @Override
42      public String toString()
43      {
44          return stringifyValue();
45      }
46  
47      protected ElementValue(final int type, final ConstantPool cpool)
48      {
49          this.type = type;
50          this.cpool = cpool;
51      }
52  
53      public int getElementValueType()
54      {
55          return type;
56      }
57  
58      public abstract String stringifyValue();
59  
60      public abstract void dump(DataOutputStream dos) throws IOException;
61  
62      public static final byte STRING            = 's';
63      public static final byte ENUM_CONSTANT     = 'e';
64      public static final byte CLASS             = 'c';
65      public static final byte ANNOTATION        = '@';
66      public static final byte ARRAY             = '[';
67      public static final byte PRIMITIVE_INT     = 'I';
68      public static final byte PRIMITIVE_BYTE    = 'B';
69      public static final byte PRIMITIVE_CHAR    = 'C';
70      public static final byte PRIMITIVE_DOUBLE  = 'D';
71      public static final byte PRIMITIVE_FLOAT   = 'F';
72      public static final byte PRIMITIVE_LONG    = 'J';
73      public static final byte PRIMITIVE_SHORT   = 'S';
74      public static final byte PRIMITIVE_BOOLEAN = 'Z';
75  
76      public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool) throws IOException
77      {
78          final byte type = input.readByte();
79          switch (type)
80          {
81              case PRIMITIVE_BYTE:
82              case PRIMITIVE_CHAR:
83              case PRIMITIVE_DOUBLE:
84              case PRIMITIVE_FLOAT:
85              case PRIMITIVE_INT:
86              case PRIMITIVE_LONG:
87              case PRIMITIVE_SHORT:
88              case PRIMITIVE_BOOLEAN:
89              case STRING:
90                  return new SimpleElementValue(type, input.readUnsignedShort(), cpool);
91  
92              case ENUM_CONSTANT:
93                  return new EnumElementValue(ENUM_CONSTANT, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
94  
95              case CLASS:
96                  return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool);
97  
98              case ANNOTATION:
99                  // TODO isRuntimeVisible
100                 return new AnnotationElementValue(ANNOTATION, AnnotationEntry.read(input, cpool, false), cpool);
101 
102             case ARRAY:
103                 final int numArrayVals = input.readUnsignedShort();
104                 final ElementValuee.html#ElementValue">ElementValue[] evalues = new ElementValue[numArrayVals];
105                 for (int j = 0; j < numArrayVals; j++)
106                 {
107                     evalues[j] = ElementValue.readElementValue(input, cpool);
108                 }
109                 return new ArrayElementValue(ARRAY, evalues, cpool);
110 
111             default:
112                 throw new IllegalArgumentException("Unexpected element value kind in annotation: " + type);
113         }
114     }
115 
116     /** @since 6.0 */
117     final ConstantPool getConstantPool() {
118         return cpool;
119     }
120 
121     /** @since 6.0 */
122     final int getType() {
123         return type;
124     }
125 
126     public String toShortString()
127     {
128         return stringifyValue();
129     }
130 }