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 represents an entry in the requires table of the Module attribute. Each entry describes a module on which
28   * the parent module depends.
29   *
30   * @see Module
31   * @since 6.4.0
32   */
33  public final class ModuleRequires implements Cloneable, Node {
34  
35      private final int requiresIndex; // points to CONSTANT_Module_info
36      private final int requiresFlags;
37      private final int requiresVersionIndex; // either 0 or points to CONSTANT_Utf8_info
38  
39      /**
40       * Constructs object from file stream.
41       *
42       * @param file Input stream
43       * @throws IOException if an I/O Exception occurs in readUnsignedShort
44       */
45      ModuleRequires(final DataInput file) throws IOException {
46          requiresIndex = file.readUnsignedShort();
47          requiresFlags = file.readUnsignedShort();
48          requiresVersionIndex = file.readUnsignedShort();
49      }
50  
51      /**
52       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
53       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
54       *
55       * @param v Visitor object
56       */
57      @Override
58      public void accept(final Visitor v) {
59          v.visitModuleRequires(this);
60      }
61  
62      // TODO add more getters and setters?
63  
64      /**
65       * @return deep copy of this object
66       */
67      public ModuleRequires copy() {
68          try {
69              return (ModuleRequires) clone();
70          } catch (final CloneNotSupportedException e) {
71              // TODO should this throw?
72          }
73          return null;
74      }
75  
76      /**
77       * Dump table entry to file stream in binary format.
78       *
79       * @param file Output file stream
80       * @throws IOException if an I/O Exception occurs in writeShort
81       */
82      public void dump(final DataOutputStream file) throws IOException {
83          file.writeShort(requiresIndex);
84          file.writeShort(requiresFlags);
85          file.writeShort(requiresVersionIndex);
86      }
87  
88      /**
89       * @return String representation
90       */
91      @Override
92      public String toString() {
93          return "requires(" + requiresIndex + ", " + String.format("%04x", requiresFlags) + ", " + requiresVersionIndex + ")";
94      }
95  
96      /**
97       * @return Resolved string representation
98       */
99      public String toString(final ConstantPool constantPool) {
100         final StringBuilder buf = new StringBuilder();
101         final String moduleName = constantPool.constantToString(requiresIndex, Const.CONSTANT_Module);
102         buf.append(Utility.compactClassName(moduleName, false));
103         buf.append(", ").append(String.format("%04x", requiresFlags));
104         final String version = requiresVersionIndex == 0 ? "0" : constantPool.getConstantString(requiresVersionIndex, Const.CONSTANT_Utf8);
105         buf.append(", ").append(version);
106         return buf.toString();
107     }
108 }