1 52 53 package com.go.trove.classfile; 54 55 import java.io.*; 56 57 65 public class ConstantClassInfo extends ConstantInfo { 66 private String mClassName; 67 private int mDim; 68 private TypeDescriptor mType; 69 70 private ConstantUTFInfo mNameConstant; 71 72 77 static ConstantClassInfo make(ConstantPool cp, String className) { 78 ConstantInfo ci = new ConstantClassInfo(cp, className); 79 return (ConstantClassInfo)cp.addConstant(ci); 80 } 81 82 83 static ConstantClassInfo make(ConstantPool cp, 84 String className, int dim) { 85 ConstantInfo ci = new ConstantClassInfo(cp, className, dim); 86 return (ConstantClassInfo)cp.addConstant(ci); 87 } 88 89 static ConstantClassInfo make(ConstantPool cp, 90 TypeDescriptor type) { 91 ConstantInfo ci = new ConstantClassInfo(cp, type); 92 return (ConstantClassInfo)cp.addConstant(ci); 93 } 94 95 ConstantClassInfo(ConstantUTFInfo nameConstant) { 96 super(TAG_CLASS); 97 mNameConstant = nameConstant; 98 String name = nameConstant.getValue(); 99 if (!name.endsWith(";") && !name.startsWith("[")) { 100 mClassName = name.replace('/', '.'); 101 mDim = 0; 102 } 103 else { 104 TypeDescriptor desc = TypeDescriptor.parseTypeDesc(name); 105 mClassName = desc.getClassName(); 106 mDim = desc.getDimensions(); 107 } 108 } 109 110 private ConstantClassInfo(ConstantPool cp, String className) { 111 super(TAG_CLASS); 112 113 mClassName = className; 114 mDim = 0; 115 116 String desc = className.replace('.', '/'); 117 mNameConstant = ConstantUTFInfo.make(cp, desc); 118 } 119 120 121 private ConstantClassInfo(ConstantPool cp, String className, int dim) { 122 super(TAG_CLASS); 123 124 mClassName = className; 125 mDim = dim; 126 127 String desc; 128 if (dim > 0) { 129 desc = TypeDescriptor.generate(className, dim); 130 } 131 else { 132 desc = className.replace('.', '/'); 133 } 134 135 mNameConstant = ConstantUTFInfo.make(cp, desc); 136 } 137 138 private ConstantClassInfo(ConstantPool cp, TypeDescriptor type) { 139 super(TAG_CLASS); 140 141 mClassName = type.getClassName(); 142 mDim = type.getDimensions(); 143 mType = type; 144 145 String desc; 146 if (mDim > 0) { 147 desc = type.toString(); 148 } 149 else { 150 desc = type.getClassName().replace('.', '/'); 151 } 152 153 mNameConstant = ConstantUTFInfo.make(cp, desc); 154 } 155 156 public String getClassName() { 157 return mClassName; 158 } 159 160 public int getDimensions() { 161 return mDim; 162 } 163 164 public TypeDescriptor getTypeDescriptor() { 165 if (mType == null) { 166 mType = new TypeDescriptor(getClassName()); 167 168 int dim = getDimensions(); 169 if (dim > 0) { 170 mType = new TypeDescriptor(mType, dim); 171 } 172 } 173 174 return mType; 175 } 176 177 public int hashCode() { 178 return mClassName.hashCode(); 179 } 180 181 public boolean equals(Object obj) { 182 if (obj instanceof ConstantClassInfo) { 183 ConstantClassInfo other = (ConstantClassInfo)obj; 184 return mClassName.equals(other.mClassName) && mDim == other.mDim; 185 } 186 187 return false; 188 } 189 190 public void writeTo(DataOutput dout) throws IOException { 191 super.writeTo(dout); 192 dout.writeShort(mNameConstant.getIndex()); 193 } 194 195 public String toString() { 196 StringBuffer buf = new StringBuffer ("CONSTANT_Class_info: "); 197 buf.append(getClassName()); 198 for (int i = getDimensions(); i > 0; i--) { 199 buf.append('['); 200 buf.append(']'); 201 } 202 return buf.toString(); 203 } 204 } 205 | Popular Tags |