ModuleProvides.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 provides table of the Module attribute. Each entry describes a service
  24.  * implementation that the parent module provides.
  25.  *
  26.  * @see Module
  27.  * @since 6.4.0
  28.  */
  29. public final class ModuleProvides implements Cloneable, Node {

  30.     private static String getImplementationClassNameAtIndex(final ConstantPool constantPool, final int index, final boolean compactClassName) {
  31.         final String className = constantPool.getConstantString(index, Const.CONSTANT_Class);
  32.         if (compactClassName) {
  33.             return Utility.compactClassName(className, false);
  34.         }
  35.         return className;
  36.     }
  37.     private final int providesIndex; // points to CONSTANT_Class_info
  38.     private final int providesWithCount;

  39.     private final int[] providesWithIndex; // points to CONSTANT_Class_info

  40.     /**
  41.      * Constructs object from file stream.
  42.      *
  43.      * @param file Input stream
  44.      * @throws IOException if an I/O Exception occurs in readUnsignedShort
  45.      */
  46.     ModuleProvides(final DataInput file) throws IOException {
  47.         providesIndex = file.readUnsignedShort();
  48.         providesWithCount = file.readUnsignedShort();
  49.         providesWithIndex = new int[providesWithCount];
  50.         for (int i = 0; i < providesWithCount; i++) {
  51.             providesWithIndex[i] = file.readUnsignedShort();
  52.         }
  53.     }

  54.     /**
  55.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  56.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  57.      *
  58.      * @param v Visitor object
  59.      */
  60.     @Override
  61.     public void accept(final Visitor v) {
  62.         v.visitModuleProvides(this);
  63.     }

  64.     /**
  65.      * @return deep copy of this object
  66.      */
  67.     public ModuleProvides copy() {
  68.         try {
  69.             return (ModuleProvides) clone();
  70.         } catch (final CloneNotSupportedException e) {
  71.             // TODO should this throw?
  72.         }
  73.         return null;
  74.     }

  75.     /**
  76.      * Dump table entry to file stream in binary format.
  77.      *
  78.      * @param file Output file stream
  79.      * @throws IOException if an I/O Exception occurs in writeShort
  80.      */
  81.     public void dump(final DataOutputStream file) throws IOException {
  82.         file.writeShort(providesIndex);
  83.         file.writeShort(providesWithCount);
  84.         for (final int entry : providesWithIndex) {
  85.             file.writeShort(entry);
  86.         }
  87.     }

  88.     /**
  89.      * Gets the array of implementation class names for this ModuleProvides.
  90.      * @param constantPool Array of constants usually obtained from the ClassFile object
  91.      * @param compactClassName false for original constant pool value, true to replace '/' with '.'
  92.      * @return array of implementation class names
  93.      * @since 6.10.0
  94.      */
  95.     public String[] getImplementationClassNames(final ConstantPool constantPool, final boolean compactClassName) {
  96.         final String[] implementationClassNames = new String[providesWithCount];
  97.         for (int i = 0; i < providesWithCount; i++) {
  98.             implementationClassNames[i] = getImplementationClassNameAtIndex(constantPool, providesWithIndex[i], compactClassName);
  99.         }
  100.         return implementationClassNames;
  101.     }

  102.     /**
  103.      * Gets the interface name for this ModuleProvides.
  104.      * @param constantPool Array of constants usually obtained from the ClassFile object
  105.      * @return interface name
  106.      * @since 6.10.0
  107.      */
  108.     public String getInterfaceName(final ConstantPool constantPool) {
  109.         return constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
  110.     }

  111.     /**
  112.      * @return String representation
  113.      */
  114.     @Override
  115.     public String toString() {
  116.         return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
  117.     }

  118.     /**
  119.      * @return Resolved string representation
  120.      */
  121.     public String toString(final ConstantPool constantPool) {
  122.         final StringBuilder buf = new StringBuilder();
  123.         final String interfaceName = getInterfaceName(constantPool);
  124.         buf.append(interfaceName);
  125.         buf.append(", with(").append(providesWithCount).append("):\n");
  126.         for (final int index : providesWithIndex) {
  127.             final String className = getImplementationClassNameAtIndex(constantPool, index, true);
  128.             buf.append("      ").append(className).append("\n");
  129.         }
  130.         return buf.substring(0, buf.length() - 1); // remove the last newline
  131.     }
  132. }