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.
28   * Each entry describes a service 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      /**
41       * Construct 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  
56      /**
57       * Called by objects that are traversing the nodes of the tree implicitely
58       * defined by the contents of a Java class. I.e., the hierarchy of methods,
59       * fields, attributes, etc. spawns a tree of objects.
60       *
61       * @param v Visitor object
62       */
63      @Override
64      public void accept( final Visitor v ) {
65          v.visitModuleProvides(this);
66      }
67  
68      // TODO add more getters and setters?
69  
70      /**
71       * Dump table entry to file stream in binary format.
72       *
73       * @param file Output file stream
74       * @throws IOException if an I/O Exception occurs in writeShort
75       */
76      public void dump( final DataOutputStream file ) throws IOException {
77          file.writeShort(providesIndex);
78          file.writeShort(providesWithCount);
79          for (final int entry : providesWithIndex) {
80              file.writeShort(entry);
81          }
82      }
83  
84  
85      /**
86       * @return String representation
87       */
88      @Override
89      public String toString() {
90          return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
91      }
92  
93  
94      /**
95       * @return Resolved string representation
96       */
97      public String toString( final ConstantPool constant_pool ) {
98          final StringBuilder buf = new StringBuilder();
99          final String interface_name = constant_pool.constantToString(providesIndex, Const.CONSTANT_Class);
100         buf.append(Utility.compactClassName(interface_name, false));
101         buf.append(", with(").append(providesWithCount).append("):\n");
102         for (final int index : providesWithIndex) {
103             final String class_name = constant_pool.getConstantString(index, Const.CONSTANT_Class);
104             buf.append("      ").append(Utility.compactClassName(class_name, false)).append("\n");
105         }
106         return buf.substring(0, buf.length()-1); // remove the last newline
107     }
108 
109 
110     /**
111      * @return deep copy of this object
112      */
113     public ModuleProvides copy() {
114         try {
115             return (ModuleProvides) clone();
116         } catch (final CloneNotSupportedException e) {
117             // TODO should this throw?
118         }
119         return null;
120     }
121 }