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 the abstract {@link Constant} and represents a reference to a module.
29   *
30   * <p>
31   * Note: Early access Java 9 support- currently subject to change
32   * </p>
33   *
34   * @see Constant
35   * @since 6.1
36   */
37  public final class ConstantModule extends Constant implements ConstantObject {
38  
39      private int nameIndex;
40  
41      /**
42       * Initialize from another object.
43       *
44       * @param c Source to copy.
45       */
46      public ConstantModule(final ConstantModule c) {
47          this(c.getNameIndex());
48      }
49  
50      /**
51       * Initialize instance from file data.
52       *
53       * @param file Input stream.
54       * @throws IOException if an I/O error occurs.
55       */
56      ConstantModule(final DataInput file) throws IOException {
57          this(file.readUnsignedShort());
58      }
59  
60      /**
61       * Constructs a ConstantModule.
62       *
63       * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8.
64       */
65      public ConstantModule(final int nameIndex) {
66          super(Const.CONSTANT_Module);
67          this.nameIndex = nameIndex;
68      }
69  
70      /**
71       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
72       * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
73       *
74       * @param v Visitor object.
75       */
76      @Override
77      public void accept(final Visitor v) {
78          v.visitConstantModule(this);
79      }
80  
81      /**
82       * Dumps constant module to file stream in binary format.
83       *
84       * @param file Output file stream.
85       * @throws IOException if an I/O error occurs.
86       */
87      @Override
88      public void dump(final DataOutputStream file) throws IOException {
89          file.writeByte(super.getTag());
90          file.writeShort(nameIndex);
91      }
92  
93      /**
94       * Gets the dereferenced string.
95       *
96       * @param cp the constant pool.
97       * @return dereferenced string.
98       */
99      public String getBytes(final ConstantPool cp) {
100         return (String) getConstantValue(cp);
101     }
102 
103     /**
104      * Gets the String object.
105      *
106      * @param cp the constant pool.
107      * @return String object.
108      */
109     @Override
110     public Object getConstantValue(final ConstantPool cp) {
111         return cp.getConstantUtf8(nameIndex).getBytes();
112     }
113 
114     /**
115      * Gets the name index in constant pool of module name.
116      *
117      * @return Name index in constant pool of module name.
118      */
119     public int getNameIndex() {
120         return nameIndex;
121     }
122 
123     /**
124      * Sets the name index in the constant pool.
125      *
126      * @param nameIndex the name index in the constant pool of this Constant Module.
127      */
128     public void setNameIndex(final int nameIndex) {
129         this.nameIndex = nameIndex;
130     }
131 
132     /**
133      * @return String representation.
134      */
135     @Override
136     public String toString() {
137         return super.toString() + "(nameIndex = " + nameIndex + ")";
138     }
139 }