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 */
    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     * Construct 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    public PMGClass(final PMGClass pgmClass) {
    066        this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(), pgmClass.getConstantPool());
    067    }
    068
    069    /**
    070     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    071     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    072     *
    073     * @param v Visitor object
    074     */
    075    @Override
    076    public void accept(final Visitor v) {
    077        println("Visiting non-standard PMGClass object");
    078    }
    079
    080    /**
    081     * @return deep copy of this attribute
    082     */
    083    @Override
    084    public Attribute copy(final ConstantPool constantPool) {
    085        return (Attribute) clone();
    086    }
    087
    088    /**
    089     * Dump source file attribute to file stream in binary format.
    090     *
    091     * @param file Output file stream
    092     * @throws IOException if an I/O error occurs.
    093     */
    094    @Override
    095    public void dump(final DataOutputStream file) throws IOException {
    096        super.dump(file);
    097        file.writeShort(pmgIndex);
    098        file.writeShort(pmgClassIndex);
    099    }
    100
    101    /**
    102     * @return Index in constant pool of source file name.
    103     */
    104    public int getPMGClassIndex() {
    105        return pmgClassIndex;
    106    }
    107
    108    /**
    109     * @return PMG class name.
    110     */
    111    public String getPMGClassName() {
    112        return super.getConstantPool().getConstantUtf8(pmgClassIndex).getBytes();
    113    }
    114
    115    /**
    116     * @return Index in constant pool of source file name.
    117     */
    118    public int getPMGIndex() {
    119        return pmgIndex;
    120    }
    121
    122    /**
    123     * @return PMG name.
    124     */
    125    public String getPMGName() {
    126        return super.getConstantPool().getConstantUtf8(pmgIndex).getBytes();
    127    }
    128
    129    /**
    130     * @param pmgClassIndex
    131     */
    132    public void setPMGClassIndex(final int pmgClassIndex) {
    133        this.pmgClassIndex = pmgClassIndex;
    134    }
    135
    136    /**
    137     * @param pmgIndex
    138     */
    139    public void setPMGIndex(final int pmgIndex) {
    140        this.pmgIndex = pmgIndex;
    141    }
    142
    143    /**
    144     * @return String representation
    145     */
    146    @Override
    147    public String toString() {
    148        return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
    149    }
    150}