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