001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025 026/** 027 * This class represents an entry in the opens table of the Module attribute. Each entry describes a package which the 028 * parent module opens. 029 * 030 * @see Module 031 * @since 6.4.0 032 */ 033public final class ModuleOpens implements Cloneable, Node { 034 035 private static String getToModuleNameAtIndex(final ConstantPool constantPool, final int index) { 036 return constantPool.getConstantString(index, Const.CONSTANT_Module); 037 } 038 private final int opensIndex; // points to CONSTANT_Package_info 039 private final int opensFlags; 040 private final int opensToCount; 041 042 private final int[] opensToIndex; // points to CONSTANT_Module_info 043 044 /** 045 * Constructs object from file stream. 046 * 047 * @param file Input stream 048 * @throws IOException if an I/O Exception occurs in readUnsignedShort 049 */ 050 ModuleOpens(final DataInput file) throws IOException { 051 opensIndex = file.readUnsignedShort(); 052 opensFlags = file.readUnsignedShort(); 053 opensToCount = file.readUnsignedShort(); 054 opensToIndex = new int[opensToCount]; 055 for (int i = 0; i < opensToCount; i++) { 056 opensToIndex[i] = file.readUnsignedShort(); 057 } 058 } 059 060 /** 061 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 062 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 063 * 064 * @param v Visitor object 065 */ 066 @Override 067 public void accept(final Visitor v) { 068 v.visitModuleOpens(this); 069 } 070 071 /** 072 * @return deep copy of this object 073 */ 074 public ModuleOpens copy() { 075 try { 076 return (ModuleOpens) clone(); 077 } catch (final CloneNotSupportedException e) { 078 // TODO should this throw? 079 } 080 return null; 081 } 082 083 /** 084 * Dump table entry to file stream in binary format. 085 * 086 * @param file Output file stream 087 * @throws IOException if an I/O Exception occurs in writeShort 088 */ 089 public void dump(final DataOutputStream file) throws IOException { 090 file.writeShort(opensIndex); 091 file.writeShort(opensFlags); 092 file.writeShort(opensToCount); 093 for (final int entry : opensToIndex) { 094 file.writeShort(entry); 095 } 096 } 097 098 /** 099 * Gets the flags for this ModuleOpens. 100 * @return the opensFlags 101 * @since 6.10.0 102 */ 103 public int getOpensFlags() { 104 return opensFlags; 105 } 106 107 /** 108 * Gets the opened package name. 109 * @param constantPool the constant pool from the ClassFile 110 * @return the opened package name 111 * @since 6.10.0 112 */ 113 public String getPackageName(final ConstantPool constantPool) { 114 return constantPool.constantToString(opensIndex, Const.CONSTANT_Package); 115 } 116 117 /** 118 * Gets an array of module names for this ModuleOpens. 119 * @param constantPool Array of constants usually obtained from the ClassFile object 120 * @return array of module names following 'opens to' 121 * @since 6.10.0 122 */ 123 public String[] getToModuleNames(final ConstantPool constantPool) { 124 final String[] toModuleNames = new String[opensToCount]; 125 for (int i = 0; i < opensToCount; i++) { 126 toModuleNames[i] = getToModuleNameAtIndex(constantPool, opensToIndex[i]); 127 } 128 return toModuleNames; 129 } 130 131 /** 132 * @return String representation 133 */ 134 @Override 135 public String toString() { 136 return "opens(" + opensIndex + ", " + opensFlags + ", " + opensToCount + ", ...)"; 137 } 138 139 /** 140 * @return Resolved string representation 141 */ 142 public String toString(final ConstantPool constantPool) { 143 final StringBuilder buf = new StringBuilder(); 144 final String packageName = getPackageName(constantPool); 145 buf.append(packageName); 146 buf.append(", ").append(String.format("%04x", opensFlags)); 147 buf.append(", to(").append(opensToCount).append("):\n"); 148 for (final int index : opensToIndex) { 149 final String moduleName = getToModuleNameAtIndex(constantPool, index); 150 buf.append(" ").append(moduleName).append("\n"); 151 } 152 return buf.substring(0, buf.length() - 1); // remove the last newline 153 } 154}