• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.bcel.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from <em>Attribute</em> and represents a reference
28  * to a PMG attribute.
29  *
30  * @version $Id$
31  * @see     Attribute
32  */
33 public final class PMGClass extends Attribute {
34 
35     private int pmg_class_index;
36     private int pmg_index;
37 
38 
39     /**
40      * Initialize from another object. Note that both objects use the same
41      * references (shallow copy). Use copy() for a physical copy.
42      */
PMGClass(final PMGClass c)43     public PMGClass(final PMGClass c) {
44         this(c.getNameIndex(), c.getLength(), c.getPMGIndex(), c.getPMGClassIndex(), c
45                 .getConstantPool());
46     }
47 
48 
49     /**
50      * Construct object from input stream.
51      * @param name_index Index in constant pool to CONSTANT_Utf8
52      * @param length Content length in bytes
53      * @param input Input stream
54      * @param constant_pool Array of constants
55      * @throws IOException
56      */
PMGClass(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)57     PMGClass(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
58             throws IOException {
59         this(name_index, length, input.readUnsignedShort(), input.readUnsignedShort(), constant_pool);
60     }
61 
62 
63     /**
64      * @param name_index Index in constant pool to CONSTANT_Utf8
65      * @param length Content length in bytes
66      * @param pmg_index index in constant pool for source file name
67      * @param pmg_class_index Index in constant pool to CONSTANT_Utf8
68      * @param constant_pool Array of constants
69      */
PMGClass(final int name_index, final int length, final int pmg_index, final int pmg_class_index, final ConstantPool constant_pool)70     public PMGClass(final int name_index, final int length, final int pmg_index, final int pmg_class_index,
71             final ConstantPool constant_pool) {
72         super(Const.ATTR_PMG, name_index, length, constant_pool);
73         this.pmg_index = pmg_index;
74         this.pmg_class_index = pmg_class_index;
75     }
76 
77 
78     /**
79      * Called by objects that are traversing the nodes of the tree implicitely
80      * defined by the contents of a Java class. I.e., the hierarchy of methods,
81      * fields, attributes, etc. spawns a tree of objects.
82      *
83      * @param v Visitor object
84      */
85     @Override
accept( final Visitor v )86     public void accept( final Visitor v ) {
87         System.err.println("Visiting non-standard PMGClass object");
88     }
89 
90 
91     /**
92      * Dump source file attribute to file stream in binary format.
93      *
94      * @param file Output file stream
95      * @throws IOException
96      */
97     @Override
dump( final DataOutputStream file )98     public final void dump( final DataOutputStream file ) throws IOException {
99         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      */
getPMGClassIndex()108     public final int getPMGClassIndex() {
109         return pmg_class_index;
110     }
111 
112 
113     /**
114      * @param pmg_class_index
115      */
setPMGClassIndex( final int pmg_class_index )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      */
getPMGIndex()124     public final int getPMGIndex() {
125         return pmg_index;
126     }
127 
128 
129     /**
130      * @param pmg_index
131      */
setPMGIndex( final int pmg_index )132     public final void setPMGIndex( final int pmg_index ) {
133         this.pmg_index = pmg_index;
134     }
135 
136 
137     /**
138      * @return PMG name.
139      */
getPMGName()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      */
getPMGClassName()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
toString()161     public final String toString() {
162         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
163     }
164 
165 
166     /**
167      * @return deep copy of this attribute
168      */
169     @Override
copy( final ConstantPool _constant_pool )170     public Attribute copy( final ConstantPool _constant_pool ) {
171         return (Attribute) clone();
172     }
173 }
174