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 * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic". 049 * @param length JVM Specification: "The value of the attribute_length item must be zero.". 050 * @param bytes Attribute contents. 051 * @param constantPool The constant pool this attribute is associated with. 052 * @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> 053 */ 054 public Synthetic(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) { 055 super(Const.ATTR_SYNTHETIC, nameIndex, Args.require0(length, "Synthetic attribute length"), constantPool); 056 } 057 058 /** 059 * Constructs object from input stream. 060 * 061 * @param nameIndex Index in constant pool to CONSTANT_Utf8. 062 * @param length JVM Specification: "The value of the attribute_length item must be zero.". 063 * @param input Input stream. 064 * @param constantPool Array of constants. 065 * @throws IOException if an I/O error occurs. 066 * @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> 067 */ 068 Synthetic(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 069 this(nameIndex, length, (byte[]) null, constantPool); 070 } 071 072 /** 073 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy. 074 * 075 * @param c Source to copy. 076 */ 077 public Synthetic(final Synthetic c) { 078 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 079 } 080 081 /** 082 * 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, 083 * attributes, etc. spawns a tree of objects. 084 * 085 * @param v Visitor object. 086 */ 087 @Override 088 public void accept(final Visitor v) { 089 v.visitSynthetic(this); 090 } 091 092 /** 093 * @return deep copy of this attribute. 094 */ 095 @Override 096 public Attribute copy(final ConstantPool constantPool) { 097 final Synthetic c = (Synthetic) clone(); 098 c.setConstantPool(constantPool); 099 return c; 100 } 101 102 /** 103 * Gets data bytes. 104 * 105 * @return data bytes. 106 */ 107 public byte[] getBytes() { 108 return ArrayUtils.EMPTY_BYTE_ARRAY; 109 } 110 111 /** 112 * Sets data bytes. 113 * 114 * @param bytes data bytes. 115 */ 116 public void setBytes(final byte[] bytes) { 117 if (bytes != null) { 118 Args.require0(bytes.length, "Deprecated attribute length"); 119 } 120 } 121 122 /** 123 * @return String representation. 124 */ 125 @Override 126 public String toString() { 127 return "Synthetic"; 128 } 129}