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.generic;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  
25  import org.apache.bcel.classfile.AnnotationElementValue;
26  import org.apache.bcel.classfile.AnnotationEntry;
27  import org.apache.bcel.classfile.ArrayElementValue;
28  import org.apache.bcel.classfile.ClassElementValue;
29  import org.apache.bcel.classfile.ElementValue;
30  import org.apache.bcel.classfile.EnumElementValue;
31  import org.apache.bcel.classfile.SimpleElementValue;
32  
33  /**
34   * @since 6.0
35   */
36  public abstract class ElementValueGen {
37      public static final int STRING = 's';
38  
39      public static final int ENUM_CONSTANT = 'e';
40  
41      public static final int CLASS = 'c';
42  
43      public static final int ANNOTATION = '@';
44  
45      public static final int ARRAY = '[';
46  
47      public static final int PRIMITIVE_INT = 'I';
48  
49      public static final int PRIMITIVE_BYTE = 'B';
50  
51      public static final int PRIMITIVE_CHAR = 'C';
52  
53      public static final int PRIMITIVE_DOUBLE = 'D';
54  
55      public static final int PRIMITIVE_FLOAT = 'F';
56  
57      public static final int PRIMITIVE_LONG = 'J';
58  
59      public static final int PRIMITIVE_SHORT = 'S';
60  
61      public static final int PRIMITIVE_BOOLEAN = 'Z';
62  
63      /**
64       * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
65       */
66      public static ElementValueGen copy(final ElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
67          switch (value.getElementValueType()) {
68          case 'B': // byte
69          case 'C': // char
70          case 'D': // double
71          case 'F': // float
72          case 'I': // int
73          case 'J': // long
74          case 'S': // short
75          case 'Z': // boolean
76          case 's': // String
77              return new SimpleElementValueGen((SimpleElementValue) value, cpool, copyPoolEntries);
78          case 'e': // Enum constant
79              return new EnumElementValueGen((EnumElementValue) value, cpool, copyPoolEntries);
80          case '@': // Annotation
81              return new AnnotationElementValueGen((AnnotationElementValue) value, cpool, copyPoolEntries);
82          case '[': // Array
83              return new ArrayElementValueGen((ArrayElementValue) value, cpool, copyPoolEntries);
84          case 'c': // Class
85              return new ClassElementValueGen((ClassElementValue) value, cpool, copyPoolEntries);
86          default:
87              throw new UnsupportedOperationException("Not implemented yet! (" + value.getElementValueType() + ")");
88          }
89      }
90  
91      public static ElementValueGen readElementValue(final DataInput dis, final ConstantPoolGen cpGen) throws IOException {
92          final int type = dis.readUnsignedByte();
93          switch (type) {
94          case 'B': // byte
95              return new SimpleElementValueGen(PRIMITIVE_BYTE, dis.readUnsignedShort(), cpGen);
96          case 'C': // char
97              return new SimpleElementValueGen(PRIMITIVE_CHAR, dis.readUnsignedShort(), cpGen);
98          case 'D': // double
99              return new SimpleElementValueGen(PRIMITIVE_DOUBLE, dis.readUnsignedShort(), cpGen);
100         case 'F': // float
101             return new SimpleElementValueGen(PRIMITIVE_FLOAT, dis.readUnsignedShort(), cpGen);
102         case 'I': // int
103             return new SimpleElementValueGen(PRIMITIVE_INT, dis.readUnsignedShort(), cpGen);
104         case 'J': // long
105             return new SimpleElementValueGen(PRIMITIVE_LONG, dis.readUnsignedShort(), cpGen);
106         case 'S': // short
107             return new SimpleElementValueGen(PRIMITIVE_SHORT, dis.readUnsignedShort(), cpGen);
108         case 'Z': // boolean
109             return new SimpleElementValueGen(PRIMITIVE_BOOLEAN, dis.readUnsignedShort(), cpGen);
110         case 's': // String
111             return new SimpleElementValueGen(STRING, dis.readUnsignedShort(), cpGen);
112         case 'e': // Enum constant
113             return new EnumElementValueGen(dis.readUnsignedShort(), dis.readUnsignedShort(), cpGen);
114         case 'c': // Class
115             return new ClassElementValueGen(dis.readUnsignedShort(), cpGen);
116         case '@': // Annotation
117             // TODO: isRuntimeVisible ??????????
118             // FIXME
119             return new AnnotationElementValueGen(ANNOTATION, new AnnotationEntryGen(AnnotationEntry.read(dis, cpGen.getConstantPool(), true), cpGen, false),
120                 cpGen);
121         case '[': // Array
122             final int numArrayVals = dis.readUnsignedShort();
123             final ElementValue[] evalues = new ElementValue[numArrayVals];
124             for (int j = 0; j < numArrayVals; j++) {
125                 evalues[j] = ElementValue.readElementValue(dis, cpGen.getConstantPool());
126             }
127             return new ArrayElementValueGen(ARRAY, evalues, cpGen);
128         default:
129             throw new IllegalArgumentException("Unexpected element value kind in annotation: " + type);
130         }
131     }
132 
133     /**
134      * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
135      */
136     @Deprecated
137     protected int type;
138 
139     /**
140      * @deprecated (since 6.0) will be made private and final; do not access directly, use getter
141      */
142     @Deprecated
143     protected ConstantPoolGen cpGen;
144 
145     protected ElementValueGen(final int type, final ConstantPoolGen cpGen) {
146         this.type = type;
147         this.cpGen = cpGen;
148     }
149 
150     public abstract void dump(DataOutputStream dos) throws IOException;
151 
152     protected ConstantPoolGen getConstantPool() {
153         return cpGen;
154     }
155 
156     /**
157      * Subtypes return an immutable variant of the ElementValueGen
158      */
159     public abstract ElementValue getElementValue();
160 
161     public int getElementValueType() {
162         return type;
163     }
164 
165     public abstract String stringifyValue();
166 }