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  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and declares this class as
28   * `synthetic', i.e., it needs special handling.  The JVM specification
29   * states "A class member that does not appear in the source code must be
30   * marked using a Synthetic attribute."  It may appear in the ClassFile
31   * attribute table, a field_info table or a method_info table.  This class
32   * is intended to be instantiated from the
33   * <em>Attribute.readAttribute()</em> method.
34   *
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 Synthetice/Synthetic.html#Synthetic">Synthetic(final 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(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
60          super(Const.ATTR_SYNTHETIC, name_index, length, constant_pool);
61          this.bytes = bytes;
62      }
63  
64  
65      /**
66       * Construct object from input stream.
67       *
68       * @param name_index Index in constant pool to CONSTANT_Utf8
69       * @param length Content length in bytes
70       * @param input Input stream
71       * @param constant_pool Array of constants
72       * @throws IOException
73       */
74      Synthetic(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
75              throws IOException {
76          this(name_index, length, (byte[]) null, constant_pool);
77          if (length > 0) {
78              bytes = new byte[length];
79              input.readFully(bytes);
80              println("Synthetic attribute with length > 0");
81          }
82      }
83  
84  
85      /**
86       * Called by objects that are traversing the nodes of the tree implicitely
87       * defined by the contents of a Java class. I.e., the hierarchy of methods,
88       * fields, attributes, etc. spawns a tree of objects.
89       *
90       * @param v Visitor object
91       */
92      @Override
93      public void accept( final Visitor v ) {
94          v.visitSynthetic(this);
95      }
96  
97  
98      /**
99       * Dump source file attribute to file stream in binary format.
100      *
101      * @param file Output file stream
102      * @throws IOException
103      */
104     @Override
105     public void dump( final DataOutputStream file ) throws IOException {
106         super.dump(file);
107         if (super.getLength() > 0) {
108             file.write(bytes, 0, super.getLength());
109         }
110     }
111 
112 
113     /**
114      * @return data bytes.
115      */
116     public byte[] getBytes() {
117         return bytes;
118     }
119 
120 
121     /**
122      * @param bytes
123      */
124     public void setBytes( final byte[] bytes ) {
125         this.bytes = bytes;
126     }
127 
128 
129     /**
130      * @return String representation.
131      */
132     @Override
133     public String toString() {
134         final StringBuilder buf = new StringBuilder("Synthetic");
135         if (super.getLength() > 0) {
136             buf.append(" ").append(Utility.toHexString(bytes));
137         }
138         return buf.toString();
139     }
140 
141 
142     /**
143      * @return deep copy of this attribute
144      */
145     @Override
146     public Attribute copy( final ConstantPool _constant_pool ) {
147         final Synthetic="../../../../org/apache/bcel/classfile/Synthetic.html#Synthetic">Synthetic c = (Synthetic) clone();
148         if (bytes != null) {
149             c.bytes = new byte[bytes.length];
150             System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
151         }
152         c.setConstantPool(_constant_pool);
153         return c;
154     }
155 }