| 1 9 package com.vladium.jcd.cls.attribute; 10 11 import java.io.IOException ; 12 13 import com.vladium.jcd.cls.ClassDef; 14 import com.vladium.jcd.cls.IConstantCollection; 15 import com.vladium.jcd.cls.constant.*; 16 import com.vladium.jcd.compiler.IClassFormatOutput; 17 import com.vladium.jcd.lib.UDataInputStream; 18 import com.vladium.jcd.lib.UDataOutputStream; 19 20 49 public 50 abstract class Attribute_info implements Cloneable , IClassFormatOutput 51 { 52 54 public static final String ATTRIBUTE_CODE = "Code"; 55 public static final String ATTRIBUTE_CONSTANT_VALUE = "ConstantValue"; 56 public static final String ATTRIBUTE_LINE_NUMBER_TABLE = "LineNumberTable"; 57 public static final String ATTRIBUTE_EXCEPTIONS = "Exceptions"; 58 public static final String ATTRIBUTE_SYNTHETIC = "Synthetic"; 59 public static final String ATTRIBUTE_BRIDGE = "Bridge"; 60 public static final String ATTRIBUTE_SOURCEFILE = "SourceFile"; 61 public static final String ATTRIBUTE_INNERCLASSES = "InnerClasses"; 62 63 67 public int m_name_index; 68 69 76 public String getName (final ClassDef cls) 77 { 78 return ((CONSTANT_Utf8_info) cls.getConstants ().get (m_name_index)).m_value; 79 } 80 81 85 public abstract long length (); 87 89 public abstract void accept (IAttributeVisitor visitor, Object ctx); 90 91 public abstract String toString (); 92 93 105 public static Attribute_info new_Attribute_info (final IConstantCollection constants, 106 final UDataInputStream bytes) 107 throws IOException  108 { 109 final int attribute_name_index = bytes.readU2 (); 110 final long attribute_length = bytes.readU4 (); 111 112 final CONSTANT_Utf8_info attribute_name = (CONSTANT_Utf8_info) constants.get (attribute_name_index); 113 final String name = attribute_name.m_value; 114 115 if (ATTRIBUTE_CODE.equals (name)) 116 { 117 return new CodeAttribute_info (constants, attribute_name_index, attribute_length, bytes); 118 } 119 else if (ATTRIBUTE_CONSTANT_VALUE.equals (name)) 120 { 121 return new ConstantValueAttribute_info (attribute_name_index, attribute_length, bytes); 122 } 123 else if (ATTRIBUTE_EXCEPTIONS.equals (name)) 124 { 125 return new ExceptionsAttribute_info (attribute_name_index, attribute_length, bytes); 126 } 127 else if (ATTRIBUTE_INNERCLASSES.equals (name)) 128 { 129 return new InnerClassesAttribute_info (attribute_name_index, attribute_length, bytes); 130 } 131 else if (ATTRIBUTE_SYNTHETIC.equals (name)) 132 { 133 return new SyntheticAttribute_info (attribute_name_index, attribute_length); 134 } 135 else if (ATTRIBUTE_BRIDGE.equals (name)) 136 { 137 return new BridgeAttribute_info (attribute_name_index, attribute_length); 138 } 139 else if (ATTRIBUTE_LINE_NUMBER_TABLE.equals (name)) 140 { 141 return new LineNumberTableAttribute_info (attribute_name_index, attribute_length, bytes); 142 } 143 else if (ATTRIBUTE_SOURCEFILE.equals (name)) 144 { 145 return new SourceFileAttribute_info (attribute_name_index, attribute_length, bytes); 146 } 147 else 148 { 149 return new GenericAttribute_info (attribute_name_index, attribute_length, bytes); 151 } 152 } 153 154 156 160 public Object clone () 161 { 162 try 163 { 164 return super.clone (); 165 } 166 catch (CloneNotSupportedException e) 167 { 168 throw new InternalError (e.toString ()); 169 } 170 } 171 172 174 public void writeInClassFormat (UDataOutputStream out) throws IOException  175 { 176 out.writeU2 (m_name_index); 177 out.writeU4 (length () - 6); } 179 180 182 189 190 protected Attribute_info (final int attribute_name_index, final long attribute_length) 191 { 192 m_name_index = attribute_name_index; 193 m_attribute_length = attribute_length; 194 } 195 196 protected long m_attribute_length; 199 201 203 } | Popular Tags |