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