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  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  
25  /**
26   * This class is derived from <em>Attribute</em> and represents a reference to a PMG attribute.
27   *
28   * @see Attribute
29   */
30  public final class PMGClass extends Attribute {
31  
32      private int pmgClassIndex;
33      private int pmgIndex;
34  
35      /**
36       * Constructs object from input stream.
37       *
38       * @param nameIndex Index in constant pool to CONSTANT_Utf8
39       * @param length Content length in bytes
40       * @param input Input stream
41       * @param constantPool Array of constants
42       * @throws IOException if an I/O error occurs.
43       */
44      PMGClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
45          this(nameIndex, length, input.readUnsignedShort(), input.readUnsignedShort(), constantPool);
46      }
47  
48      /**
49       * @param nameIndex Index in constant pool to CONSTANT_Utf8
50       * @param length Content length in bytes
51       * @param pmgIndex index in constant pool for source file name
52       * @param pmgClassIndex Index in constant pool to CONSTANT_Utf8
53       * @param constantPool Array of constants
54       */
55      public PMGClass(final int nameIndex, final int length, final int pmgIndex, final int pmgClassIndex, final ConstantPool constantPool) {
56          super(Const.ATTR_PMG, nameIndex, length, constantPool);
57          this.pmgIndex = pmgIndex;
58          this.pmgClassIndex = pmgClassIndex;
59      }
60  
61      /**
62       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
63       * physical copy.
64       *
65       * @param pgmClass Source to copy.
66       */
67      public PMGClass(final PMGClass pgmClass) {
68          this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(), pgmClass.getConstantPool());
69      }
70  
71      /**
72       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
73       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
74       *
75       * @param v Visitor object
76       */
77      @Override
78      public void accept(final Visitor v) {
79          println("Visiting non-standard PMGClass object");
80      }
81  
82      /**
83       * @return deep copy of this attribute
84       */
85      @Override
86      public Attribute copy(final ConstantPool constantPool) {
87          return (Attribute) clone();
88      }
89  
90      /**
91       * Dump source file attribute to file stream in binary format.
92       *
93       * @param file Output file stream
94       * @throws IOException if an I/O error occurs.
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      * @return Index in constant pool of source file name.
105      */
106     public int getPMGClassIndex() {
107         return pmgClassIndex;
108     }
109 
110     /**
111      * @return PMG class name.
112      */
113     public String getPMGClassName() {
114         return super.getConstantPool().getConstantUtf8(pmgClassIndex).getBytes();
115     }
116 
117     /**
118      * @return Index in constant pool of source file name.
119      */
120     public int getPMGIndex() {
121         return pmgIndex;
122     }
123 
124     /**
125      * @return PMG name.
126      */
127     public String getPMGName() {
128         return super.getConstantPool().getConstantUtf8(pmgIndex).getBytes();
129     }
130 
131     /**
132      * @param pmgClassIndex
133      */
134     public void setPMGClassIndex(final int pmgClassIndex) {
135         this.pmgClassIndex = pmgClassIndex;
136     }
137 
138     /**
139      * @param pmgIndex
140      */
141     public void setPMGIndex(final int pmgIndex) {
142         this.pmgIndex = pmgIndex;
143     }
144 
145     /**
146      * @return String representation
147      */
148     @Override
149     public String toString() {
150         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
151     }
152 }