1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import org.apache.bcel.Constants;
23
24 /***
25 * This class is derived from <em>Attribute</em> and declares this class as
26 * `synthetic', i.e., it needs special handling. The JVM specification
27 * states "A class member that does not appear in the source code must be
28 * marked using a Synthetic attribute." It may appear in the ClassFile
29 * attribute table, a field_info table or a method_info table. This class
30 * is intended to be instantiated from the
31 * <em>Attribute.readAttribute()</em> method.
32 *
33 * @version $Id: Synthetic.java 386056 2006-03-15 11:31:56Z tcurdt $
34 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
35 * @see Attribute
36 */
37 public final class Synthetic extends Attribute {
38
39 private byte[] bytes;
40
41
42 /***
43 * Initialize from another object. Note that both objects use the same
44 * references (shallow copy). Use copy() for a physical copy.
45 */
46 public Synthetic(Synthetic c) {
47 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
48 }
49
50
51 /***
52 * @param name_index Index in constant pool to CONSTANT_Utf8, which
53 * should represent the string "Synthetic".
54 * @param length Content length in bytes - should be zero.
55 * @param bytes Attribute contents
56 * @param constant_pool The constant pool this attribute is associated
57 * with.
58 */
59 public Synthetic(int name_index, int length, byte[] bytes, ConstantPool constant_pool) {
60 super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool);
61 this.bytes = bytes;
62 }
63
64
65 /***
66 * Construct object from file stream.
67 * @param name_index Index in constant pool to CONSTANT_Utf8
68 * @param length Content length in bytes
69 * @param file Input stream
70 * @param constant_pool Array of constants
71 * @throws IOException
72 */
73 Synthetic(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
74 throws IOException {
75 this(name_index, length, (byte[]) null, constant_pool);
76 if (length > 0) {
77 bytes = new byte[length];
78 file.readFully(bytes);
79 System.err.println("Synthetic attribute with length > 0");
80 }
81 }
82
83
84 /***
85 * Called by objects that are traversing the nodes of the tree implicitely
86 * defined by the contents of a Java class. I.e., the hierarchy of methods,
87 * fields, attributes, etc. spawns a tree of objects.
88 *
89 * @param v Visitor object
90 */
91 public void accept( Visitor v ) {
92 v.visitSynthetic(this);
93 }
94
95
96 /***
97 * Dump source file attribute to file stream in binary format.
98 *
99 * @param file Output file stream
100 * @throws IOException
101 */
102 public final void dump( DataOutputStream file ) throws IOException {
103 super.dump(file);
104 if (length > 0) {
105 file.write(bytes, 0, length);
106 }
107 }
108
109
110 /***
111 * @return data bytes.
112 */
113 public final byte[] getBytes() {
114 return bytes;
115 }
116
117
118 /***
119 * @param bytes
120 */
121 public final void setBytes( byte[] bytes ) {
122 this.bytes = bytes;
123 }
124
125
126 /***
127 * @return String representation.
128 */
129 public final String toString() {
130 StringBuffer buf = new StringBuffer("Synthetic");
131 if (length > 0) {
132 buf.append(" ").append(Utility.toHexString(bytes));
133 }
134 return buf.toString();
135 }
136
137
138 /***
139 * @return deep copy of this attribute
140 */
141 public Attribute copy( ConstantPool _constant_pool ) {
142 Synthetic c = (Synthetic) clone();
143 if (bytes != null) {
144 c.bytes = new byte[bytes.length];
145 System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
146 }
147 c.constant_pool = _constant_pool;
148 return c;
149 }
150 }