1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.bcel.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from the abstract {@link Constant}
28  * and represents a reference to a package.
29  *
30  * <p>Note: Early access Java 9 support- currently subject to change</p>
31  *
32  * @see     Constant
33  * @since 6.1
34  */
35 public final class ConstantPackage extends Constant implements ConstantObject {
36 
37     private int name_index;
38 
39 
40     /**
41      * Initialize from another object.
42      */
ConstantPackage(final ConstantPackage c)43     public ConstantPackage(final ConstantPackage c) {
44         this(c.getNameIndex());
45     }
46 
47 
48     /**
49      * Initialize instance from file data.
50      *
51      * @param file Input stream
52      * @throws IOException
53      */
ConstantPackage(final DataInput file)54     ConstantPackage(final DataInput file) throws IOException {
55         this(file.readUnsignedShort());
56     }
57 
58 
59     /**
60      * @param name_index Name index in constant pool.  Should refer to a
61      * ConstantUtf8.
62      */
ConstantPackage(final int name_index)63     public ConstantPackage(final int name_index) {
64         super(Const.CONSTANT_Package);
65         this.name_index = name_index;
66     }
67 
68 
69     /**
70      * Called by objects that are traversing the nodes of the tree implicitely
71      * defined by the contents of a Java class. I.e., the hierarchy of methods,
72      * fields, attributes, etc. spawns a tree of objects.
73      *
74      * @param v Visitor object
75      */
76     @Override
accept( final Visitor v )77     public void accept( final Visitor v ) {
78         v.visitConstantPackage(this);
79     }
80 
81 
82     /**
83      * Dump constant package to file stream in binary format.
84      *
85      * @param file Output file stream
86      * @throws IOException
87      */
88     @Override
dump( final DataOutputStream file )89     public final void dump( final DataOutputStream file ) throws IOException {
90         file.writeByte(super.getTag());
91         file.writeShort(name_index);
92     }
93 
94 
95     /**
96      * @return Name index in constant pool of package name.
97      */
getNameIndex()98     public final int getNameIndex() {
99         return name_index;
100     }
101 
102 
103     /**
104      * @param name_index the name index in the constant pool of this Constant Package
105      */
setNameIndex( final int name_index )106     public final void setNameIndex( final int name_index ) {
107         this.name_index = name_index;
108     }
109 
110 
111     /** @return String object
112      */
113     @Override
getConstantValue( final ConstantPool cp )114     public Object getConstantValue( final ConstantPool cp ) {
115         final Constant c = cp.getConstant(name_index, Const.CONSTANT_Utf8);
116         return ((ConstantUtf8) c).getBytes();
117     }
118 
119 
120     /** @return dereferenced string
121      */
getBytes( final ConstantPool cp )122     public String getBytes( final ConstantPool cp ) {
123         return (String) getConstantValue(cp);
124     }
125 
126 
127     /**
128      * @return String representation.
129      */
130     @Override
toString()131     public final String toString() {
132         return super.toString() + "(name_index = " + name_index + ")";
133     }
134 }
135