View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  import org.apache.bcel.util.Args;
27  import org.apache.commons.lang3.ArrayUtils;
28  
29  /**
30   * This class is derived from <em>Attribute</em> and declares this class as 'synthetic', that is, it needs special handling. The JVM specification states "A
31   * class member that does not appear in the source code must be marked using a Synthetic attribute." It may appear in the ClassFile attribute table, a
32   * field_info table or a method_info table. This class is intended to be instantiated from the <em>Attribute.readAttribute()</em> method.
33   *
34   * <pre>
35   *
36   * Synthetic_attribute {
37   *     u2 attribute_name_index;
38   *     u4 attribute_length;
39   * }
40   * </pre>
41   *
42   * @see Attribute
43   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.8">JVM Specification: The Synthetic Attribute</a>
44   */
45  public final class Synthetic extends Attribute {
46  
47      /**
48       * @param nameIndex    Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic".
49       * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
50       * @param bytes        Attribute contents.
51       * @param constantPool The constant pool this attribute is associated with.
52       * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.8">JVM Specification: The Synthetic Attribute</a>
53       */
54      public Synthetic(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
55          super(Const.ATTR_SYNTHETIC, nameIndex, Args.require0(length, "Synthetic attribute length"), constantPool);
56      }
57  
58      /**
59       * Constructs object from input stream.
60       *
61       * @param nameIndex    Index in constant pool to CONSTANT_Utf8.
62       * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
63       * @param input        Input stream.
64       * @param constantPool Array of constants.
65       * @throws IOException if an I/O error occurs.
66       * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.8">JVM Specification: The Synthetic Attribute</a>
67       */
68      Synthetic(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
69          this(nameIndex, length, (byte[]) null, constantPool);
70      }
71  
72      /**
73       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.
74       *
75       * @param c Source to copy.
76       */
77      public Synthetic(final Synthetic c) {
78          this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
79      }
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields,
83       * attributes, etc. spawns a tree of objects.
84       *
85       * @param v Visitor object.
86       */
87      @Override
88      public void accept(final Visitor v) {
89          v.visitSynthetic(this);
90      }
91  
92      /**
93       * @return deep copy of this attribute.
94       */
95      @Override
96      public Attribute copy(final ConstantPool constantPool) {
97          final Synthetic c = (Synthetic) clone();
98          c.setConstantPool(constantPool);
99          return c;
100     }
101 
102     /**
103      * Gets data bytes.
104      *
105      * @return data bytes.
106      */
107     public byte[] getBytes() {
108         return ArrayUtils.EMPTY_BYTE_ARRAY;
109     }
110 
111     /**
112      * Sets data bytes.
113      *
114      * @param bytes data bytes.
115      */
116     public void setBytes(final byte[] bytes) {
117         if (bytes != null) {
118             Args.require0(bytes.length, "Deprecated attribute length");
119         }
120     }
121 
122     /**
123      * @return String representation.
124      */
125     @Override
126     public String toString() {
127         return "Synthetic";
128     }
129 }