View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.bcel.Const;
27  
28  /**
29   * This class represents a reference to an unknown (i.e.,
30   * application-specific) attribute of a class.  It is instantiated from the
31   * {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method.
32   * Applications that need to read in application-specific attributes should create an
33   * {@link UnknownAttributeReader} implementation and attach it via
34   * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}.
35  
36   *
37   * @see Attribute
38   * @see UnknownAttributeReader
39   */
40  public final class Unknown extends Attribute {
41  
42      private byte[] bytes;
43      private final String name;
44      private static final Map<String, Unknown> unknownAttributes = new HashMap<>();
45  
46  
47      /** @return array of unknown attributes, but just one for each kind.
48       */
49      static Unknown[] getUnknownAttributes() {
50          final Unknown.html#Unknown">Unknown[] unknowns = new Unknown[unknownAttributes.size()];
51          unknownAttributes.values().toArray(unknowns);
52          unknownAttributes.clear();
53          return unknowns;
54      }
55  
56  
57      /**
58       * Initialize from another object. Note that both objects use the same
59       * references (shallow copy). Use clone() for a physical copy.
60       */
61      public Unknownile/Unknown.html#Unknown">Unknown(final Unknown c) {
62          this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
63      }
64  
65  
66      /**
67       * Create a non-standard attribute.
68       *
69       * @param name_index Index in constant pool
70       * @param length Content length in bytes
71       * @param bytes Attribute contents
72       * @param constant_pool Array of constants
73       */
74      public Unknown(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
75          super(Const.ATTR_UNKNOWN, name_index, length, constant_pool);
76          this.bytes = bytes;
77          name = ((ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8))
78                  .getBytes();
79          unknownAttributes.put(name, this);
80      }
81  
82  
83      /**
84       * Construct object from input stream.
85       *
86       * @param name_index Index in constant pool
87       * @param length Content length in bytes
88       * @param input Input stream
89       * @param constant_pool Array of constants
90       * @throws IOException
91       */
92      Unknown(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
93              throws IOException {
94          this(name_index, length, (byte[]) null, constant_pool);
95          if (length > 0) {
96              bytes = new byte[length];
97              input.readFully(bytes);
98          }
99      }
100 
101 
102     /**
103      * Called by objects that are traversing the nodes of the tree implicitely
104      * defined by the contents of a Java class. I.e., the hierarchy of methods,
105      * fields, attributes, etc. spawns a tree of objects.
106      *
107      * @param v Visitor object
108      */
109     @Override
110     public void accept( final Visitor v ) {
111         v.visitUnknown(this);
112     }
113 
114 
115     /**
116      * Dump unknown bytes to file stream.
117      *
118      * @param file Output file stream
119      * @throws IOException
120      */
121     @Override
122     public void dump( final DataOutputStream file ) throws IOException {
123         super.dump(file);
124         if (super.getLength() > 0) {
125             file.write(bytes, 0, super.getLength());
126         }
127     }
128 
129 
130     /**
131      * @return data bytes.
132      */
133     public byte[] getBytes() {
134         return bytes;
135     }
136 
137 
138     /**
139      * @return name of attribute.
140      */
141     @Override
142     public String getName() {
143         return name;
144     }
145 
146 
147     /**
148      * @param bytes the bytes to set
149      */
150     public void setBytes( final byte[] bytes ) {
151         this.bytes = bytes;
152     }
153 
154 
155     /**
156      * @return String representation.
157      */
158     @Override
159     public String toString() {
160         if (super.getLength() == 0 || bytes == null) {
161             return "(Unknown attribute " + name + ")";
162         }
163         String hex;
164         if (super.getLength() > 10) {
165             final byte[] tmp = new byte[10];
166             System.arraycopy(bytes, 0, tmp, 0, 10);
167             hex = Utility.toHexString(tmp) + "... (truncated)";
168         } else {
169             hex = Utility.toHexString(bytes);
170         }
171         return "(Unknown attribute " + name + ": " + hex + ")";
172     }
173 
174 
175     /**
176      * @return deep copy of this attribute
177      */
178     @Override
179     public Attribute copy( final ConstantPool _constant_pool ) {
180         final Unknownef="../../../../org/apache/bcel/classfile/Unknown.html#Unknown">Unknown c = (Unknown) clone();
181         if (bytes != null) {
182             c.bytes = new byte[bytes.length];
183             System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
184         }
185         c.setConstantPool(_constant_pool);
186         return c;
187     }
188 }