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 provides table of the Module attribute. Each entry describes a service
28   * implementation that the parent module provides.
29   *
30   * @see Module
31   * @since 6.4.0
32   */
33  public final class ModuleProvides implements Cloneable, Node {
34  
35      private final int providesIndex; // points to CONSTANT_Class_info
36      private final int providesWithCount;
37      private final int[] providesWithIndex; // points to CONSTANT_Class_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      ModuleProvides(final DataInput file) throws IOException {
46          providesIndex = file.readUnsignedShort();
47          providesWithCount = file.readUnsignedShort();
48          providesWithIndex = new int[providesWithCount];
49          for (int i = 0; i < providesWithCount; i++) {
50              providesWithIndex[i] = file.readUnsignedShort();
51          }
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      // TODO add more getters and setters?
66  
67      /**
68       * @return deep copy of this object
69       */
70      public ModuleProvides copy() {
71          try {
72              return (ModuleProvides) clone();
73          } catch (final CloneNotSupportedException e) {
74              // TODO should this throw?
75          }
76          return null;
77      }
78  
79      /**
80       * Dump table entry to file stream in binary format.
81       *
82       * @param file Output file stream
83       * @throws IOException if an I/O Exception occurs in writeShort
84       */
85      public void dump(final DataOutputStream file) throws IOException {
86          file.writeShort(providesIndex);
87          file.writeShort(providesWithCount);
88          for (final int entry : providesWithIndex) {
89              file.writeShort(entry);
90          }
91      }
92  
93      /**
94       * @return String representation
95       */
96      @Override
97      public String toString() {
98          return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
99      }
100 
101     /**
102      * @return Resolved string representation
103      */
104     public String toString(final ConstantPool constantPool) {
105         final StringBuilder buf = new StringBuilder();
106         final String interfaceName = constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
107         buf.append(Utility.compactClassName(interfaceName, false));
108         buf.append(", with(").append(providesWithCount).append("):\n");
109         for (final int index : providesWithIndex) {
110             final String className = constantPool.getConstantString(index, Const.CONSTANT_Class);
111             buf.append("      ").append(Utility.compactClassName(className, false)).append("\n");
112         }
113         return buf.substring(0, buf.length() - 1); // remove the last newline
114     }
115 }