Source code: com/techtrader/modules/tools/bytecode/Attribute.java
1 package com.techtrader.modules.tools.bytecode;
2
3
4 import java.lang.reflect.Constructor;
5 import java.io.DataInput;
6 import java.io.DataOutput;
7 import java.io.IOException;
8
9 import com.techtrader.modules.tools.bytecode.lowlevel.ConstantPool;
10 import com.techtrader.modules.tools.bytecode.visitor.VisitAcceptor;
11
12
13 /**
14 * Representation of an attribute in a .class file. Attributes
15 * are used to represent constants and the code of methods, among other things.
16 * All attributes contain at a minimum a name, which is immutable, as it
17 * determines the attribute's type.
18 *
19 * @author Abe White
20 */
21 public abstract class Attribute
22 extends BCEntity
23 implements Constants, VisitAcceptor
24 {
25 protected int _nameIndex = 0;
26 protected BCEntity _owner = null;
27
28
29 /**
30 * Create an attribute of the appropriate type based on the
31 * the attribute name.
32 */
33 protected static Attribute createAttribute (String name, BCEntity owner)
34 {
35 int nameIndex = owner.getPool ().setUTF (0, name);
36
37 // special handling for code
38 if (name.equals (ATTR_CODE))
39 return new Code (nameIndex, owner);
40
41 try
42 {
43 Class type = Class.forName ("com.techtrader.modules.tools.bytecode."
44 + name + "Attribute");
45 Constructor cons = type.getConstructor (new Class[]
46 { int.class, BCEntity.class });
47
48 return (Attribute) cons.newInstance (new Object[]
49 { new Integer (nameIndex), owner });
50 }
51 catch (Throwable t)
52 {
53 return new UnknownAttribute (nameIndex, owner);
54 }
55 }
56
57
58 /**
59 * Protected Constructor.
60 */
61 protected Attribute (int nameIndex, BCEntity owner)
62 {
63 _owner = owner;
64 _nameIndex = nameIndex;
65 }
66
67
68 /**
69 * Invalidate this Attribute.
70 */
71 protected void invalidate ()
72 {
73 _owner = null;
74 }
75
76
77 /**
78 * Each Attribute references the entity that owns it.
79 */
80 public BCEntity getOwner ()
81 {
82 return _owner;
83 }
84
85
86 /**
87 * Return the constant pool index of the UTF entry holding the name
88 * of this attribute.
89 */
90 public int getNameIndex ()
91 {
92 return _nameIndex;
93 }
94
95
96 /**
97 * Return the name of this attribute.
98 */
99 public String getName ()
100 {
101 return _owner.getPool ().getUTF (_nameIndex);
102 }
103
104
105 /**
106 * Implementation of the BCEntity abstract method; delegates to the
107 * owning entity.
108 */
109 public ConstantPool getPool ()
110 {
111 return _owner.getPool ();
112 }
113
114
115 /**
116 * Return the length of the .class representation of this attribute,
117 * in bytes.
118 */
119 public int getLength ()
120 {
121 return 0;
122 }
123
124
125 /**
126 * Copy the information from the given attribute to this one.
127 */
128 protected void copy (Attribute other)
129 {
130 }
131
132
133 /**
134 * Should be overridden by subclasses to read their internal data from
135 * the given stream, up to length bytes, excluding the name index.
136 */
137 protected void readData (DataInput in, int length)
138 throws IOException
139 {
140 }
141
142
143 /**
144 * Should be overridden by subclasses to write their internal data to
145 * the given stream, up to length bytes, excluding the name index.
146 */
147 protected void writeData (DataOutput out, int length)
148 throws IOException
149 {
150 }
151 }