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