1 24 25 package org.aspectj.compiler.base.bytecode; 26 27 import java.io.*; 28 import java.lang.reflect.Modifier ; 29 30 class Attributes { 31 Attribute[] data; 33 ClassFile classFile; 34 35 static Attributes createFrom(ClassFile classFile, DataInputStream stream) throws IOException { 36 Attributes attributes = new Attributes(); 37 attributes.classFile = classFile; 38 attributes.readFrom(stream); 39 return attributes; 40 } 41 42 43 public Attribute findAttribute(Class ofType) { 44 for(int i=0; i<data.length; i++) { 45 if (ofType.isInstance(data[i])) { 46 return data[i]; 47 } 48 } 49 return null; 50 } 51 52 void readFrom(DataInputStream stream) throws IOException { 53 int attributeCount = stream.readUnsignedShort(); 54 data = new Attribute[attributeCount]; 56 for(int index = 0; index<attributeCount; index++) { 57 int nameIndex = stream.readUnsignedShort(); 58 long attributeLength = (256*256)*stream.readUnsignedShort() + 60 stream.readUnsignedShort(); 61 String name = classFile.pool.getUTF(nameIndex).value; 62 63 Attribute attribute; 64 65 if (name.equals("SourceFile")) { 66 attribute = new SourceFileAttribute(); 67 attribute.readFrom(stream); 68 } else if (name.equals("Deprecated")) { 69 attribute = new DeprecatedAttribute(); 70 attribute.readFrom(stream); 71 } else if (name.equals("Exceptions")) { 72 attribute = new ExceptionsAttribute(); 73 attribute.readFrom(stream); 74 } else if (name.equals("ConstantValue")) { 75 attribute = new ConstantValueAttribute(); 76 attribute.readFrom(stream); 77 } else if (name.equals("InnerClasses")) { 78 attribute = new InnerClassesAttribute(); 79 attribute.readFrom(stream); 80 } else { 81 attribute = new UnknownAttribute(name, attributeLength); 82 attribute.readFrom(stream); 83 } 84 data[index] = attribute; 85 } 86 } 87 88 public String toString() { 89 StringBuffer buf = new StringBuffer (); 90 buf.append("Attributes("); 91 for(int i=0; i<data.length; i++) { 92 if (i > 0) buf.append(", "); 93 buf.append(data[i]); 94 } 95 buf.append(")"); 96 return buf.toString(); 97 } 98 99 abstract class Attribute { 100 abstract void readFrom(DataInputStream stream) throws IOException; 101 } 102 103 class SourceFileAttribute extends Attribute { 104 ConstantPool.UTF filename; 105 106 void readFrom(DataInputStream stream) throws IOException { 107 filename = classFile.pool.getUTF(stream.readUnsignedShort()); 108 } 109 110 public String toString() { 111 return "SourceFile("+filename.value+")"; 112 } 113 } 114 115 public class ExceptionsAttribute extends Attribute { 116 public ConstantPool.Class[] exceptions; 117 118 void readFrom(DataInputStream stream) throws IOException { 119 int n = stream.readUnsignedShort(); 120 exceptions = new ConstantPool.Class[n]; 121 for(int i=0; i<n; i++) { 122 exceptions[i] = classFile.pool.getClass(stream.readUnsignedShort()); 123 } 124 } 125 126 public String toString() { 127 return "Exceptions("+exceptions.length+")"; 128 } 129 } 130 131 class DeprecatedAttribute extends Attribute { 132 void readFrom(DataInputStream stream) throws IOException { 133 ; 134 } 135 136 public String toString() { 137 return "Deprecated()"; 138 } 139 } 140 141 class ConstantValueAttribute extends Attribute { 142 public ConstantPool.Entry value; 143 144 void readFrom(DataInputStream stream) throws IOException { 145 value = classFile.pool.getEntry(stream.readUnsignedShort()); 146 } 147 148 public String toString() { 149 return "ConstantValue(...)"; 150 } 151 } 152 153 class InnerClassesAttribute extends Attribute { 154 public ConstantPool.Class[] innerClasses; 155 public ConstantPool.Class[] outerClasses; 156 public ConstantPool.UTF[] innerNames; 157 public int[] innerAccessFlags; 158 159 void readFrom(DataInputStream stream) throws IOException { 160 int size = stream.readUnsignedShort(); 161 innerClasses = new ConstantPool.Class[size]; 162 outerClasses = new ConstantPool.Class[size]; 163 innerNames = new ConstantPool.UTF[size]; 164 innerAccessFlags = new int[size]; 165 166 for (int i = 0; i < size; i++) { 167 innerClasses[i] = classFile.pool.getClass(stream.readUnsignedShort()); 168 outerClasses[i] = classFile.pool.getClass(stream.readUnsignedShort()); 169 innerNames[i] = classFile.pool.getUTF(stream.readUnsignedShort()); 170 innerAccessFlags[i] = stream.readUnsignedShort(); 171 } 172 } 173 174 public String toString() { 175 return "InnerClassesAttribute(...)"; 176 } 177 178 public boolean isPackageLevel(String str) { 179 int len = innerAccessFlags.length; 180 for (int i = 0; i < len; i++) { 181 ConstantPool.Class inner = innerClasses[i]; 182 if (str.equals(inner.getName())) return false; 183 } 184 return true; 185 } 186 187 public int getModifiers(String str) { 188 int len = innerAccessFlags.length; 189 for (int i = 0; i < len; i++) { 190 ConstantPool.Class inner = innerClasses[i]; 191 if (str.equals(inner.getName())) return innerAccessFlags[i]; 192 } 193 throw new RuntimeException ("Unknown non-package-level class " + str); 194 } 195 196 public java.util.Set getContainedTypeNames(String str) { 197 java.util.HashSet set = new java.util.HashSet (); 198 int len = innerAccessFlags.length; 199 for (int i = 0; i < len; i++) { 200 ConstantPool.Class outer = outerClasses[i]; 201 if (outer == null) continue; 202 ConstantPool.UTF name = innerNames[i]; 203 if (name == null) continue; 204 if (str.equals(outer.getName())) { 205 set.add(name.value); 206 } 207 } 208 return set; 209 } 210 211 236 } 237 238 class UnknownAttribute extends Attribute { 239 String name; 240 long size; 241 public UnknownAttribute(String name, long size) { 242 this.name = name; 243 this.size = size; 244 } 245 void readFrom(DataInputStream stream) throws IOException { 246 stream.skip(size); 247 } 248 249 public String toString() { 250 return "UnknownAttribute("+name+", "+size+")"; 251 } 252 } 253 } 254 | Popular Tags |