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 public boolean equals(final Object obj) { 117 if (this == obj) { 118 return true; 119 } 120 if (!super.equals(obj)) { 121 return false; 122 } 123 if (this.getClass() != obj.getClass()) { 124 return false; 125 } 126 final InnerClassesAttribute other = (InnerClassesAttribute) obj; 127 if (getAttributeName() == null) { 128 if (other.getAttributeName() != null) { 129 return false; 130 } 131 } else if (!getAttributeName().equals(other.getAttributeName())) { 132 return false; 133 } 134 return true; 135 } 136 137 @Override 138 protected int getLength() { 139 return 2 + (2 + 2 + 2 + 2) * innerClasses.size(); 140 } 141 142 @Override 143 protected ClassFileEntry[] getNestedClassFileEntries() { 144 return nestedClassFileEntries.toArray(NONE); 145 } 146 147 @Override 148 public int hashCode() { 149 final int PRIME = 31; 150 int result = super.hashCode(); 151 result = PRIME * result + (getAttributeName() == null ? 0 : getAttributeName().hashCode()); 152 return result; 153 } 154 155 @Override 156 protected void resolve(final ClassConstantPool pool) { 157 super.resolve(pool); 158 for (final InnerClassesEntry entry : innerClasses) { 159 entry.resolve(pool); 160 } 161 } 162 163 @Override 164 public String toString() { 165 return "InnerClasses: " + getAttributeName(); 166 } 167 168 @Override 169 protected void writeBody(final DataOutputStream dos) throws IOException { 170 dos.writeShort(innerClasses.size()); 171 172 for (final InnerClassesEntry entry : innerClasses) { 173 entry.write(dos); 174 } 175 } 176}