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