1/* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5/* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22package com.sun.org.apache.bcel.internal.classfile; 23 24import java.io.DataInput; 25import java.io.DataOutputStream; 26import java.io.IOException; 27 28/** 29 * @version $Id: ElementValue 30 * @since 6.0 31 */ 32public abstract class ElementValue 33{ 34 private final int type; 35 36 private final ConstantPool cpool; 37 38 @Override 39 public String toString() 40 { 41 return stringifyValue(); 42 } 43 44 protected ElementValue(final int type, final ConstantPool cpool) 45 { 46 this.type = type; 47 this.cpool = cpool; 48 } 49 50 public int getElementValueType() 51 { 52 return type; 53 } 54 55 public abstract String stringifyValue(); 56 57 public abstract void dump(DataOutputStream dos) throws IOException; 58 59 public static final byte STRING = 's'; 60 public static final byte ENUM_CONSTANT = 'e'; 61 public static final byte CLASS = 'c'; 62 public static final byte ANNOTATION = '@'; 63 public static final byte ARRAY = '['; 64 public static final byte PRIMITIVE_INT = 'I'; 65 public static final byte PRIMITIVE_BYTE = 'B'; 66 public static final byte PRIMITIVE_CHAR = 'C'; 67 public static final byte PRIMITIVE_DOUBLE = 'D'; 68 public static final byte PRIMITIVE_FLOAT = 'F'; 69 public static final byte PRIMITIVE_LONG = 'J'; 70 public static final byte PRIMITIVE_SHORT = 'S'; 71 public static final byte PRIMITIVE_BOOLEAN = 'Z'; 72 73 public static ElementValue readElementValue(final DataInput input, final ConstantPool cpool) throws IOException 74 { 75 final byte type = input.readByte(); 76 switch (type) 77 { 78 case PRIMITIVE_BYTE: 79 case PRIMITIVE_CHAR: 80 case PRIMITIVE_DOUBLE: 81 case PRIMITIVE_FLOAT: 82 case PRIMITIVE_INT: 83 case PRIMITIVE_LONG: 84 case PRIMITIVE_SHORT: 85 case PRIMITIVE_BOOLEAN: 86 case STRING: 87 return new SimpleElementValue(type, input.readUnsignedShort(), cpool); 88 89 case ENUM_CONSTANT: 90 return new EnumElementValue(ENUM_CONSTANT, input.readUnsignedShort(), input.readUnsignedShort(), cpool); 91 92 case CLASS: 93 return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool); 94 95 case ANNOTATION: 96 // TODO isRuntimeVisible 97 return new AnnotationElementValue(ANNOTATION, AnnotationEntry.read(input, cpool, false), cpool); 98 99 case ARRAY: 100 final int numArrayVals = input.readUnsignedShort(); 101 final ElementValue[] evalues = new ElementValue[numArrayVals]; 102 for (int j = 0; j < numArrayVals; j++) 103 { 104 evalues[j] = ElementValue.readElementValue(input, cpool); 105 } 106 return new ArrayElementValue(ARRAY, evalues, cpool); 107 108 default: 109 throw new RuntimeException("Unexpected element value kind in annotation: " + type); 110 } 111 } 112 113 /** @since 6.0 */ 114 final ConstantPool getConstantPool() { 115 return cpool; 116 } 117 118 /** @since 6.0 */ 119 final int getType() { 120 return type; 121 } 122 123 public String toShortString() 124 { 125 return stringifyValue(); 126 } 127} 128