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