1 16 17 package org.cojen.classfile.constant; 18 19 import java.io.DataOutput ; 20 import java.io.IOException ; 21 import org.cojen.classfile.ConstantInfo; 22 import org.cojen.classfile.ConstantPool; 23 import org.cojen.classfile.TypeDesc; 24 25 31 public class ConstantClassInfo extends ConstantInfo { 32 private final TypeDesc mType; 33 private final ConstantUTFInfo mNameConstant; 34 35 public ConstantClassInfo(ConstantUTFInfo nameConstant) { 36 super(TAG_CLASS); 37 String name = nameConstant.getValue(); 38 if (!name.endsWith(";") && !name.startsWith("[")) { 39 mType = TypeDesc.forClass(name); 40 } else { 41 mType = TypeDesc.forDescriptor(name); 42 } 43 mNameConstant = nameConstant; 44 } 45 46 public ConstantClassInfo(ConstantPool cp, String className) { 47 super(TAG_CLASS); 48 String desc = className.replace('.', '/'); 49 mType = TypeDesc.forClass(className); 50 mNameConstant = cp.addConstantUTF(desc); 51 } 52 53 56 public ConstantClassInfo(ConstantPool cp, String className, int dim) { 57 super(TAG_CLASS); 58 TypeDesc type = TypeDesc.forClass(className); 59 String desc; 60 if (dim > 0) { 61 while (--dim >= 0) { 62 type = type.toArrayType(); 63 } 64 desc = type.getDescriptor(); 65 } else { 66 desc = className.replace('.', '/'); 67 } 68 mType = type; 69 mNameConstant = cp.addConstantUTF(desc); 70 } 71 72 public ConstantClassInfo(ConstantPool cp, TypeDesc type) { 73 super(TAG_CLASS); 74 String desc; 75 if (type.isArray()) { 76 desc = type.getDescriptor(); 77 } else { 78 desc = type.getRootName().replace('.', '/'); 79 } 80 mType = type; 81 mNameConstant = cp.addConstantUTF(desc); 82 } 83 84 public TypeDesc getType() { 85 return mType; 86 } 87 88 public int hashCode() { 89 return mType.hashCode(); 90 } 91 92 public boolean equals(Object obj) { 93 if (this == obj) { 94 return true; 95 } 96 if (obj instanceof ConstantClassInfo) { 97 ConstantClassInfo other = (ConstantClassInfo)obj; 98 return mType.equals(other.mType); 99 } 100 return false; 101 } 102 103 public void writeTo(DataOutput dout) throws IOException { 104 super.writeTo(dout); 105 dout.writeShort(mNameConstant.getIndex()); 106 } 107 108 public String toString() { 109 return "CONSTANT_Class_info: ".concat(getType().getFullName()); 110 } 111 } 112 | Popular Tags |