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 (this.hashCode() != obj.hashCode()) {
063            return false;
064        }
065        final CPRef other = (CPRef) obj;
066        if (!className.equals(other.className)) {
067            return false;
068        }
069        if (!nameAndType.equals(other.nameAndType)) {
070            return false;
071        }
072        return true;
073    }
074
075    @Override
076    protected ClassFileEntry[] getNestedClassFileEntries() {
077        final ClassFileEntry[] entries = new ClassFileEntry[2];
078        entries[0] = className;
079        entries[1] = nameAndType;
080        return entries;
081    }
082
083    @Override
084    protected void resolve(final ClassConstantPool pool) {
085        super.resolve(pool);
086        nameAndTypeIndex = pool.indexOf(nameAndType);
087        classNameIndex = pool.indexOf(className);
088    }
089
090    @Override
091    public String toString() {
092        if (cachedToString == null) {
093            String type;
094            if (getTag() == ConstantPoolEntry.CP_Fieldref) {
095                type = "FieldRef"; //$NON-NLS-1$
096            } else if (getTag() == ConstantPoolEntry.CP_Methodref) {
097                type = "MethoddRef"; //$NON-NLS-1$
098            } else if (getTag() == ConstantPoolEntry.CP_InterfaceMethodref) {
099                type = "InterfaceMethodRef"; //$NON-NLS-1$
100            } else {
101                type = "unknown"; //$NON-NLS-1$
102            }
103            cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
104        }
105        return cachedToString;
106    }
107
108    @Override
109    protected void writeBody(final DataOutputStream dos) throws IOException {
110        dos.writeShort(classNameIndex);
111        dos.writeShort(nameAndTypeIndex);
112    }
113
114}