1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataInput; 25 import java.io.DataOutputStream; 26 import java.io.IOException; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 30 /** 31 * This class is derived from <em>Attribute</em> and represents the list of modules required, exported, opened or provided by a module. 32 * There may be at most one Module attribute in a ClassFile structure. 33 * 34 * @see Attribute 35 * @since 6.4.0 36 */ 37 public final class Module extends Attribute { 38 39 private final int module_name_index; 40 private final int module_flags; 41 private final int module_version_index; 42 43 private ModuleRequires[] requires_table; 44 private ModuleExports[] exports_table; 45 private ModuleOpens[] opens_table; 46 private final int uses_count; 47 private final int[] uses_index; 48 private ModuleProvides[] provides_table; 49 50 /** 51 * Construct object from input stream. 52 * @param name_index Index in constant pool 53 * @param length Content length in bytes 54 * @param input Input stream 55 * @param constant_pool Array of constants 56 * @throws IOException 57 */ Module(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)58 Module(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 59 super(Const.ATTR_MODULE, name_index, length, constant_pool); 60 61 module_name_index = input.readUnsignedShort(); 62 module_flags = input.readUnsignedShort(); 63 module_version_index = input.readUnsignedShort(); 64 65 final int requires_count = input.readUnsignedShort(); 66 requires_table = new ModuleRequires[requires_count]; 67 for (int i = 0; i < requires_count; i++) { 68 requires_table[i] = new ModuleRequires(input); 69 } 70 71 final int exports_count = input.readUnsignedShort(); 72 exports_table = new ModuleExports[exports_count]; 73 for (int i = 0; i < exports_count; i++) { 74 exports_table[i] = new ModuleExports(input); 75 } 76 77 final int opens_count = input.readUnsignedShort(); 78 opens_table = new ModuleOpens[opens_count]; 79 for (int i = 0; i < opens_count; i++) { 80 opens_table[i] = new ModuleOpens(input); 81 } 82 83 uses_count = input.readUnsignedShort(); 84 uses_index = new int[uses_count]; 85 for (int i = 0; i < uses_count; i++) { 86 uses_index[i] = input.readUnsignedShort(); 87 } 88 89 final int provides_count = input.readUnsignedShort(); 90 provides_table = new ModuleProvides[provides_count]; 91 for (int i = 0; i < provides_count; i++) { 92 provides_table[i] = new ModuleProvides(input); 93 } 94 } 95 96 97 /** 98 * Called by objects that are traversing the nodes of the tree implicitely 99 * defined by the contents of a Java class. I.e., the hierarchy of methods, 100 * fields, attributes, etc. spawns a tree of objects. 101 * 102 * @param v Visitor object 103 */ 104 @Override accept( final Visitor v )105 public void accept( final Visitor v ) { 106 v.visitModule(this); 107 } 108 109 // TODO add more getters and setters? 110 111 /** 112 * @return table of required modules 113 * @see ModuleRequires 114 */ getRequiresTable()115 public ModuleRequires[] getRequiresTable() { 116 return requires_table; 117 } 118 119 120 /** 121 * @return table of exported interfaces 122 * @see ModuleExports 123 */ getExportsTable()124 public ModuleExports[] getExportsTable() { 125 return exports_table; 126 } 127 128 129 /** 130 * @return table of provided interfaces 131 * @see ModuleOpens 132 */ getOpensTable()133 public ModuleOpens[] getOpensTable() { 134 return opens_table; 135 } 136 137 138 /** 139 * @return table of provided interfaces 140 * @see ModuleProvides 141 */ getProvidesTable()142 public ModuleProvides[] getProvidesTable() { 143 return provides_table; 144 } 145 146 147 /** 148 * Dump Module attribute to file stream in binary format. 149 * 150 * @param file Output file stream 151 * @throws IOException 152 */ 153 @Override dump( final DataOutputStream file )154 public void dump( final DataOutputStream file ) throws IOException { 155 super.dump(file); 156 157 file.writeShort(module_name_index); 158 file.writeShort(module_flags); 159 file.writeShort(module_version_index); 160 161 file.writeShort(requires_table.length); 162 for (final ModuleRequires entry : requires_table) { 163 entry.dump(file); 164 } 165 166 file.writeShort(exports_table.length); 167 for (final ModuleExports entry : exports_table) { 168 entry.dump(file); 169 } 170 171 file.writeShort(opens_table.length); 172 for (final ModuleOpens entry : opens_table) { 173 entry.dump(file); 174 } 175 176 file.writeShort(uses_index.length); 177 for (final int entry : uses_index) { 178 file.writeShort(entry); 179 } 180 181 file.writeShort(provides_table.length); 182 for (final ModuleProvides entry : provides_table) { 183 entry.dump(file); 184 } 185 } 186 187 188 /** 189 * @return String representation, i.e., a list of packages. 190 */ 191 @Override toString()192 public String toString() { 193 final ConstantPool cp = super.getConstantPool(); 194 final StringBuilder buf = new StringBuilder(); 195 buf.append("Module:\n"); 196 buf.append(" name: ") .append(cp.getConstantString(module_name_index, Const.CONSTANT_Module).replace('/', '.')).append("\n"); 197 buf.append(" flags: ") .append(String.format("%04x", module_flags)).append("\n"); 198 final String version = module_version_index == 0 ? "0" : cp.getConstantString(module_version_index, Const.CONSTANT_Utf8); 199 buf.append(" version: ") .append(version).append("\n"); 200 201 buf.append(" requires(").append(requires_table.length).append("):\n"); 202 for (final ModuleRequires module : requires_table) { 203 buf.append(" ").append(module.toString(cp)).append("\n"); 204 } 205 206 buf.append(" exports(").append(exports_table.length).append("):\n"); 207 for (final ModuleExports module : exports_table) { 208 buf.append(" ").append(module.toString(cp)).append("\n"); 209 } 210 211 buf.append(" opens(").append(opens_table.length).append("):\n"); 212 for (final ModuleOpens module : opens_table) { 213 buf.append(" ").append(module.toString(cp)).append("\n"); 214 } 215 216 buf.append(" uses(").append(uses_index.length).append("):\n"); 217 for (final int index : uses_index) { 218 final String class_name = cp.getConstantString(index, Const.CONSTANT_Class); 219 buf.append(" ").append(Utility.compactClassName(class_name, false)).append("\n"); 220 } 221 222 buf.append(" provides(").append(provides_table.length).append("):\n"); 223 for (final ModuleProvides module : provides_table) { 224 buf.append(" ").append(module.toString(cp)).append("\n"); 225 } 226 227 return buf.substring(0, buf.length()-1); // remove the last newline 228 } 229 230 231 /** 232 * @return deep copy of this attribute 233 */ 234 @Override copy( final ConstantPool _constant_pool )235 public Attribute copy( final ConstantPool _constant_pool ) { 236 final Module c = (Module) clone(); 237 238 c.requires_table = new ModuleRequires[requires_table.length]; 239 for (int i = 0; i < requires_table.length; i++) { 240 c.requires_table[i] = requires_table[i].copy(); 241 } 242 243 c.exports_table = new ModuleExports[exports_table.length]; 244 for (int i = 0; i < exports_table.length; i++) { 245 c.exports_table[i] = exports_table[i].copy(); 246 } 247 248 c.opens_table = new ModuleOpens[opens_table.length]; 249 for (int i = 0; i < opens_table.length; i++) { 250 c.opens_table[i] = opens_table[i].copy(); 251 } 252 253 c.provides_table = new ModuleProvides[provides_table.length]; 254 for (int i = 0; i < provides_table.length; i++) { 255 c.provides_table[i] = provides_table[i].copy(); 256 } 257 258 c.setConstantPool(_constant_pool); 259 return c; 260 } 261 } 262