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 provides table of the Module attribute. Each entry describes a service
30   * implementation that the parent module provides.
31   *
32   * @see Module
33   * @since 6.4.0
34   */
35  public final class ModuleProvides implements Cloneable, Node {
36  
37      private static String getImplementationClassNameAtIndex(final ConstantPool constantPool, final int index, final boolean compactClassName) {
38          final String className = constantPool.getConstantString(index, Const.CONSTANT_Class);
39          if (compactClassName) {
40              return Utility.compactClassName(className, false);
41          }
42          return className;
43      }
44      private final int providesIndex; // points to CONSTANT_Class_info
45      private final int providesWithCount;
46  
47      private final int[] providesWithIndex; // points to CONSTANT_Class_info
48  
49      /**
50       * Constructs object from file stream.
51       *
52       * @param dataInput Input stream.
53       * @throws IOException Thrown if an I/O Exception occurs in readUnsignedShort.
54       */
55      ModuleProvides(final DataInput dataInput) throws IOException {
56          providesIndex = dataInput.readUnsignedShort();
57          providesWithIndex = ClassParser.readU2U2Table(dataInput);
58          providesWithCount = providesWithIndex.length;
59      }
60  
61      /**
62       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
63       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
64       *
65       * @param v Visitor object.
66       */
67      @Override
68      public void accept(final Visitor v) {
69          v.visitModuleProvides(this);
70      }
71  
72      /**
73       * Creates a deep copy of this object.
74       *
75       * @return deep copy of this object.
76       */
77      public ModuleProvides copy() {
78          try {
79              return (ModuleProvides) clone();
80          } catch (final CloneNotSupportedException e) {
81              // TODO should this throw?
82          }
83          return null;
84      }
85  
86      /**
87       * Dumps table entry to file stream in binary format.
88       *
89       * @param file Output file stream.
90       * @throws IOException Thrown if an I/O Exception occurs in writeShort.
91       */
92      public void dump(final DataOutputStream file) throws IOException {
93          file.writeShort(providesIndex);
94          file.writeShort(providesWithCount);
95          for (final int entry : providesWithIndex) {
96              file.writeShort(entry);
97          }
98      }
99  
100     /**
101      * Gets the array of implementation class names for this ModuleProvides.
102      *
103      * @param constantPool Array of constants usually obtained from the ClassFile object.
104      * @param compactClassName false for original constant pool value, true to replace '/' with '.'.
105      * @return array of implementation class names.
106      * @since 6.10.0
107      */
108     public String[] getImplementationClassNames(final ConstantPool constantPool, final boolean compactClassName) {
109         final String[] implementationClassNames = new String[providesWithCount];
110         for (int i = 0; i < providesWithCount; i++) {
111             implementationClassNames[i] = getImplementationClassNameAtIndex(constantPool, providesWithIndex[i], compactClassName);
112         }
113         return implementationClassNames;
114     }
115 
116     /**
117      * Gets the interface name for this ModuleProvides.
118      *
119      * @param constantPool Array of constants usually obtained from the ClassFile object.
120      * @return interface name.
121      * @since 6.10.0
122      */
123     public String getInterfaceName(final ConstantPool constantPool) {
124         return constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
125     }
126 
127     /**
128      * @return String representation.
129      */
130     @Override
131     public String toString() {
132         return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
133     }
134 
135     /**
136      * Gets the resolved string representation.
137      *
138      * @param constantPool The constant pool.
139      * @return Resolved string representation.
140      */
141     public String toString(final ConstantPool constantPool) {
142         final StringBuilder buf = new StringBuilder();
143         final String interfaceName = getInterfaceName(constantPool);
144         buf.append(interfaceName);
145         buf.append(", with(").append(providesWithCount).append("):\n");
146         for (final int index : providesWithIndex) {
147             final String className = getImplementationClassNameAtIndex(constantPool, index, true);
148             buf.append("      ").append(className).append("\n");
149         }
150         return buf.substring(0, buf.length() - 1); // remove the last newline
151     }
152 }