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