1 7 package org.gjt.jclasslib.structures.elementvalues; 8 9 import org.gjt.jclasslib.structures.*; 10 11 import java.io.*; 12 13 19 public abstract class ElementValue extends AbstractStructure { 20 21 public final static char BYTE_TAG = 'B'; 22 public final static char CHAR_TAG = 'C'; 23 public final static char DOUBLE_TAG = 'D'; 24 public final static char FLOAT_TAG = 'F'; 25 public final static char INT_TAG = 'I'; 26 public final static char LONG_TAG = 'J'; 27 public final static char SHORT_TAG = 'S'; 28 public final static char BOOL_TAG = 'Z'; 29 public final static char STRING_TAG = 's'; 30 public final static char ENUM_TAG = 'e'; 31 public final static char CLASS_TAG = 'c'; 32 public final static char ARRAY_TAG = '['; 33 public final static char ANNOTATION_TAG = '@'; 34 35 public final static String BYTE_TAG_VERBOSE = "byte"; 36 public final static String CHAR_TAG_VERBOSE = "String"; 37 public final static String DOUBLE_TAG_VERBOSE = "double"; 38 public final static String FLOAT_TAG_VERBOSE = "float"; 39 public final static String INT_TAG_VERBOSE = "int"; 40 public final static String LONG_TAG_VERBOSE = "long"; 41 public final static String SHORT_TAG_VERBOSE = "short"; 42 public final static String BOOL_TAG_VERBOSE = "boolean"; 43 public final static String STRING_TAG_VERBOSE = "String"; 44 public final static String ENUM_TAG_VERBOSE = "Enum"; 45 public final static String CLASS_TAG_VERBOSE = "Class"; 46 public final static String ARRAY_TAG_VERBOSE = "Array"; 47 public final static String ANNOTATION_TAG_VERBOSE = "Annotation"; 48 49 protected static final int INITIAL_LENGTH = 1; 50 private int tag; 51 52 public ElementValue(int tag) { 53 this.tag = tag; 54 } 55 56 57 68 public static ElementValue create(DataInput in, ClassFile classFile) 69 throws InvalidByteCodeException, IOException { 70 71 int tagType = in.readUnsignedByte(); 72 73 ElementValue entry = null; 74 switch (tagType) { 75 case BOOL_TAG: 76 case BYTE_TAG: 77 case CHAR_TAG: 78 case DOUBLE_TAG: 79 case FLOAT_TAG: 80 case SHORT_TAG: 81 case INT_TAG: 82 case LONG_TAG: 83 case STRING_TAG: 84 entry = new ConstElementValue(tagType); 85 break; 86 case ENUM_TAG: 87 entry = new EnumElementValue(); 88 break; 89 case CLASS_TAG: 90 entry = new ClassElementValue(); 91 break; 92 case ANNOTATION_TAG: 93 entry = new AnnotationElementValue(); 94 break; 95 case ARRAY_TAG: 96 entry = new ArrayElementValue(); 97 break; 98 default: 99 throw new InvalidByteCodeException("Unknown tag " + (char)tagType); 100 } 101 entry.setClassFile(classFile); 102 entry.read(in); 103 104 return entry; 105 } 106 107 108 public void write(DataOutput out) throws InvalidByteCodeException, IOException { 109 super.write(out); 110 111 out.writeByte(tag); 112 113 if (debug) debug("wrote "); 114 } 115 116 117 protected abstract int getSpecificLength(); 118 119 final public int getLength() { 120 return INITIAL_LENGTH + getSpecificLength(); 121 } 122 123 public abstract String getEntryName(); 124 125 protected String printAccessFlagsVerbose(int accessFlags) { 126 if (accessFlags != 0) 127 throw new RuntimeException ("Access flags should be zero: " + Integer.toHexString(accessFlags)); 128 return ""; 129 } 130 131 public int getTag() { 132 return tag; 133 } 134 135 public static String getTagDescription(int tag) { 136 switch (tag) { 137 case BOOL_TAG: 138 return BOOL_TAG_VERBOSE; 139 case BYTE_TAG: 140 return BYTE_TAG_VERBOSE; 141 case CHAR_TAG: 142 return CHAR_TAG_VERBOSE; 143 case DOUBLE_TAG: 144 return DOUBLE_TAG_VERBOSE; 145 case FLOAT_TAG: 146 return FLOAT_TAG_VERBOSE; 147 case SHORT_TAG: 148 return SHORT_TAG_VERBOSE; 149 case INT_TAG: 150 return INT_TAG_VERBOSE; 151 case LONG_TAG: 152 return LONG_TAG_VERBOSE; 153 case STRING_TAG: 154 return STRING_TAG_VERBOSE; 155 case ENUM_TAG: 156 return ENUM_TAG_VERBOSE; 157 case CLASS_TAG: 158 return CLASS_TAG_VERBOSE; 159 case ANNOTATION_TAG: 160 return ANNOTATION_TAG_VERBOSE; 161 case ARRAY_TAG: 162 return ARRAY_TAG_VERBOSE; 163 default: 164 return "Unknown"; 165 } 166 } 167 168 169 } 170 | Popular Tags |