    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;
    024
    025/**
    026 * This class is derived from <em>Attribute</em> and represents a reference to a PMG attribute.
    027 *
    028 * @see Attribute
    029 */
    030public final class PMGClass extends Attribute {
    031
    032    private int pmgClassIndex;
    033    private int pmgIndex;
    034
    035    /**
    036     * Constructs object from input stream.
    037     *
    038     * @param nameIndex Index in constant pool to CONSTANT_Utf8
    039     * @param length Content length in bytes
    040     * @param input Input stream
    041     * @param constantPool Array of constants
    042     * @throws IOException if an I/O error occurs.
    043     */
    044    PMGClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    045        this(nameIndex, length, input.readUnsignedShort(), input.readUnsignedShort(), constantPool);
    046    }
    047
    048    /**
    049     * @param nameIndex Index in constant pool to CONSTANT_Utf8
    050     * @param length Content length in bytes
    051     * @param pmgIndex index in constant pool for source file name
    052     * @param pmgClassIndex Index in constant pool to CONSTANT_Utf8
    053     * @param constantPool Array of constants
    054     */
    055    public PMGClass(final int nameIndex, final int length, final int pmgIndex, final int pmgClassIndex, final ConstantPool constantPool) {
    056        super(Const.ATTR_PMG, nameIndex, length, constantPool);
    057        this.pmgIndex = pmgIndex;
    058        this.pmgClassIndex = pmgClassIndex;
    059    }
    060
    061    /**
    062     * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
    063     * physical copy.
    064     *
    065     * @param pgmClass Source to copy.
    066     */
    067    public PMGClass(final PMGClass pgmClass) {
    068        this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(), pgmClass.getConstantPool());
    069    }
    070
    071    /**
    072     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    073     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    074     *
    075     * @param v Visitor object
    076     */
    077    @Override
    078    public void accept(final Visitor v) {
    079        println("Visiting non-standard PMGClass object");
    080    }
    081
    082    /**
    083     * @return deep copy of this attribute
    084     */
    085    @Override
    086    public Attribute copy(final ConstantPool constantPool) {
    087        return (Attribute) clone();
    088    }
    089
    090    /**
    091     * Dump source file attribute to file stream in binary format.
    092     *
    093     * @param file Output file stream
    094     * @throws IOException if an I/O error occurs.
    095     */
    096    @Override
    097    public void dump(final DataOutputStream file) throws IOException {
    098        super.dump(file);
    099        file.writeShort(pmgIndex);
    100        file.writeShort(pmgClassIndex);
    101    }
    102
    103    /**
    104     * @return Index in constant pool of source file name.
    105     */
    106    public int getPMGClassIndex() {
    107        return pmgClassIndex;
    108    }
    109
    110    /**
    111     * @return PMG class name.
    112     */
    113    public String getPMGClassName() {
    114        return super.getConstantPool().getConstantUtf8(pmgClassIndex).getBytes();
    115    }
    116
    117    /**
    118     * @return Index in constant pool of source file name.
    119     */
    120    public int getPMGIndex() {
    121        return pmgIndex;
    122    }
    123
    124    /**
    125     * @return PMG name.
    126     */
    127    public String getPMGName() {
    128        return super.getConstantPool().getConstantUtf8(pmgIndex).getBytes();
    129    }
    130
    131    /**
    132     * @param pmgClassIndex
    133     */
    134    public void setPMGClassIndex(final int pmgClassIndex) {
    135        this.pmgClassIndex = pmgClassIndex;
    136    }
    137
    138    /**
    139     * @param pmgIndex
    140     */
    141    public void setPMGIndex(final int pmgIndex) {
    142        this.pmgIndex = pmgIndex;
    143    }
    144
    145    /**
    146     * @return String representation
    147     */
    148    @Override
    149    public String toString() {
    150        return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
    151    }
    152}