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