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