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 */ 017package org.apache.bcel.classfile; 018 019import java.io.DataInput; 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.Const; 024 025/** 026 * This class is derived from <em>Attribute</em> and declares this class as `synthetic', i.e., it needs special 027 * handling. The JVM specification states "A class member that does not appear in the source code must be marked using a 028 * Synthetic attribute." It may appear in the ClassFile attribute table, a field_info table or a method_info table. This 029 * class is intended to be instantiated from the <em>Attribute.readAttribute()</em> method. 030 * 031 * @see Attribute 032 */ 033public final class Synthetic extends Attribute { 034 035 private byte[] bytes; 036 037 /** 038 * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic". 039 * @param length Content length in bytes - should be zero. 040 * @param bytes Attribute contents 041 * @param constantPool The constant pool this attribute is associated with. 042 */ 043 public Synthetic(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) { 044 super(Const.ATTR_SYNTHETIC, nameIndex, length, constantPool); 045 this.bytes = bytes; 046 } 047 048 /** 049 * Construct object from input stream. 050 * 051 * @param nameIndex Index in constant pool to CONSTANT_Utf8 052 * @param length Content length in bytes 053 * @param input Input stream 054 * @param constantPool Array of constants 055 * @throws IOException if an I/O error occurs. 056 */ 057 Synthetic(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 058 this(nameIndex, length, (byte[]) null, constantPool); 059 if (length > 0) { 060 bytes = new byte[length]; 061 input.readFully(bytes); 062 println("Synthetic attribute with length > 0"); 063 } 064 } 065 066 /** 067 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a 068 * physical copy. 069 */ 070 public Synthetic(final Synthetic c) { 071 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 072 } 073 074 /** 075 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 076 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 077 * 078 * @param v Visitor object 079 */ 080 @Override 081 public void accept(final Visitor v) { 082 v.visitSynthetic(this); 083 } 084 085 /** 086 * @return deep copy of this attribute 087 */ 088 @Override 089 public Attribute copy(final ConstantPool constantPool) { 090 final Synthetic c = (Synthetic) clone(); 091 if (bytes != null) { 092 c.bytes = bytes.clone(); 093 } 094 c.setConstantPool(constantPool); 095 return c; 096 } 097 098 /** 099 * Dump source file attribute to file stream in binary format. 100 * 101 * @param file Output file stream 102 * @throws IOException if an I/O error occurs. 103 */ 104 @Override 105 public void dump(final DataOutputStream file) throws IOException { 106 super.dump(file); 107 if (super.getLength() > 0) { 108 file.write(bytes, 0, super.getLength()); 109 } 110 } 111 112 /** 113 * @return data bytes. 114 */ 115 public byte[] getBytes() { 116 return bytes; 117 } 118 119 /** 120 * @param bytes 121 */ 122 public void setBytes(final byte[] bytes) { 123 this.bytes = bytes; 124 } 125 126 /** 127 * @return String representation. 128 */ 129 @Override 130 public String toString() { 131 final StringBuilder buf = new StringBuilder("Synthetic"); 132 if (super.getLength() > 0) { 133 buf.append(" ").append(Utility.toHexString(bytes)); 134 } 135 return buf.toString(); 136 } 137}