Source code

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