001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025 026/** 027 * This class is derived from <em>Attribute</em> and declares this class as `synthetic', i.e., it needs special 028 * handling. The JVM specification states "A class member that does not appear in the source code must be marked using a 029 * Synthetic attribute." It may appear in the ClassFile attribute table, a field_info table or a method_info table. This 030 * class is intended to be instantiated from the <em>Attribute.readAttribute()</em> method. 031 * 032 * @see Attribute 033 */ 034public final class Synthetic extends Attribute { 035 036 private byte[] bytes; 037 038 /** 039 * @param name_index Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic". 040 * @param length Content length in bytes - should be zero. 041 * @param bytes Attribute contents 042 * @param constant_pool The constant pool this attribute is associated with. 043 */ 044 public Synthetic(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) { 045 super(Const.ATTR_SYNTHETIC, name_index, length, constant_pool); 046 this.bytes = bytes; 047 } 048 049 /** 050 * Construct object from input stream. 051 * 052 * @param name_index Index in constant pool to CONSTANT_Utf8 053 * @param length Content length in bytes 054 * @param input Input stream 055 * @param constant_pool Array of constants 056 * @throws IOException if an I/O error occurs. 057 */ 058 Synthetic(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 059 this(name_index, length, (byte[]) null, constant_pool); 060 if (length > 0) { 061 bytes = new byte[length]; 062 input.readFully(bytes); 063 println("Synthetic attribute with length > 0"); 064 } 065 } 066 067 /** 068 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a 069 * physical copy. 070 */ 071 public Synthetic(final Synthetic c) { 072 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 073 } 074 075 /** 076 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 077 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 078 * 079 * @param v Visitor object 080 */ 081 @Override 082 public void accept(final Visitor v) { 083 v.visitSynthetic(this); 084 } 085 086 /** 087 * @return deep copy of this attribute 088 */ 089 @Override 090 public Attribute copy(final ConstantPool constantPool) { 091 final Synthetic c = (Synthetic) clone(); 092 if (bytes != null) { 093 c.bytes = new byte[bytes.length]; 094 System.arraycopy(bytes, 0, c.bytes, 0, bytes.length); 095 } 096 c.setConstantPool(constantPool); 097 return c; 098 } 099 100 /** 101 * Dump source file attribute to file stream in binary format. 102 * 103 * @param file Output file stream 104 * @throws IOException if an I/O error occurs. 105 */ 106 @Override 107 public void dump(final DataOutputStream file) throws IOException { 108 super.dump(file); 109 if (super.getLength() > 0) { 110 file.write(bytes, 0, super.getLength()); 111 } 112 } 113 114 /** 115 * @return data bytes. 116 */ 117 public byte[] getBytes() { 118 return bytes; 119 } 120 121 /** 122 * @param bytes 123 */ 124 public void setBytes(final byte[] bytes) { 125 this.bytes = bytes; 126 } 127 128 /** 129 * @return String representation. 130 */ 131 @Override 132 public String toString() { 133 final StringBuilder buf = new StringBuilder("Synthetic"); 134 if (super.getLength() > 0) { 135 buf.append(" ").append(Utility.toHexString(bytes)); 136 } 137 return buf.toString(); 138 } 139}