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 */ 017package org.apache.bcel.classfile; 018 019import java.io.DataInput; 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.Const; 024 025/** 026 * This class is derived from the abstract {@link Constant} and represents a reference to a module. 027 * 028 * <p> 029 * Note: Early access Java 9 support- currently subject to change 030 * </p> 031 * 032 * @see Constant 033 * @since 6.1 034 */ 035public final class ConstantModule extends Constant implements ConstantObject { 036 037 private int nameIndex; 038 039 /** 040 * Initialize from another object. 041 */ 042 public ConstantModule(final ConstantModule c) { 043 this(c.getNameIndex()); 044 } 045 046 /** 047 * Initialize instance from file data. 048 * 049 * @param file Input stream 050 * @throws IOException if an I/O error occurs. 051 */ 052 ConstantModule(final DataInput file) throws IOException { 053 this(file.readUnsignedShort()); 054 } 055 056 /** 057 * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8. 058 */ 059 public ConstantModule(final int nameIndex) { 060 super(Const.CONSTANT_Module); 061 this.nameIndex = nameIndex; 062 } 063 064 /** 065 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., 066 * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 067 * 068 * @param v Visitor object 069 */ 070 @Override 071 public void accept(final Visitor v) { 072 v.visitConstantModule(this); 073 } 074 075 /** 076 * Dump constant module to file stream in binary format. 077 * 078 * @param file Output file stream 079 * @throws IOException if an I/O error occurs. 080 */ 081 @Override 082 public void dump(final DataOutputStream file) throws IOException { 083 file.writeByte(super.getTag()); 084 file.writeShort(nameIndex); 085 } 086 087 /** 088 * @return dereferenced string 089 */ 090 public String getBytes(final ConstantPool cp) { 091 return (String) getConstantValue(cp); 092 } 093 094 /** 095 * @return String object 096 */ 097 @Override 098 public Object getConstantValue(final ConstantPool cp) { 099 return cp.getConstantUtf8(nameIndex).getBytes(); 100 } 101 102 /** 103 * @return Name index in constant pool of module name. 104 */ 105 public int getNameIndex() { 106 return nameIndex; 107 } 108 109 /** 110 * @param nameIndex the name index in the constant pool of this Constant Module 111 */ 112 public void setNameIndex(final int nameIndex) { 113 this.nameIndex = nameIndex; 114 } 115 116 /** 117 * @return String representation. 118 */ 119 @Override 120 public String toString() { 121 return super.toString() + "(nameIndex = " + nameIndex + ")"; 122 } 123}