ModuleRequires.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * This class represents an entry in the requires table of the Module attribute. Each entry describes a module on which
  24.  * the parent module depends.
  25.  *
  26.  * @see Module
  27.  * @since 6.4.0
  28.  */
  29. public final class ModuleRequires implements Cloneable, Node {

  30.     private final int requiresIndex; // points to CONSTANT_Module_info
  31.     private final int requiresFlags;
  32.     private final int requiresVersionIndex; // either 0 or points to CONSTANT_Utf8_info

  33.     /**
  34.      * Constructs object from file stream.
  35.      *
  36.      * @param file Input stream
  37.      * @throws IOException if an I/O Exception occurs in readUnsignedShort
  38.      */
  39.     ModuleRequires(final DataInput file) throws IOException {
  40.         requiresIndex = file.readUnsignedShort();
  41.         requiresFlags = file.readUnsignedShort();
  42.         requiresVersionIndex = file.readUnsignedShort();
  43.     }

  44.     /**
  45.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  46.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  47.      *
  48.      * @param v Visitor object
  49.      */
  50.     @Override
  51.     public void accept(final Visitor v) {
  52.         v.visitModuleRequires(this);
  53.     }

  54.     /**
  55.      * @return deep copy of this object
  56.      */
  57.     public ModuleRequires copy() {
  58.         try {
  59.             return (ModuleRequires) clone();
  60.         } catch (final CloneNotSupportedException e) {
  61.             // TODO should this throw?
  62.         }
  63.         return null;
  64.     }

  65.     /**
  66.      * Dump table entry to file stream in binary format.
  67.      *
  68.      * @param file Output file stream
  69.      * @throws IOException if an I/O Exception occurs in writeShort
  70.      */
  71.     public void dump(final DataOutputStream file) throws IOException {
  72.         file.writeShort(requiresIndex);
  73.         file.writeShort(requiresFlags);
  74.         file.writeShort(requiresVersionIndex);
  75.     }

  76.     /**
  77.      * Gets the module name from the constant pool.
  78.      * @param constantPool Array of constants usually obtained from the ClassFile object
  79.      * @return module name
  80.      * @since 6.10.0
  81.      */
  82.     public String getModuleName(final ConstantPool constantPool) {
  83.         return constantPool.constantToString(requiresIndex, Const.CONSTANT_Module);
  84.     }

  85.     /**
  86.      * Gets the flags for this ModuleRequires.
  87.      * @return the requiresFlags
  88.      * @since 6.10.0
  89.      */
  90.     public int getRequiresFlags() {
  91.         return requiresFlags;
  92.     }

  93.     /**
  94.      * Gets the required version from the constant pool.
  95.      * @param constantPool Array of constants usually obtained from the ClassFile object
  96.      * @return required version, "0" if version index is 0.
  97.      * @since 6.10.0
  98.      */
  99.     public String getVersion(final ConstantPool constantPool) {
  100.         return requiresVersionIndex == 0 ? "0" : constantPool.getConstantString(requiresVersionIndex, Const.CONSTANT_Utf8);
  101.     }

  102.     /**
  103.      * @return String representation
  104.      */
  105.     @Override
  106.     public String toString() {
  107.         return "requires(" + requiresIndex + ", " + String.format("%04x", requiresFlags) + ", " + requiresVersionIndex + ")";
  108.     }

  109.     /**
  110.      * @return Resolved string representation
  111.      */
  112.     public String toString(final ConstantPool constantPool) {
  113.         final StringBuilder buf = new StringBuilder();
  114.         final String moduleName = getModuleName(constantPool);
  115.         buf.append(moduleName);
  116.         buf.append(", ").append(String.format("%04x", requiresFlags));
  117.         final String version = getVersion(constantPool);
  118.         buf.append(", ").append(version);
  119.         return buf.toString();
  120.     }
  121. }