21
22 package com.sun.org.apache.bcel.internal.classfile;
23
24 import java.io.DataInput;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import com.sun.org.apache.bcel.internal.Const;
31
32 /**
33 * This class represents a reference to an unknown (i.e.,
34 * application-specific) attribute of a class. It is instantiated from the
35 * {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method.
36 * Applications that need to read in application-specific attributes should create an
37 * {@link UnknownAttributeReader} implementation and attach it via
38 * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}.
39
40 *
41 * @version $Id$
42 * @see Attribute
43 * @see UnknownAttributeReader
44 */
45 public final class Unknown extends Attribute {
46
47 private byte[] bytes;
48 private final String name;
49 private static final Map<String, Unknown> unknown_attributes = new HashMap<>();
50
51
52 /** @return array of unknown attributes, but just one for each kind.
53 */
54 static Unknown[] getUnknownAttributes() {
55 final Unknown[] unknowns = new Unknown[unknown_attributes.size()];
56 unknown_attributes.values().toArray(unknowns);
57 unknown_attributes.clear();
58 return unknowns;
59 }
60
61
107 /**
108 * Called by objects that are traversing the nodes of the tree implicitely
109 * defined by the contents of a Java class. I.e., the hierarchy of methods,
110 * fields, attributes, etc. spawns a tree of objects.
111 *
112 * @param v Visitor object
113 */
114 @Override
115 public void accept( final Visitor v ) {
116 v.visitUnknown(this);
117 }
118
119
120 /**
121 * Dump unknown bytes to file stream.
122 *
123 * @param file Output file stream
124 * @throws IOException
125 */
126 @Override
127 public final void dump( final DataOutputStream file ) throws IOException {
128 super.dump(file);
129 if (super.getLength() > 0) {
130 file.write(bytes, 0, super.getLength());
131 }
132 }
133
134
135 /**
136 * @return data bytes.
137 */
138 public final byte[] getBytes() {
139 return bytes;
140 }
141
142
143 /**
144 * @return name of attribute.
145 */
146 @Override
147 public final String getName() {
148 return name;
149 }
150
151
152 /**
153 * @param bytes the bytes to set
154 */
155 public final void setBytes( final byte[] bytes ) {
156 this.bytes = bytes;
157 }
158
159
160 /**
161 * @return String representation.
162 */
163 @Override
164 public final String toString() {
165 if (super.getLength() == 0 || bytes == null) {
166 return "(Unknown attribute " + name + ")";
167 }
168 String hex;
169 if (super.getLength() > 10) {
170 final byte[] tmp = new byte[10];
171 System.arraycopy(bytes, 0, tmp, 0, 10);
172 hex = Utility.toHexString(tmp) + "... (truncated)";
173 } else {
174 hex = Utility.toHexString(bytes);
175 }
176 return "(Unknown attribute " + name + ": " + hex + ")";
177 }
178
179
180 /**
181 * @return deep copy of this attribute
182 */
183 @Override
184 public Attribute copy( final ConstantPool _constant_pool ) {
|
21
22 package com.sun.org.apache.bcel.internal.classfile;
23
24 import java.io.DataInput;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import com.sun.org.apache.bcel.internal.Const;
31
32 /**
33 * This class represents a reference to an unknown (i.e.,
34 * application-specific) attribute of a class. It is instantiated from the
35 * {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method.
36 * Applications that need to read in application-specific attributes should create an
37 * {@link UnknownAttributeReader} implementation and attach it via
38 * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}.
39
40 *
41 * @see Attribute
42 * @see UnknownAttributeReader
43 */
44 public final class Unknown extends Attribute {
45
46 private byte[] bytes;
47 private final String name;
48 private static final Map<String, Unknown> unknown_attributes = new HashMap<>();
49
50
51 /** @return array of unknown attributes, but just one for each kind.
52 */
53 static Unknown[] getUnknownAttributes() {
54 final Unknown[] unknowns = new Unknown[unknown_attributes.size()];
55 unknown_attributes.values().toArray(unknowns);
56 unknown_attributes.clear();
57 return unknowns;
58 }
59
60
106 /**
107 * Called by objects that are traversing the nodes of the tree implicitely
108 * defined by the contents of a Java class. I.e., the hierarchy of methods,
109 * fields, attributes, etc. spawns a tree of objects.
110 *
111 * @param v Visitor object
112 */
113 @Override
114 public void accept( final Visitor v ) {
115 v.visitUnknown(this);
116 }
117
118
119 /**
120 * Dump unknown bytes to file stream.
121 *
122 * @param file Output file stream
123 * @throws IOException
124 */
125 @Override
126 public void dump( final DataOutputStream file ) throws IOException {
127 super.dump(file);
128 if (super.getLength() > 0) {
129 file.write(bytes, 0, super.getLength());
130 }
131 }
132
133
134 /**
135 * @return data bytes.
136 */
137 public byte[] getBytes() {
138 return bytes;
139 }
140
141
142 /**
143 * @return name of attribute.
144 */
145 @Override
146 public String getName() {
147 return name;
148 }
149
150
151 /**
152 * @param bytes the bytes to set
153 */
154 public void setBytes( final byte[] bytes ) {
155 this.bytes = bytes;
156 }
157
158
159 /**
160 * @return String representation.
161 */
162 @Override
163 public String toString() {
164 if (super.getLength() == 0 || bytes == null) {
165 return "(Unknown attribute " + name + ")";
166 }
167 String hex;
168 if (super.getLength() > 10) {
169 final byte[] tmp = new byte[10];
170 System.arraycopy(bytes, 0, tmp, 0, 10);
171 hex = Utility.toHexString(tmp) + "... (truncated)";
172 } else {
173 hex = Utility.toHexString(bytes);
174 }
175 return "(Unknown attribute " + name + ": " + hex + ")";
176 }
177
178
179 /**
180 * @return deep copy of this attribute
181 */
182 @Override
183 public Attribute copy( final ConstantPool _constant_pool ) {
|