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