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