    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021import java.util.Objects;
    022
    023/**
    024 * Abstract superclass for reference constant pool entries, such as a method or field reference.
    025 */
    026public abstract class CPRef extends ConstantPoolEntry {
    027
    028    CPClass className;
    029    transient int classNameIndex;
    030
    031    protected CPNameAndType nameAndType;
    032    transient int nameAndTypeIndex;
    033
    034    protected String cachedToString;
    035
    036    /**
    037     * Constructs a new CPRef.
    038     *
    039     * @param type        TODO
    040     * @param className   TODO
    041     * @param descriptor  TODO
    042     * @param globalIndex index in CpBands
    043     * @throws NullPointerException if descriptor or className is null
    044     */
    045    public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
    046        super(type, globalIndex);
    047        this.className = Objects.requireNonNull(className, "className");
    048        this.nameAndType = Objects.requireNonNull(descriptor, "descriptor");
    049    }
    050
    051    @Override
    052    public boolean equals(final Object obj) {
    053        if (this == obj) {
    054            return true;
    055        }
    056        if (obj == null) {
    057            return false;
    058        }
    059        if (getClass() != obj.getClass()) {
    060            return false;
    061        }
    062        if (hashCode() != obj.hashCode()) {
    063            return false;
    064        }
    065        final CPRef other = (CPRef) obj;
    066        return Objects.equals(className, other.className)
    067                && Objects.equals(nameAndType, other.nameAndType);
    068    }
    069
    070    @Override
    071    protected ClassFileEntry[] getNestedClassFileEntries() {
    072        final ClassFileEntry[] entries = new ClassFileEntry[2];
    073        entries[0] = className;
    074        entries[1] = nameAndType;
    075        return entries;
    076    }
    077
    078    @Override
    079    protected void resolve(final ClassConstantPool pool) {
    080        super.resolve(pool);
    081        nameAndTypeIndex = pool.indexOf(nameAndType);
    082        classNameIndex = pool.indexOf(className);
    083    }
    084
    085    @Override
    086    public String toString() {
    087        if (cachedToString == null) {
    088            String type;
    089            if (getTag() == CP_Fieldref) {
    090                type = "FieldRef"; //$NON-NLS-1$
    091            } else if (getTag() == CP_Methodref) {
    092                type = "MethoddRef"; //$NON-NLS-1$
    093            } else if (getTag() == CP_InterfaceMethodref) {
    094                type = "InterfaceMethodRef"; //$NON-NLS-1$
    095            } else {
    096                type = "unknown"; //$NON-NLS-1$
    097            }
    098            cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
    099        }
    100        return cachedToString;
    101    }
    102
    103    @Override
    104    protected void writeBody(final DataOutputStream dos) throws IOException {
    105        dos.writeShort(classNameIndex);
    106        dos.writeShort(nameAndTypeIndex);
    107    }
    108
    109}