1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 25 import com.sun.org.apache.bcel.internal.Constants; 26 import java.io.*; 27 28 /** 29 * This class is derived from <em>Attribute</em> and declares this class as 30 * `synthetic', i.e., it needs special handling. The JVM specification 31 * states "A class member that does not appear in the source code must be 32 * marked using a Synthetic attribute." It may appear in the ClassFile 33 * attribute table, a field_info table or a method_info table. This class 34 * is intended to be instantiated from the 35 * <em>Attribute.readAttribute()</em> method. 36 * 37 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 38 * @see Attribute 39 */ 40 public final class Synthetic extends Attribute { 41 private byte[] bytes; 42 43 /** 44 * Initialize from another object. Note that both objects use the same 45 * references (shallow copy). Use copy() for a physical copy. 46 */ 47 public Synthetic(Synthetic c) { 48 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 49 } 50 51 /** 52 * @param name_index Index in constant pool to CONSTANT_Utf8, which 53 * should represent the string "Synthetic". 54 * @param length Content length in bytes - should be zero. 55 * @param bytes Attribute contents 56 * @param constant_pool The constant pool this attribute is associated 57 * with. 58 */ 59 public Synthetic(int name_index, int length, byte[] bytes, 60 ConstantPool constant_pool) 61 { 62 super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool); 63 this.bytes = bytes; 64 } 65 66 /** 67 * Construct object from file stream. 68 * @param name_index Index in constant pool to CONSTANT_Utf8 69 * @param length Content length in bytes 70 * @param file Input stream 71 * @param constant_pool Array of constants 72 * @throws IOException 73 */ 74 Synthetic(int name_index, int length, DataInputStream file, 75 ConstantPool constant_pool) throws IOException 76 { 77 this(name_index, length, (byte [])null, constant_pool); 78 79 if(length > 0) { 80 bytes = new byte[length]; 81 file.readFully(bytes); 82 System.err.println("Synthetic attribute with length > 0"); 83 } 84 } 85 /** 86 * Called by objects that are traversing the nodes of the tree implicitely 87 * defined by the contents of a Java class. I.e., the hierarchy of methods, 88 * fields, attributes, etc. spawns a tree of objects. 89 * 90 * @param v Visitor object 91 */ 92 public void accept(Visitor v) { 93 v.visitSynthetic(this); 94 } 95 /** 96 * Dump source file attribute to file stream in binary format. 97 * 98 * @param file Output file stream 99 * @throws IOException 100 */ 101 public final void dump(DataOutputStream file) throws IOException 102 { 103 super.dump(file); 104 if(length > 0) 105 file.write(bytes, 0, length); 106 } 107 /** 108 * @return data bytes. 109 */ 110 public final byte[] getBytes() { return bytes; } 111 112 /** 113 * @param bytes. 114 */ 115 public final void setBytes(byte[] bytes) { 116 this.bytes = bytes; 117 } 118 119 /** 120 * @return String representation. 121 */ 122 public final String toString() { 123 StringBuffer buf = new StringBuffer("Synthetic"); 124 125 if(length > 0) 126 buf.append(" " + Utility.toHexString(bytes)); 127 128 return buf.toString(); 129 } 130 131 /** 132 * @return deep copy of this attribute 133 */ 134 public Attribute copy(ConstantPool constant_pool) { 135 Synthetic c = (Synthetic)clone(); 136 137 if(bytes != null) 138 c.bytes = (byte[])bytes.clone(); 139 140 c.constant_pool = constant_pool; 141 return c; 142 } 143 }