001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.harmony.unpack200.bytecode;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.Objects;
024
025import org.apache.commons.compress.harmony.unpack200.SegmentUtils;
026
027/**
028 * Name and Type pair constant pool entry.
029 */
030public class CPNameAndType extends ConstantPoolEntry {
031
032    CPUTF8 descriptor;
033
034    transient int descriptorIndex;
035
036    CPUTF8 name;
037
038    transient int nameIndex;
039
040    private boolean hashCodeComputed;
041
042    private int cachedHashCode;
043
044    /**
045     * Constructs a new CPNameAndType.
046     *
047     * @param name        TODO
048     * @param descriptor  TODO
049     * @param globalIndex index in CpBands
050     * @throws NullPointerException if name or descriptor is null
051     */
052    public CPNameAndType(final CPUTF8 name, final CPUTF8 descriptor, final int globalIndex) {
053        super(CP_NameAndType, globalIndex);
054        this.name = Objects.requireNonNull(name, "name");
055        this.descriptor = Objects.requireNonNull(descriptor, "descriptor");
056    }
057
058    /*
059     * field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
060     */
061
062    @Override
063    public boolean equals(final Object obj) {
064        if (this == obj) {
065            return true;
066        }
067        if (obj == null || getClass() != obj.getClass()) {
068            return false;
069        }
070        final CPNameAndType other = (CPNameAndType) obj;
071        return Objects.equals(descriptor, other.descriptor)
072                && Objects.equals(name, other.name);
073    }
074
075    private void generateHashCode() {
076        hashCodeComputed = true;
077        final int PRIME = 31;
078        int result = 1;
079        result = PRIME * result + descriptor.hashCode();
080        result = PRIME * result + name.hashCode();
081        cachedHashCode = result;
082    }
083
084    @Override
085    protected ClassFileEntry[] getNestedClassFileEntries() {
086        return new ClassFileEntry[] { name, descriptor };
087    }
088
089    @Override
090    public int hashCode() {
091        if (!hashCodeComputed) {
092            generateHashCode();
093        }
094        return cachedHashCode;
095    }
096
097    /**
098     * Answers the invokeinterface count argument when the receiver is treated as an invokeinterface target. This value is not meaningful if the receiver is not
099     * an invokeinterface target.
100     *
101     * @return count
102     */
103    public int invokeInterfaceCount() {
104        return 1 + SegmentUtils.countInvokeInterfaceArgs(descriptor.underlyingString());
105    }
106
107    @Override
108    protected void resolve(final ClassConstantPool pool) {
109        super.resolve(pool);
110        descriptorIndex = pool.indexOf(descriptor);
111        nameIndex = pool.indexOf(name);
112    }
113
114    @Override
115    public String toString() {
116        return "NameAndType: " + name + "(" + descriptor + ")";
117    }
118
119    @Override
120    protected void writeBody(final DataOutputStream dos) throws IOException {
121        dos.writeShort(nameIndex);
122        dos.writeShort(descriptorIndex);
123    }
124}