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