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