View Javadoc
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   * @see     Attribute
31   */
32  public final class PMGClass extends Attribute {
33  
34      private int pmgClassIndex;
35      private int pmgIndex;
36  
37  
38      /**
39       * Initialize from another object. Note that both objects use the same
40       * references (shallow copy). Use copy() for a physical copy.
41       */
42      public PMGClassle/PMGClass.html#PMGClass">PMGClass(final PMGClass pgmClass) {
43          this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(),
44              pgmClass.getConstantPool());
45      }
46  
47  
48      /**
49       * Construct object from input stream.
50       * @param name_index Index in constant pool to CONSTANT_Utf8
51       * @param length Content length in bytes
52       * @param input Input stream
53       * @param constant_pool Array of constants
54       * @throws IOException
55       */
56      PMGClass(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
57              throws IOException {
58          this(name_index, length, input.readUnsignedShort(), input.readUnsignedShort(), constant_pool);
59      }
60  
61  
62      /**
63       * @param name_index Index in constant pool to CONSTANT_Utf8
64       * @param length Content length in bytes
65       * @param pmgIndex index in constant pool for source file name
66       * @param pmgClassIndex Index in constant pool to CONSTANT_Utf8
67       * @param constantPool Array of constants
68       */
69      public PMGClass(final int name_index, final int length, final int pmgIndex, final int pmgClassIndex,
70              final ConstantPool constantPool) {
71          super(Const.ATTR_PMG, name_index, length, constantPool);
72          this.pmgIndex = pmgIndex;
73          this.pmgClassIndex = pmgClassIndex;
74      }
75  
76  
77      /**
78       * Called by objects that are traversing the nodes of the tree implicitely
79       * defined by the contents of a Java class. I.e., the hierarchy of methods,
80       * fields, attributes, etc. spawns a tree of objects.
81       *
82       * @param v Visitor object
83       */
84      @Override
85      public void accept( final Visitor v ) {
86          println("Visiting non-standard PMGClass object");
87      }
88  
89  
90      /**
91       * Dump source file attribute to file stream in binary format.
92       *
93       * @param file Output file stream
94       * @throws IOException
95       */
96      @Override
97      public void dump( final DataOutputStream file ) throws IOException {
98          super.dump(file);
99          file.writeShort(pmgIndex);
100         file.writeShort(pmgClassIndex);
101     }
102 
103 
104     /**
105      * @return Index in constant pool of source file name.
106      */
107     public int getPMGClassIndex() {
108         return pmgClassIndex;
109     }
110 
111 
112     /**
113      * @param pmgClassIndex
114      */
115     public void setPMGClassIndex( final int pmgClassIndex ) {
116         this.pmgClassIndex = pmgClassIndex;
117     }
118 
119 
120     /**
121      * @return Index in constant pool of source file name.
122      */
123     public int getPMGIndex() {
124         return pmgIndex;
125     }
126 
127 
128     /**
129      * @param pmgIndex
130      */
131     public void setPMGIndex( final int pmgIndex ) {
132         this.pmgIndex = pmgIndex;
133     }
134 
135 
136     /**
137      * @return PMG name.
138      */
139     public String getPMGName() {
140         final ConstantUtf8./../../../org/apache/bcel/classfile/ConstantUtf8.html#ConstantUtf8">ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(pmgIndex,
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./../../../org/apache/bcel/classfile/ConstantUtf8.html#ConstantUtf8">ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(pmgClassIndex,
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 }