    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
    023import org.apache.commons.compress.harmony.unpack200.SegmentUtils;
    024
    025/**
    026 * Name and Type pair constant pool entry.
    027 */
    028public class CPNameAndType extends ConstantPoolEntry {
    029
    030    CPUTF8 descriptor;
    031
    032    transient int descriptorIndex;
    033
    034    CPUTF8 name;
    035
    036    transient int nameIndex;
    037
    038    private boolean hashCodeComputed;
    039
    040    private int cachedHashCode;
    041
    042    /**
    043     * Constructs a new CPNameAndType.
    044     *
    045     * @param name        TODO
    046     * @param descriptor  TODO
    047     * @param globalIndex - index in CpBands
    048     * @throws NullPointerException if name or descriptor is null
    049     */
    050    public CPNameAndType(final CPUTF8 name, final CPUTF8 descriptor, final int globalIndex) {
    051        super(ConstantPoolEntry.CP_NameAndType, globalIndex);
    052        this.name = Objects.requireNonNull(name, "name");
    053        this.descriptor = Objects.requireNonNull(descriptor, "descriptor");
    054    }
    055
    056    /*
    057     * field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
    058     */
    059
    060    @Override
    061    public boolean equals(final Object obj) {
    062        if (this == obj) {
    063            return true;
    064        }
    065        if (obj == null) {
    066            return false;
    067        }
    068        if (getClass() != obj.getClass()) {
    069            return false;
    070        }
    071        final CPNameAndType other = (CPNameAndType) obj;
    072        if (!descriptor.equals(other.descriptor)) {
    073            return false;
    074        }
    075        if (!name.equals(other.name)) {
    076            return false;
    077        }
    078        return true;
    079    }
    080
    081    private void generateHashCode() {
    082        hashCodeComputed = true;
    083        final int PRIME = 31;
    084        int result = 1;
    085        result = PRIME * result + descriptor.hashCode();
    086        result = PRIME * result + name.hashCode();
    087        cachedHashCode = result;
    088    }
    089
    090    @Override
    091    protected ClassFileEntry[] getNestedClassFileEntries() {
    092        return new ClassFileEntry[] { name, descriptor };
    093    }
    094
    095    @Override
    096    public int hashCode() {
    097        if (!hashCodeComputed) {
    098            generateHashCode();
    099        }
    100        return cachedHashCode;
    101    }
    102
    103    /**
    104     * Answers the invokeinterface count argument when the receiver is treated as an invokeinterface target. This value is not meaningful if the receiver is not
    105     * an invokeinterface target.
    106     *
    107     * @return count
    108     */
    109    public int invokeInterfaceCount() {
    110        return 1 + SegmentUtils.countInvokeInterfaceArgs(descriptor.underlyingString());
    111    }
    112
    113    @Override
    114    protected void resolve(final ClassConstantPool pool) {
    115        super.resolve(pool);
    116        descriptorIndex = pool.indexOf(descriptor);
    117        nameIndex = pool.indexOf(name);
    118    }
    119
    120    @Override
    121    public String toString() {
    122        return "NameAndType: " + name + "(" + descriptor + ")";
    123    }
    124
    125    @Override
    126    protected void writeBody(final DataOutputStream dos) throws IOException {
    127        dos.writeShort(nameIndex);
    128        dos.writeShort(descriptorIndex);
    129    }
    130}