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
022/**
023 * Field reference constant pool entry.
024 */
025public class CPFieldRef extends ConstantPoolEntry {
026
027    CPClass className;
028    transient int classNameIndex;
029    private final CPNameAndType nameAndType;
030    transient int nameAndTypeIndex;
031
032    public CPFieldRef(final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
033        super(ConstantPoolEntry.CP_Fieldref, globalIndex);
034        this.className = className;
035        this.nameAndType = descriptor;
036    }
037
038    @Override
039    protected ClassFileEntry[] getNestedClassFileEntries() {
040        return new ClassFileEntry[] {className, nameAndType};
041    }
042
043    @Override
044    protected void resolve(final ClassConstantPool pool) {
045        super.resolve(pool);
046        nameAndTypeIndex = pool.indexOf(nameAndType);
047        classNameIndex = pool.indexOf(className);
048    }
049
050    @Override
051    protected void writeBody(final DataOutputStream dos) throws IOException {
052        dos.writeShort(classNameIndex);
053        dos.writeShort(nameAndTypeIndex);
054    }
055
056    @Override
057    public String toString() {
058        return "FieldRef: " + className + "#" + nameAndType;
059    }
060
061    private boolean hashcodeComputed;
062    private int cachedHashCode;
063
064    private void generateHashCode() {
065        hashcodeComputed = true;
066        final int PRIME = 31;
067        int result = 1;
068        result = PRIME * result + ((className == null) ? 0 : className.hashCode());
069        result = PRIME * result + ((nameAndType == null) ? 0 : nameAndType.hashCode());
070        cachedHashCode = result;
071    }
072
073    @Override
074    public int hashCode() {
075        if (!hashcodeComputed) {
076            generateHashCode();
077        }
078        return cachedHashCode;
079    }
080
081    @Override
082    public boolean equals(final Object obj) {
083        if (this == obj) {
084            return true;
085        }
086        if (obj == null) {
087            return false;
088        }
089        if (getClass() != obj.getClass()) {
090            return false;
091        }
092        final CPFieldRef other = (CPFieldRef) obj;
093        if (className == null) {
094            if (other.className != null) {
095                return false;
096            }
097        } else if (!className.equals(other.className)) {
098            return false;
099        }
100        if (nameAndType == null) {
101            if (other.nameAndType != null) {
102                return false;
103            }
104        } else if (!nameAndType.equals(other.nameAndType)) {
105            return false;
106        }
107        return true;
108    }
109
110}