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 25 import com.sun.org.apache.bcel.internal.Constants; 26 import java.io.*; 27 28 /** 29 * This class is derived from the abstract 30 * <A HREF="com.sun.org.apache.bcel.internal.classfile.Constant.html">Constant</A> class 31 * and represents a reference to a (external) class. 32 * 33 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 34 * @see Constant 35 */ 36 public final class ConstantClass extends Constant implements ConstantObject { 37 private int name_index; // Identical to ConstantString except for the name 38 39 /** 40 * Initialize from another object. 41 */ 42 public ConstantClass(ConstantClass c) { 43 this(c.getNameIndex()); 44 } 45 46 /** 47 * Initialize instance from file data. 48 * 49 * @param file Input stream 50 * @throws IOException 51 */ 52 ConstantClass(DataInputStream file) throws IOException 53 { 54 this(file.readUnsignedShort()); 55 } 56 57 /** 58 * @param name_index Name index in constant pool. Should refer to a 59 * ConstantUtf8. 60 */ 61 public ConstantClass(int name_index) { 62 super(Constants.CONSTANT_Class); 63 this.name_index = name_index; 64 } 65 66 /** 67 * Called by objects that are traversing the nodes of the tree implicitely 68 * defined by the contents of a Java class. I.e., the hierarchy of methods, 69 * fields, attributes, etc. spawns a tree of objects. 70 * 71 * @param v Visitor object 72 */ 73 public void accept(Visitor v) { 74 v.visitConstantClass(this); 75 } 76 77 /** 78 * Dump constant class to file stream in binary format. 79 * 80 * @param file Output file stream 81 * @throws IOException 82 */ 83 public final void dump(DataOutputStream file) throws IOException 84 { 85 file.writeByte(tag); 86 file.writeShort(name_index); 87 } 88 89 /** 90 * @return Name index in constant pool of class name. 91 */ 92 public final int getNameIndex() { return name_index; } 93 94 /** 95 * @param name_index. 96 */ 97 public final void setNameIndex(int name_index) { 98 this.name_index = name_index; 99 } 100 101 102 /** @return String object 103 */ 104 public Object getConstantValue(ConstantPool cp) { 105 Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8); 106 return ((ConstantUtf8)c).getBytes(); 107 } 108 109 /** @return dereferenced string 110 */ 111 public String getBytes(ConstantPool cp) { 112 return (String)getConstantValue(cp); 113 } 114 115 /** 116 * @return String representation. 117 */ 118 public final String toString() { 119 return super.toString() + "(name_index = " + name_index + ")"; 120 } 121 } | 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 is derived from the abstract {@link Constant} 32 * and represents a reference to a (external) class. 33 * 34 * @version $Id: ConstantClass.java 1749603 2016-06-21 20:50:19Z ggregory $ 35 * @see Constant 36 */ 37 public final class ConstantClass extends Constant implements ConstantObject { 38 39 private int name_index; // Identical to ConstantString except for the name 40 41 42 /** 43 * Initialize from another object. 44 */ 45 public ConstantClass(final ConstantClass c) { 46 this(c.getNameIndex()); 47 } 48 49 50 /** 51 * Initialize instance from file data. 52 * 53 * @param file Input stream 54 * @throws IOException 55 */ 56 ConstantClass(final DataInput file) throws IOException { 57 this(file.readUnsignedShort()); 58 } 59 60 61 /** 62 * @param name_index Name index in constant pool. Should refer to a 63 * ConstantUtf8. 64 */ 65 public ConstantClass(final int name_index) { 66 super(Const.CONSTANT_Class); 67 this.name_index = name_index; 68 } 69 70 71 /** 72 * Called by objects that are traversing the nodes of the tree implicitely 73 * defined by the contents of a Java class. I.e., the hierarchy of methods, 74 * fields, attributes, etc. spawns a tree of objects. 75 * 76 * @param v Visitor object 77 */ 78 @Override 79 public void accept( final Visitor v ) { 80 v.visitConstantClass(this); 81 } 82 83 84 /** 85 * Dump constant class to file stream in binary format. 86 * 87 * @param file Output file stream 88 * @throws IOException 89 */ 90 @Override 91 public final void dump( final DataOutputStream file ) throws IOException { 92 file.writeByte(super.getTag()); 93 file.writeShort(name_index); 94 } 95 96 97 /** 98 * @return Name index in constant pool of class name. 99 */ 100 public final int getNameIndex() { 101 return name_index; 102 } 103 104 105 /** 106 * @param name_index the name index in the constant pool of this Constant Class 107 */ 108 public final void setNameIndex( final int name_index ) { 109 this.name_index = name_index; 110 } 111 112 113 /** @return String object 114 */ 115 @Override 116 public Object getConstantValue( final ConstantPool cp ) { 117 final Constant c = cp.getConstant(name_index, Const.CONSTANT_Utf8); 118 return ((ConstantUtf8) c).getBytes(); 119 } 120 121 122 /** @return dereferenced string 123 */ 124 public String getBytes( final ConstantPool cp ) { 125 return (String) getConstantValue(cp); 126 } 127 128 129 /** 130 * @return String representation. 131 */ 132 @Override 133 public final String toString() { 134 return super.toString() + "(name_index = " + name_index + ")"; 135 } 136 } |