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 requires table of the Module attribute. 028 * Each entry describes a module on which the parent module depends. 029 * 030 * @see Module 031 * @since 6.4.0 032 */ 033public final class ModuleRequires implements Cloneable, Node { 034 035 private final int requires_index; // points to CONSTANT_Module_info 036 private final int requires_flags; 037 private final int requires_version_index; // either 0 or points to CONSTANT_Utf8_info 038 039 040 /** 041 * Construct object from file stream. 042 * 043 * @param file Input stream 044 * @throws IOException if an I/O Exception occurs in readUnsignedShort 045 */ 046 ModuleRequires(final DataInput file) throws IOException { 047 requires_index = file.readUnsignedShort(); 048 requires_flags = file.readUnsignedShort(); 049 requires_version_index = file.readUnsignedShort(); 050 } 051 052 053 /** 054 * Called by objects that are traversing the nodes of the tree implicitely 055 * defined by the contents of a Java class. I.e., the hierarchy of methods, 056 * fields, attributes, etc. spawns a tree of objects. 057 * 058 * @param v Visitor object 059 */ 060 @Override 061 public void accept( final Visitor v ) { 062 v.visitModuleRequires(this); 063 } 064 065 // TODO add more getters and setters? 066 067 /** 068 * Dump table entry to file stream in binary format. 069 * 070 * @param file Output file stream 071 * @throws IOException if an I/O Exception occurs in writeShort 072 */ 073 public void dump( final DataOutputStream file ) throws IOException { 074 file.writeShort(requires_index); 075 file.writeShort(requires_flags); 076 file.writeShort(requires_version_index); 077 } 078 079 080 /** 081 * @return String representation 082 */ 083 @Override 084 public String toString() { 085 return "requires(" + requires_index + ", " + String.format("%04x", requires_flags) + ", " + requires_version_index + ")"; 086 } 087 088 089 /** 090 * @return Resolved string representation 091 */ 092 public String toString( final ConstantPool constant_pool ) { 093 final StringBuilder buf = new StringBuilder(); 094 final String module_name = constant_pool.constantToString(requires_index, Const.CONSTANT_Module); 095 buf.append(Utility.compactClassName(module_name, false)); 096 buf.append(", ").append(String.format("%04x", requires_flags)); 097 final String version = requires_version_index == 0 ? "0" : constant_pool.getConstantString(requires_version_index, Const.CONSTANT_Utf8); 098 buf.append(", ").append(version); 099 return buf.toString(); 100 } 101 102 103 /** 104 * @return deep copy of this object 105 */ 106 public ModuleRequires copy() { 107 try { 108 return (ModuleRequires) clone(); 109 } catch (final CloneNotSupportedException e) { 110 // TODO should this throw? 111 } 112 return null; 113 } 114}