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