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     * Create 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 ClassFileEntry[] getNestedClassFileEntries() {
055        final int attributeCount = attributes.size();
056        final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
057        entries[0] = name;
058        entries[1] = descriptor;
059        for (int i = 0; i < attributeCount; i++) {
060            entries[i + 2] = attributes.get(i);
061        }
062        return entries;
063    }
064
065    @Override
066    protected void resolve(final ClassConstantPool pool) {
067        super.resolve(pool);
068        nameIndex = pool.indexOf(name);
069        descriptorIndex = pool.indexOf(descriptor);
070        attributes.forEach(attribute -> attribute.resolve(pool));
071    }
072
073    @Override
074    public int hashCode() {
075        final int PRIME = 31;
076        int result = 1;
077        result = PRIME * result + attributes.hashCode();
078        result = PRIME * result + descriptor.hashCode();
079        result = PRIME * result + flags;
080        result = PRIME * result + name.hashCode();
081        return result;
082    }
083
084    @Override
085    public String toString() {
086        return "CPMember: " + name + "(" + descriptor + ")";
087    }
088
089    @Override
090    public boolean equals(final Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (obj == null) {
095            return false;
096        }
097        if (getClass() != obj.getClass()) {
098            return false;
099        }
100        final CPMember other = (CPMember) obj;
101        if (!attributes.equals(other.attributes)) {
102            return false;
103        }
104        if (!descriptor.equals(other.descriptor)) {
105            return false;
106        }
107        if (flags != other.flags) {
108            return false;
109        }
110        if (!name.equals(other.name)) {
111            return false;
112        }
113        return true;
114    }
115
116    @Override
117    protected void doWrite(final DataOutputStream dos) throws IOException {
118        dos.writeShort(flags);
119        dos.writeShort(nameIndex);
120        dos.writeShort(descriptorIndex);
121        final int attributeCount = attributes.size();
122        dos.writeShort(attributeCount);
123        for (int i = 0; i < attributeCount; i++) {
124            final Attribute attribute = attributes.get(i);
125            attribute.doWrite(dos);
126        }
127    }
128
129}