    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.ArrayList;
    022import java.util.List;
    023
    024/**
    025 * Inner classes class file attribute
    026 */
    027public class InnerClassesAttribute extends Attribute {
    028
    029    private static final class InnerClassesEntry {
    030
    031        CPClass innerClassInfo;
    032        CPClass outerClassInfo;
    033        CPUTF8 innerClassName;
    034
    035        int innerClassInfoIndex = -1;
    036        int outerClassInfoIndex = -1;
    037        int innerNameIndex = -1;
    038        int innerClassAccessFlags = -1;
    039
    040        InnerClassesEntry(final CPClass innerClass, final CPClass outerClass, final CPUTF8 innerName, final int flags) {
    041            this.innerClassInfo = innerClass;
    042            this.outerClassInfo = outerClass;
    043            this.innerClassName = innerName;
    044            this.innerClassAccessFlags = flags;
    045        }
    046
    047        /**
    048         * Determine the indices of the things in the receiver which point to elements of the ClassConstantPool
    049         *
    050         * @param pool ClassConstantPool which holds the CPClass and CPUTF8 objects.
    051         */
    052        public void resolve(final ClassConstantPool pool) {
    053            if (innerClassInfo != null) {
    054                innerClassInfo.resolve(pool);
    055                innerClassInfoIndex = pool.indexOf(innerClassInfo);
    056            } else {
    057                innerClassInfoIndex = 0;
    058            }
    059
    060            if (innerClassName != null) {
    061                innerClassName.resolve(pool);
    062                innerNameIndex = pool.indexOf(innerClassName);
    063            } else {
    064                innerNameIndex = 0;
    065            }
    066
    067            if (outerClassInfo != null) {
    068                outerClassInfo.resolve(pool);
    069                outerClassInfoIndex = pool.indexOf(outerClassInfo);
    070            } else {
    071                outerClassInfoIndex = 0;
    072            }
    073        }
    074
    075        public void write(final DataOutputStream dos) throws IOException {
    076            dos.writeShort(innerClassInfoIndex);
    077            dos.writeShort(outerClassInfoIndex);
    078            dos.writeShort(innerNameIndex);
    079            dos.writeShort(innerClassAccessFlags);
    080        }
    081
    082    }
    083
    084    private static CPUTF8 attributeName;
    085
    086    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    087        attributeName = cpUTF8Value;
    088    }
    089
    090    private final List<InnerClassesEntry> innerClasses = new ArrayList<>();
    091    private final List<ConstantPoolEntry> nestedClassFileEntries = new ArrayList<>();
    092
    093    public InnerClassesAttribute(final String name) {
    094        super(attributeName);
    095        nestedClassFileEntries.add(getAttributeName());
    096    }
    097
    098    public void addInnerClassesEntry(final CPClass innerClass, final CPClass outerClass, final CPUTF8 innerName, final int flags) {
    099        if (innerClass != null) {
    100            nestedClassFileEntries.add(innerClass);
    101        }
    102        if (outerClass != null) {
    103            nestedClassFileEntries.add(outerClass);
    104        }
    105        if (innerName != null) {
    106            nestedClassFileEntries.add(innerName);
    107        }
    108        addInnerClassesEntry(new InnerClassesEntry(innerClass, outerClass, innerName, flags));
    109    }
    110
    111    private void addInnerClassesEntry(final InnerClassesEntry innerClassesEntry) {
    112        innerClasses.add(innerClassesEntry);
    113    }
    114
    115    @Override
    116    protected void doWrite(final DataOutputStream dos) throws IOException {
    117        // Hack so I can see what's being written.
    118        super.doWrite(dos);
    119    }
    120
    121    @Override
    122    public boolean equals(final Object obj) {
    123        if (this == obj) {
    124            return true;
    125        }
    126        if (!super.equals(obj)) {
    127            return false;
    128        }
    129        if (this.getClass() != obj.getClass()) {
    130            return false;
    131        }
    132        final InnerClassesAttribute other = (InnerClassesAttribute) obj;
    133        if (getAttributeName() == null) {
    134            if (other.getAttributeName() != null) {
    135                return false;
    136            }
    137        } else if (!getAttributeName().equals(other.getAttributeName())) {
    138            return false;
    139        }
    140        return true;
    141    }
    142
    143    @Override
    144    protected int getLength() {
    145        return 2 + (2 + 2 + 2 + 2) * innerClasses.size();
    146    }
    147
    148    @Override
    149    protected ClassFileEntry[] getNestedClassFileEntries() {
    150        return nestedClassFileEntries.toArray(ClassFileEntry.NONE);
    151    }
    152
    153    @Override
    154    public int hashCode() {
    155        final int PRIME = 31;
    156        int result = super.hashCode();
    157        result = PRIME * result + (getAttributeName() == null ? 0 : getAttributeName().hashCode());
    158        return result;
    159    }
    160
    161    @Override
    162    protected void resolve(final ClassConstantPool pool) {
    163        super.resolve(pool);
    164        for (final InnerClassesEntry entry : innerClasses) {
    165            entry.resolve(pool);
    166        }
    167    }
    168
    169    @Override
    170    public String toString() {
    171        return "InnerClasses: " + getAttributeName();
    172    }
    173
    174    @Override
    175    protected void writeBody(final DataOutputStream dos) throws IOException {
    176        dos.writeShort(innerClasses.size());
    177
    178        for (final InnerClassesEntry entry : innerClasses) {
    179            entry.write(dos);
    180        }
    181    }
    182}