    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.Collections;
    022import java.util.List;
    023import java.util.Objects;
    024
    025/**
    026 * Superclass for member constant pool entries, such as fields or methods.
    027 */
    028public class CPMember extends ClassFileEntry {
    029
    030    List<Attribute> attributes;
    031    short flags;
    032    CPUTF8 name;
    033    transient int nameIndex;
    034    protected final CPUTF8 descriptor;
    035    transient int descriptorIndex;
    036
    037    /**
    038     * Constructs a new CPMember.
    039     *
    040     * @param name       TODO
    041     * @param descriptor TODO
    042     * @param flags      TODO
    043     * @param attributes TODO
    044     * @throws NullPointerException if name or descriptor is null
    045     */
    046    public CPMember(final CPUTF8 name, final CPUTF8 descriptor, final long flags, final List<Attribute> attributes) {
    047        this.name = Objects.requireNonNull(name, "name");
    048        this.descriptor = Objects.requireNonNull(descriptor, "descriptor");
    049        this.flags = (short) flags;
    050        this.attributes = attributes == null ? Collections.EMPTY_LIST : attributes;
    051    }
    052
    053    @Override
    054    protected void doWrite(final DataOutputStream dos) throws IOException {
    055        dos.writeShort(flags);
    056        dos.writeShort(nameIndex);
    057        dos.writeShort(descriptorIndex);
    058        final int attributeCount = attributes.size();
    059        dos.writeShort(attributeCount);
    060        for (int i = 0; i < attributeCount; i++) {
    061            final Attribute attribute = attributes.get(i);
    062            attribute.doWrite(dos);
    063        }
    064    }
    065
    066    @Override
    067    public boolean equals(final Object obj) {
    068        if (this == obj) {
    069            return true;
    070        }
    071        if (obj == null) {
    072            return false;
    073        }
    074        if (getClass() != obj.getClass()) {
    075            return false;
    076        }
    077        final CPMember other = (CPMember) obj;
    078        if (!attributes.equals(other.attributes)) {
    079            return false;
    080        }
    081        if (!descriptor.equals(other.descriptor)) {
    082            return false;
    083        }
    084        if (flags != other.flags) {
    085            return false;
    086        }
    087        if (!name.equals(other.name)) {
    088            return false;
    089        }
    090        return true;
    091    }
    092
    093    @Override
    094    protected ClassFileEntry[] getNestedClassFileEntries() {
    095        final int attributeCount = attributes.size();
    096        final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
    097        entries[0] = name;
    098        entries[1] = descriptor;
    099        for (int i = 0; i < attributeCount; i++) {
    100            entries[i + 2] = attributes.get(i);
    101        }
    102        return entries;
    103    }
    104
    105    @Override
    106    public int hashCode() {
    107        final int PRIME = 31;
    108        int result = 1;
    109        result = PRIME * result + attributes.hashCode();
    110        result = PRIME * result + descriptor.hashCode();
    111        result = PRIME * result + flags;
    112        result = PRIME * result + name.hashCode();
    113        return result;
    114    }
    115
    116    @Override
    117    protected void resolve(final ClassConstantPool pool) {
    118        super.resolve(pool);
    119        nameIndex = pool.indexOf(name);
    120        descriptorIndex = pool.indexOf(descriptor);
    121        attributes.forEach(attribute -> attribute.resolve(pool));
    122    }
    123
    124    @Override
    125    public String toString() {
    126        return "CPMember: " + name + "(" + descriptor + ")";
    127    }
    128
    129}