View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  
27  /**
28   * This class is derived from <em>Attribute</em> and represents a reference 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       * Constructs object from input stream.
39       *
40       * @param nameIndex Index in constant pool to CONSTANT_Utf8
41       * @param length Content length in bytes
42       * @param input Input stream
43       * @param constantPool Array of constants
44       * @throws IOException if an I/O error occurs.
45       */
46      PMGClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
47          this(nameIndex, length, input.readUnsignedShort(), input.readUnsignedShort(), constantPool);
48      }
49  
50      /**
51       * @param nameIndex Index in constant pool to CONSTANT_Utf8
52       * @param length Content length in bytes
53       * @param pmgIndex index in constant pool for source file name
54       * @param pmgClassIndex Index in constant pool to CONSTANT_Utf8
55       * @param constantPool Array of constants
56       */
57      public PMGClass(final int nameIndex, final int length, final int pmgIndex, final int pmgClassIndex, final ConstantPool constantPool) {
58          super(Const.ATTR_PMG, nameIndex, length, constantPool);
59          this.pmgIndex = pmgIndex;
60          this.pmgClassIndex = pmgClassIndex;
61      }
62  
63      /**
64       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
65       * physical copy.
66       *
67       * @param pgmClass Source to copy.
68       */
69      public PMGClass(final PMGClass pgmClass) {
70          this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(), pgmClass.getConstantPool());
71      }
72  
73      /**
74       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
75       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
76       *
77       * @param v Visitor object
78       */
79      @Override
80      public void accept(final Visitor v) {
81          println("Visiting non-standard PMGClass object");
82      }
83  
84      /**
85       * @return deep copy of this attribute
86       */
87      @Override
88      public Attribute copy(final ConstantPool constantPool) {
89          return (Attribute) clone();
90      }
91  
92      /**
93       * Dump source file attribute to file stream in binary format.
94       *
95       * @param file Output file stream
96       * @throws IOException if an I/O error occurs.
97       */
98      @Override
99      public void dump(final DataOutputStream file) throws IOException {
100         super.dump(file);
101         file.writeShort(pmgIndex);
102         file.writeShort(pmgClassIndex);
103     }
104 
105     /**
106      * @return Index in constant pool of source file name.
107      */
108     public int getPMGClassIndex() {
109         return pmgClassIndex;
110     }
111 
112     /**
113      * @return PMG class name.
114      */
115     public String getPMGClassName() {
116         return super.getConstantPool().getConstantUtf8(pmgClassIndex).getBytes();
117     }
118 
119     /**
120      * @return Index in constant pool of source file name.
121      */
122     public int getPMGIndex() {
123         return pmgIndex;
124     }
125 
126     /**
127      * @return PMG name.
128      */
129     public String getPMGName() {
130         return super.getConstantPool().getConstantUtf8(pmgIndex).getBytes();
131     }
132 
133     /**
134      * @param pmgClassIndex
135      */
136     public void setPMGClassIndex(final int pmgClassIndex) {
137         this.pmgClassIndex = pmgClassIndex;
138     }
139 
140     /**
141      * @param pmgIndex
142      */
143     public void setPMGIndex(final int pmgIndex) {
144         this.pmgIndex = pmgIndex;
145     }
146 
147     /**
148      * @return String representation
149      */
150     @Override
151     public String toString() {
152         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
153     }
154 }