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