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