View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInputStream;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import org.apache.bcel.Constants;
23  
24  /***
25   * This class is derived from <em>Attribute</em> and represents a reference
26   * to a PMG attribute.
27   *
28   * @version $Id: PMGClass.java 386056 2006-03-15 11:31:56Z tcurdt $
29   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
30   * @see     Attribute
31   */
32  public final class PMGClass extends Attribute {
33  
34      private int pmg_class_index, pmg_index;
35  
36  
37      /***
38       * Initialize from another object. Note that both objects use the same
39       * references (shallow copy). Use clone() for a physical copy.
40       */
41      public PMGClass(PMGClass c) {
42          this(c.getNameIndex(), c.getLength(), c.getPMGIndex(), c.getPMGClassIndex(), c
43                  .getConstantPool());
44      }
45  
46  
47      /***
48       * Construct object from file stream.
49       * @param name_index Index in constant pool to CONSTANT_Utf8
50       * @param length Content length in bytes
51       * @param file Input stream
52       * @param constant_pool Array of constants
53       * @throws IOException
54       */
55      PMGClass(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
56              throws IOException {
57          this(name_index, length, file.readUnsignedShort(), file.readUnsignedShort(), constant_pool);
58      }
59  
60  
61      /***
62       * @param name_index Index in constant pool to CONSTANT_Utf8
63       * @param length Content length in bytes
64       * @param pmg_index index in constant pool for source file name
65       * @param pmg_class_index Index in constant pool to CONSTANT_Utf8
66       * @param constant_pool Array of constants
67       */
68      public PMGClass(int name_index, int length, int pmg_index, int pmg_class_index,
69              ConstantPool constant_pool) {
70          super(Constants.ATTR_PMG, name_index, length, constant_pool);
71          this.pmg_index = pmg_index;
72          this.pmg_class_index = pmg_class_index;
73      }
74  
75  
76      /***
77       * Called by objects that are traversing the nodes of the tree implicitely
78       * defined by the contents of a Java class. I.e., the hierarchy of methods,
79       * fields, attributes, etc. spawns a tree of objects.
80       *
81       * @param v Visitor object
82       */
83      public void accept( Visitor v ) {
84          System.err.println("Visiting non-standard PMGClass object");
85      }
86  
87  
88      /***
89       * Dump source file attribute to file stream in binary format.
90       *
91       * @param file Output file stream
92       * @throws IOException
93       */
94      public final void dump( DataOutputStream file ) throws IOException {
95          super.dump(file);
96          file.writeShort(pmg_index);
97          file.writeShort(pmg_class_index);
98      }
99  
100 
101     /***
102      * @return Index in constant pool of source file name.
103      */
104     public final int getPMGClassIndex() {
105         return pmg_class_index;
106     }
107 
108 
109     /***
110      * @param pmg_class_index
111      */
112     public final void setPMGClassIndex( int pmg_class_index ) {
113         this.pmg_class_index = pmg_class_index;
114     }
115 
116 
117     /***
118      * @return Index in constant pool of source file name.
119      */
120     public final int getPMGIndex() {
121         return pmg_index;
122     }
123 
124 
125     /***
126      * @param pmg_index
127      */
128     public final void setPMGIndex( int pmg_index ) {
129         this.pmg_index = pmg_index;
130     }
131 
132 
133     /***
134      * @return PMG name.
135      */
136     public final String getPMGName() {
137         ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(pmg_index,
138                 Constants.CONSTANT_Utf8);
139         return c.getBytes();
140     }
141 
142 
143     /***
144      * @return PMG class name.
145      */
146     public final String getPMGClassName() {
147         ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(pmg_class_index,
148                 Constants.CONSTANT_Utf8);
149         return c.getBytes();
150     }
151 
152 
153     /***
154      * @return String representation
155      */
156     public final String toString() {
157         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
158     }
159 
160 
161     /***
162      * @return deep copy of this attribute
163      */
164     public Attribute copy( ConstantPool _constant_pool ) {
165         return (PMGClass) clone();
166     }
167 }