001: /*
002: * Copyright 2004 Brian S O'Neill
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.cojen.classfile.constant;
018:
019: import java.io.DataOutput;
020: import java.io.IOException;
021: import org.cojen.classfile.ConstantInfo;
022: import org.cojen.classfile.ConstantPool;
023: import org.cojen.classfile.TypeDesc;
024:
025: /**
026: * This class corresponds to the CONSTANT_Class_info structure as defined in
027: * section 4.4.1 of <i>The Java Virtual Machine Specification</i>.
028: *
029: * @author Brian S O'Neill
030: */
031: public class ConstantClassInfo extends ConstantInfo {
032: private final TypeDesc mType;
033: private final ConstantUTFInfo mNameConstant;
034:
035: public ConstantClassInfo(ConstantUTFInfo nameConstant) {
036: super (TAG_CLASS);
037: String name = nameConstant.getValue();
038: if (!name.endsWith(";") && !name.startsWith("[")) {
039: mType = TypeDesc.forClass(name);
040: } else {
041: mType = TypeDesc.forDescriptor(name);
042: }
043: mNameConstant = nameConstant;
044: }
045:
046: public ConstantClassInfo(ConstantPool cp, String className) {
047: super (TAG_CLASS);
048: String desc = className.replace('.', '/');
049: mType = TypeDesc.forClass(className);
050: mNameConstant = cp.addConstantUTF(desc);
051: }
052:
053: /**
054: * Used to describe an array class.
055: */
056: public ConstantClassInfo(ConstantPool cp, String className, int dim) {
057: super (TAG_CLASS);
058: TypeDesc type = TypeDesc.forClass(className);
059: String desc;
060: if (dim > 0) {
061: while (--dim >= 0) {
062: type = type.toArrayType();
063: }
064: desc = type.getDescriptor();
065: } else {
066: desc = className.replace('.', '/');
067: }
068: mType = type;
069: mNameConstant = cp.addConstantUTF(desc);
070: }
071:
072: public ConstantClassInfo(ConstantPool cp, TypeDesc type) {
073: super (TAG_CLASS);
074: String desc;
075: if (type.isArray()) {
076: desc = type.getDescriptor();
077: } else {
078: desc = type.getRootName().replace('.', '/');
079: }
080: mType = type;
081: mNameConstant = cp.addConstantUTF(desc);
082: }
083:
084: public TypeDesc getType() {
085: return mType;
086: }
087:
088: public int hashCode() {
089: return mType.hashCode();
090: }
091:
092: public boolean equals(Object obj) {
093: if (this == obj) {
094: return true;
095: }
096: if (obj instanceof ConstantClassInfo) {
097: ConstantClassInfo other = (ConstantClassInfo) obj;
098: return mType.equals(other.mType);
099: }
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: }
|