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 package org.apache.bcel.classfile; 20 21 import java.io.DataInput; 22 import java.io.DataOutputStream; 23 import java.io.IOException; 24 25 import org.apache.bcel.Const; 26 import org.apache.bcel.util.Args; 27 28 /** 29 * This class is derived from <em>Attribute</em> and declares this class as 'synthetic', i.e., it needs special 30 * handling. The JVM specification states "A class member that does not appear in the source code must be marked using a 31 * Synthetic attribute." It may appear in the ClassFile attribute table, a field_info table or a method_info table. This 32 * class is intended to be instantiated from the <em>Attribute.readAttribute()</em> method. 33 * 34 * @see Attribute 35 */ 36 public final class Synthetic extends Attribute { 37 38 private byte[] bytes; 39 40 /** 41 * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic". 42 * @param length Content length in bytes - should be zero. 43 * @param bytes Attribute contents 44 * @param constantPool The constant pool this attribute is associated with. 45 */ 46 public Synthetic(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) { 47 super(Const.ATTR_SYNTHETIC, nameIndex, Args.require0(length, "Synthetic attribute length"), constantPool); 48 this.bytes = bytes; 49 } 50 51 /** 52 * Constructs object from input stream. 53 * 54 * @param nameIndex Index in constant pool to CONSTANT_Utf8 55 * @param length Content length in bytes 56 * @param input Input stream 57 * @param constantPool Array of constants 58 * @throws IOException if an I/O error occurs. 59 */ 60 Synthetic(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 61 this(nameIndex, length, (byte[]) null, constantPool); 62 if (length > 0) { 63 bytes = new byte[length]; 64 input.readFully(bytes); 65 println("Synthetic attribute with length > 0"); 66 } 67 } 68 69 /** 70 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a 71 * physical copy. 72 * 73 * @param c Source to copy. 74 */ 75 public Synthetic(final Synthetic c) { 76 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 77 } 78 79 /** 80 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 81 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 82 * 83 * @param v Visitor object 84 */ 85 @Override 86 public void accept(final Visitor v) { 87 v.visitSynthetic(this); 88 } 89 90 /** 91 * @return deep copy of this attribute 92 */ 93 @Override 94 public Attribute copy(final ConstantPool constantPool) { 95 final Synthetic c = (Synthetic) clone(); 96 if (bytes != null) { 97 c.bytes = bytes.clone(); 98 } 99 c.setConstantPool(constantPool); 100 return c; 101 } 102 103 /** 104 * Dump source file attribute to file stream in binary format. 105 * 106 * @param file Output file stream 107 * @throws IOException if an I/O error occurs. 108 */ 109 @Override 110 public void dump(final DataOutputStream file) throws IOException { 111 super.dump(file); 112 if (super.getLength() > 0) { 113 file.write(bytes, 0, super.getLength()); 114 } 115 } 116 117 /** 118 * @return data bytes. 119 */ 120 public byte[] getBytes() { 121 return bytes; 122 } 123 124 /** 125 * @param bytes 126 */ 127 public void setBytes(final byte[] bytes) { 128 this.bytes = bytes; 129 } 130 131 /** 132 * @return String representation. 133 */ 134 @Override 135 public String toString() { 136 final StringBuilder buf = new StringBuilder("Synthetic"); 137 if (super.getLength() > 0) { 138 buf.append(" ").append(Utility.toHexString(bytes)); 139 } 140 return buf.toString(); 141 } 142 }