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 file Input stream
53 * @throws IOException if an I/O Exception occurs in readUnsignedShort
54 */
55 ModuleProvides(final DataInput file) throws IOException {
56 providesIndex = file.readUnsignedShort();
57 providesWithCount = file.readUnsignedShort();
58 providesWithIndex = new int[providesWithCount];
59 for (int i = 0; i < providesWithCount; i++) {
60 providesWithIndex[i] = file.readUnsignedShort();
61 }
62 }
63
64 /**
65 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
66 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
67 *
68 * @param v Visitor object
69 */
70 @Override
71 public void accept(final Visitor v) {
72 v.visitModuleProvides(this);
73 }
74
75 /**
76 * @return deep copy of this object
77 */
78 public ModuleProvides copy() {
79 try {
80 return (ModuleProvides) clone();
81 } catch (final CloneNotSupportedException e) {
82 // TODO should this throw?
83 }
84 return null;
85 }
86
87 /**
88 * Dump table entry to file stream in binary format.
89 *
90 * @param file Output file stream
91 * @throws IOException if an I/O Exception occurs in writeShort
92 */
93 public void dump(final DataOutputStream file) throws IOException {
94 file.writeShort(providesIndex);
95 file.writeShort(providesWithCount);
96 for (final int entry : providesWithIndex) {
97 file.writeShort(entry);
98 }
99 }
100
101 /**
102 * Gets the array of implementation class names for this ModuleProvides.
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 * @param constantPool Array of constants usually obtained from the ClassFile object
119 * @return interface name
120 * @since 6.10.0
121 */
122 public String getInterfaceName(final ConstantPool constantPool) {
123 return constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
124 }
125
126 /**
127 * @return String representation
128 */
129 @Override
130 public String toString() {
131 return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
132 }
133
134 /**
135 * @return Resolved string representation
136 */
137 public String toString(final ConstantPool constantPool) {
138 final StringBuilder buf = new StringBuilder();
139 final String interfaceName = getInterfaceName(constantPool);
140 buf.append(interfaceName);
141 buf.append(", with(").append(providesWithCount).append("):\n");
142 for (final int index : providesWithIndex) {
143 final String className = getImplementationClassNameAtIndex(constantPool, index, true);
144 buf.append(" ").append(className).append("\n");
145 }
146 return buf.substring(0, buf.length() - 1); // remove the last newline
147 }
148 }