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 * Constructs a Synthetic attribute.
49 *
50 * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic".
51 * @param length JVM Specification: "The value of the attribute_length item must be zero.".
52 * @param bytes Attribute contents.
53 * @param constantPool The constant pool this attribute is associated with.
54 * @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>
55 */
56 public Synthetic(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
57 super(Const.ATTR_SYNTHETIC, nameIndex, Args.require0(length, "Synthetic attribute length"), constantPool);
58 }
59
60 /**
61 * Constructs object from input stream.
62 *
63 * @param nameIndex Index in constant pool to CONSTANT_Utf8.
64 * @param length JVM Specification: "The value of the attribute_length item must be zero.".
65 * @param input Input stream.
66 * @param constantPool Array of constants.
67 * @throws IOException Thrown if an I/O error occurs.
68 * @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>
69 */
70 Synthetic(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
71 this(nameIndex, length, (byte[]) null, constantPool);
72 }
73
74 /**
75 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.
76 *
77 * @param c Source to copy.
78 */
79 public Synthetic(final Synthetic c) {
80 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
81 }
82
83 /**
84 * 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,
85 * attributes, etc. spawns a tree of objects.
86 *
87 * @param v Visitor object.
88 */
89 @Override
90 public void accept(final Visitor v) {
91 v.visitSynthetic(this);
92 }
93
94 /**
95 * @return deep copy of this attribute.
96 */
97 @Override
98 public Attribute copy(final ConstantPool constantPool) {
99 final Synthetic c = (Synthetic) clone();
100 c.setConstantPool(constantPool);
101 return c;
102 }
103
104 /**
105 * Gets data bytes.
106 *
107 * @return data bytes.
108 */
109 public byte[] getBytes() {
110 return ArrayUtils.EMPTY_BYTE_ARRAY;
111 }
112
113 /**
114 * Sets data bytes.
115 *
116 * @param bytes data bytes.
117 */
118 public void setBytes(final byte[] bytes) {
119 if (bytes != null) {
120 Args.require0(bytes.length, "Deprecated attribute length");
121 }
122 }
123
124 /**
125 * @return String representation.
126 */
127 @Override
128 public String toString() {
129 return "Synthetic";
130 }
131 }