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