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 represents an entry in the requires table of the Module attribute.
32  * Each entry describes a module on which the parent module depends.
33  *
34  * @see   Module
35  * @since 6.4.0
36  */
37 public final class ModuleRequires implements Cloneable, Node {
38 
39     private final int requires_index;  // points to CONSTANT_Module_info
40     private final int requires_flags;
41     private final int requires_version_index;  // either 0 or points to CONSTANT_Utf8_info
42 
43 
44     /**
45      * Construct object from file stream.
46      *
47      * @param file Input stream
48      * @throws IOException if an I/O Exception occurs in readUnsignedShort
49      */
ModuleRequires(final DataInput file)50     ModuleRequires(final DataInput file) throws IOException {
51         requires_index = file.readUnsignedShort();
52         requires_flags = file.readUnsignedShort();
53         requires_version_index = file.readUnsignedShort();
54     }
55 
56 
57     /**
58      * Called by objects that are traversing the nodes of the tree implicitely
59      * defined by the contents of a Java class. I.e., the hierarchy of methods,
60      * fields, attributes, etc. spawns a tree of objects.
61      *
62      * @param v Visitor object
63      */
64     @Override
accept( final Visitor v )65     public void accept( final Visitor v ) {
66         v.visitModuleRequires(this);
67     }
68 
69     // TODO add more getters and setters?
70 
71     /**
72      * Dump table entry to file stream in binary format.
73      *
74      * @param file Output file stream
75      * @throws IOException if an I/O Exception occurs in writeShort
76      */
dump( final DataOutputStream file )77     public void dump( final DataOutputStream file ) throws IOException {
78         file.writeShort(requires_index);
79         file.writeShort(requires_flags);
80         file.writeShort(requires_version_index);
81     }
82 
83 
84     /**
85      * @return String representation
86      */
87     @Override
toString()88     public String toString() {
89         return "requires(" + requires_index + ", " + String.format("%04x", requires_flags) + ", " + requires_version_index + ")";
90     }
91 
92 
93     /**
94      * @return Resolved string representation
95      */
toString( final ConstantPool constant_pool )96     public String toString( final ConstantPool constant_pool ) {
97         final StringBuilder buf = new StringBuilder();
98         final String module_name = constant_pool.constantToString(requires_index, Const.CONSTANT_Module);
99         buf.append(Utility.compactClassName(module_name, false));
100         buf.append(", ").append(String.format("%04x", requires_flags));
101         final String version = requires_version_index == 0 ? "0" : constant_pool.getConstantString(requires_version_index, Const.CONSTANT_Utf8);
102         buf.append(", ").append(version);
103         return buf.toString();
104     }
105 
106 
107     /**
108      * @return deep copy of this object
109      */
copy()110     public ModuleRequires copy() {
111         try {
112             return (ModuleRequires) clone();
113         } catch (final CloneNotSupportedException e) {
114             // TODO should this throw?
115         }
116         return null;
117     }
118 }
119