1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 37 import org.apache.log4j.*; 38 39 public class AttributeFactory { 40 private static final String CONSTANT_VALUE = "ConstantValue"; 41 private static final String CODE = "Code"; 42 private static final String EXCEPTIONS = "Exceptions"; 43 private static final String INNER_CLASSES = "InnerClasses"; 44 private static final String SYNTHETIC = "Synthetic"; 45 private static final String SOURCE_FILE = "SourceFile"; 46 private static final String LINE_NUMBER_TABLE = "LineNumberTable"; 47 private static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable"; 48 private static final String DEPRECATED = "Deprecated"; 49 50 public static Attribute_info create(Classfile classfile, Visitable owner, DataInputStream in) throws IOException { 51 Attribute_info result = null; 52 53 int nameIndex = in.readUnsignedShort(); 54 if (nameIndex > 0) { 55 Object entry = classfile.getConstantPool().get(nameIndex); 56 57 if (entry instanceof UTF8_info) { 58 String name = ((UTF8_info) entry).getValue(); 59 Logger.getLogger(AttributeFactory.class).debug("Attribute name index: " + nameIndex + " (" + name + ")"); 60 61 if (CONSTANT_VALUE.equals(name)) { 62 result = new ConstantValue_attribute(classfile, owner, in); 63 } else if (CODE.equals(name)) { 64 result = new Code_attribute(classfile, owner, in); 65 } else if (EXCEPTIONS.equals(name)) { 66 result = new Exceptions_attribute(classfile, owner, in); 67 } else if (INNER_CLASSES.equals(name)) { 68 result = new InnerClasses_attribute(classfile, owner, in); 69 } else if (SYNTHETIC.equals(name)) { 70 result = new Synthetic_attribute(classfile, owner, in); 71 } else if (SOURCE_FILE.equals(name)) { 72 result = new SourceFile_attribute(classfile, owner, in); 73 } else if (LINE_NUMBER_TABLE.equals(name)) { 74 result = new LineNumberTable_attribute(classfile, owner, in); 75 } else if (LOCAL_VARIABLE_TABLE.equals(name)) { 76 result = new LocalVariableTable_attribute(classfile, owner, in); 77 } else if (DEPRECATED.equals(name)) { 78 result = new Deprecated_attribute(classfile, owner, in); 79 } else { 80 Logger.getLogger(AttributeFactory.class).warn("Unknown attribute name \"" + name + "\""); 81 result = new Custom_attribute(name, classfile, owner, in); 82 } 83 } else { 84 Logger.getLogger(AttributeFactory.class).debug("Attribute name: " + entry); 85 86 Logger.getLogger(AttributeFactory.class).warn("Unknown attribute with invalid name"); 87 result = new Custom_attribute(classfile, owner, in); 88 } 89 } else { 90 Logger.getLogger(AttributeFactory.class).debug("Attribute name index: " + nameIndex); 91 92 Logger.getLogger(AttributeFactory.class).warn("Unknown attribute with no name"); 93 result = new Custom_attribute(classfile, owner, in); 94 } 95 96 return result; 97 } 98 } 99 | Popular Tags |