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 provides table of the Module attribute. 32 * Each entry describes a service implementation that the parent module provides. 33 * 34 * @see Module 35 * @since 6.4.0 36 */ 37 public final class ModuleProvides implements Cloneable, Node { 38 39 private final int provides_index; // points to CONSTANT_Class_info 40 private final int provides_with_count; 41 private final int[] provides_with_index; // points to CONSTANT_Class_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 */ ModuleProvides(final DataInput file)50 ModuleProvides(final DataInput file) throws IOException { 51 provides_index = file.readUnsignedShort(); 52 provides_with_count = file.readUnsignedShort(); 53 provides_with_index = new int[provides_with_count]; 54 for (int i = 0; i < provides_with_count; i++) { 55 provides_with_index[i] = file.readUnsignedShort(); 56 } 57 } 58 59 60 /** 61 * Called by objects that are traversing the nodes of the tree implicitely 62 * defined by the contents of a Java class. I.e., the hierarchy of methods, 63 * fields, attributes, etc. spawns a tree of objects. 64 * 65 * @param v Visitor object 66 */ 67 @Override accept( final Visitor v )68 public void accept( final Visitor v ) { 69 v.visitModuleProvides(this); 70 } 71 72 // TODO add more getters and setters? 73 74 /** 75 * Dump table entry to file stream in binary format. 76 * 77 * @param file Output file stream 78 * @throws IOException if an I/O Exception occurs in writeShort 79 */ dump( final DataOutputStream file )80 public void dump( final DataOutputStream file ) throws IOException { 81 file.writeShort(provides_index); 82 file.writeShort(provides_with_count); 83 for (final int entry : provides_with_index) { 84 file.writeShort(entry); 85 } 86 } 87 88 89 /** 90 * @return String representation 91 */ 92 @Override toString()93 public String toString() { 94 return "provides(" + provides_index + ", " + provides_with_count + ", ...)"; 95 } 96 97 98 /** 99 * @return Resolved string representation 100 */ toString( final ConstantPool constant_pool )101 public String toString( final ConstantPool constant_pool ) { 102 final StringBuilder buf = new StringBuilder(); 103 final String interface_name = constant_pool.constantToString(provides_index, Const.CONSTANT_Class); 104 buf.append(Utility.compactClassName(interface_name, false)); 105 buf.append(", with(").append(provides_with_count).append("):\n"); 106 for (final int index : provides_with_index) { 107 final String class_name = constant_pool.getConstantString(index, Const.CONSTANT_Class); 108 buf.append(" ").append(Utility.compactClassName(class_name, false)).append("\n"); 109 } 110 return buf.substring(0, buf.length()-1); // remove the last newline 111 } 112 113 114 /** 115 * @return deep copy of this object 116 */ copy()117 public ModuleProvides copy() { 118 try { 119 return (ModuleProvides) clone(); 120 } catch (final CloneNotSupportedException e) { 121 // TODO should this throw? 122 } 123 return null; 124 } 125 } 126