1 52 53 package com.go.trove.classfile; 54 55 import java.util.*; 56 import java.io.*; 57 58 67 class InnerClassesAttr extends Attribute { 68 private List mInnerClasses = new ArrayList(); 69 70 public InnerClassesAttr(ConstantPool cp) { 71 super(cp, INNER_CLASSES); 72 } 73 74 80 public void addInnerClass(String inner, 81 String outer, 82 String name, 83 AccessFlags accessFlags) { 84 85 ConstantClassInfo innerInfo = ConstantClassInfo.make(mCp, inner); 86 ConstantClassInfo outerInfo; 87 if (outer == null) { 88 outerInfo = null; 89 } 90 else { 91 outerInfo = ConstantClassInfo.make(mCp, outer); 92 } 93 94 ConstantUTFInfo nameInfo; 95 if (name == null) { 96 nameInfo = null; 97 } 98 else { 99 nameInfo = ConstantUTFInfo.make(mCp, name); 100 } 101 102 mInnerClasses.add(new Info(innerInfo, outerInfo, nameInfo, 103 accessFlags.getModifier())); 104 } 105 106 public Info[] getInnerClassesInfo() { 107 Info[] infos = new Info[mInnerClasses.size()]; 108 return (Info[])mInnerClasses.toArray(infos); 109 } 110 111 public int getLength() { 112 return 2 + 8 * mInnerClasses.size(); 113 } 114 115 public void writeDataTo(DataOutput dout) throws IOException { 116 int size = mInnerClasses.size(); 117 dout.writeShort(size); 118 for (int i=0; i<size; i++) { 119 ((Info)mInnerClasses.get(i)).writeTo(dout); 120 } 121 } 122 123 static Attribute define(ConstantPool cp, 124 String name, 125 int length, 126 DataInput din) throws IOException { 127 128 InnerClassesAttr innerClasses = new InnerClassesAttr(cp); 129 130 int size = din.readUnsignedShort(); 131 for (int i=0; i<size; i++) { 132 int inner_index = din.readUnsignedShort(); 133 int outer_index = din.readUnsignedShort(); 134 int name_index = din.readUnsignedShort(); 135 int af = din.readUnsignedShort(); 136 137 ConstantClassInfo inner; 138 if (inner_index == 0) { 139 inner = null; 140 } 141 else { 142 inner = (ConstantClassInfo)cp.getConstant(inner_index); 143 } 144 145 ConstantClassInfo outer; 146 if (outer_index == 0) { 147 outer = null; 148 } 149 else { 150 outer = (ConstantClassInfo)cp.getConstant(outer_index); 151 } 152 153 ConstantUTFInfo innerName; 154 if (name_index == 0) { 155 innerName = null; 156 } 157 else { 158 innerName = (ConstantUTFInfo)cp.getConstant(name_index); 159 } 160 161 Info info = new Info(inner, outer, innerName, af); 162 innerClasses.mInnerClasses.add(info); 163 } 164 165 return innerClasses; 166 } 167 168 public static class Info { 169 private ConstantClassInfo mInner; 170 private ConstantClassInfo mOuter; 171 private ConstantUTFInfo mName; 172 private int mAccessFlags; 173 174 Info(ConstantClassInfo inner, 175 ConstantClassInfo outer, 176 ConstantUTFInfo name, 177 int accessFlags) { 178 179 mInner = inner; 180 mOuter = outer; 181 mName = name; 182 mAccessFlags = accessFlags; 183 } 184 185 188 public ConstantClassInfo getInnerClass() { 189 return mInner; 190 } 191 192 195 public ConstantClassInfo getOuterClass() { 196 return mOuter; 197 } 198 199 202 public String getInnerClassName() { 203 if (mName == null) { 204 return null; 205 } 206 else { 207 return mName.getValue(); 208 } 209 } 210 211 214 public AccessFlags getAccessFlags() { 215 return new AccessFlags(mAccessFlags); 216 } 217 218 public void writeTo(DataOutput dout) throws IOException { 219 if (mInner == null) { 220 dout.writeShort(0); 221 } 222 else { 223 dout.writeShort(mInner.getIndex()); 224 } 225 226 if (mOuter == null) { 227 dout.writeShort(0); 228 } 229 else { 230 dout.writeShort(mOuter.getIndex()); 231 } 232 233 if (mName == null) { 234 dout.writeShort(0); 235 } 236 else { 237 dout.writeShort(mName.getIndex()); 238 } 239 240 dout.writeShort(mAccessFlags); 241 } 242 } 243 } 244 | Popular Tags |