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  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  
26  import org.apache.bcel.Const;
27  
28  /**
29   * This class represents an entry in the requires table of the Module attribute. Each entry describes a module on which
30   * the parent module depends.
31   *
32   * @see Module
33   * @since 6.4.0
34   */
35  public final class ModuleRequires implements Cloneable, Node {
36  
37      private final int requiresIndex; // points to CONSTANT_Module_info
38      private final int requiresFlags;
39      private final int requiresVersionIndex; // either 0 or points to CONSTANT_Utf8_info
40  
41      /**
42       * Constructs object from file stream.
43       *
44       * @param file Input stream.
45       * @throws IOException Thrown if an I/O Exception occurs in readUnsignedShort.
46       */
47      ModuleRequires(final DataInput file) throws IOException {
48          requiresIndex = file.readUnsignedShort();
49          requiresFlags = file.readUnsignedShort();
50          requiresVersionIndex = file.readUnsignedShort();
51      }
52  
53      /**
54       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
55       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
56       *
57       * @param v Visitor object.
58       */
59      @Override
60      public void accept(final Visitor v) {
61          v.visitModuleRequires(this);
62      }
63  
64      /**
65       * Creates a deep copy of this object.
66       *
67       * @return deep copy of this object.
68       */
69      public ModuleRequires copy() {
70          try {
71              return (ModuleRequires) clone();
72          } catch (final CloneNotSupportedException e) {
73              // TODO should this throw?
74          }
75          return null;
76      }
77  
78      /**
79       * Dumps table entry to file stream in binary format.
80       *
81       * @param file Output file stream.
82       * @throws IOException Thrown if an I/O Exception occurs in writeShort.
83       */
84      public void dump(final DataOutputStream file) throws IOException {
85          file.writeShort(requiresIndex);
86          file.writeShort(requiresFlags);
87          file.writeShort(requiresVersionIndex);
88      }
89  
90      /**
91       * Gets the module name from the constant pool.
92       *
93       * @param constantPool Array of constants usually obtained from the ClassFile object.
94       * @return module name.
95       * @since 6.10.0
96       */
97      public String getModuleName(final ConstantPool constantPool) {
98          return constantPool.constantToString(requiresIndex, Const.CONSTANT_Module);
99      }
100 
101     /**
102      * Gets the flags for this ModuleRequires.
103      *
104      * @return The requiresFlags.
105      * @since 6.10.0
106      */
107     public int getRequiresFlags() {
108         return requiresFlags;
109     }
110 
111     /**
112      * Gets the required version from the constant pool.
113      *
114      * @param constantPool Array of constants usually obtained from the ClassFile object.
115      * @return required version, "0" if version index is 0.
116      * @since 6.10.0
117      */
118     public String getVersion(final ConstantPool constantPool) {
119         return requiresVersionIndex == 0 ? "0" : constantPool.getConstantString(requiresVersionIndex, Const.CONSTANT_Utf8);
120     }
121 
122     /**
123      * @return String representation.
124      */
125     @Override
126     public String toString() {
127         return "requires(" + requiresIndex + ", " + String.format("%04x", requiresFlags) + ", " + requiresVersionIndex + ")";
128     }
129 
130     /**
131      * Gets the resolved string representation.
132      *
133      * @param constantPool The constant pool.
134      * @return Resolved string representation.
135      */
136     public String toString(final ConstantPool constantPool) {
137         final StringBuilder buf = new StringBuilder();
138         final String moduleName = getModuleName(constantPool);
139         buf.append(moduleName);
140         buf.append(", ").append(String.format("%04x", requiresFlags));
141         final String version = getVersion(constantPool);
142         buf.append(", ").append(version);
143         return buf.toString();
144     }
145 }