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