* Copyright (C) 2010 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
package org.fife.rsta.ac.java.classreader.attributes;
import org.fife.rsta.ac.java.classreader.*;
import org.fife.rsta.ac.java.classreader.constantpool.*;
* The "<code>ConstantValue</code>" attribute, as defined by 4.7.2 of the
public class ConstantValue extends AttributeInfo {
* An index into the constant pool that gives the constant value
* represented by this attribute.
private int constantValueIndex; // u2
* @param cf The class file.
* @param constantValueIndex The index into the constant pool that gives
* the constant value represented by this attribute.
public ConstantValue(ClassFile cf, int constantValueIndex) {
this.constantValueIndex = constantValueIndex;
* Returns the index into the constant pool that gives the constant value
* represented by this attribute.
public int getConstantValueIndex() {
return constantValueIndex;
* Returns the constant's value, as a string.
* @return The constant's value, as a string.
public String getConstantValueAsString() {
ClassFile cf = getClassFile();
ConstantPoolInfo cpi = cf.getConstantPoolInfo(getConstantValueIndex());
if (cpi instanceof ConstantDoubleInfo) {
ConstantDoubleInfo cdi = (ConstantDoubleInfo)cpi;
double value = cdi.getDoubleValue();
return Double.toString(value);
else if (cpi instanceof ConstantFloatInfo) {
ConstantFloatInfo cfi = (ConstantFloatInfo)cpi;
float value = cfi.getFloatValue();
return Float.toString(value);
else if (cpi instanceof ConstantIntegerInfo) {
ConstantIntegerInfo cii = (ConstantIntegerInfo)cpi;
int value = cii.getIntValue();
return Integer.toString(value);
else if (cpi instanceof ConstantLongInfo) {
ConstantLongInfo cli = (ConstantLongInfo)cpi;
long value = cli.getLongValue();
return Long.toString(value);
else if (cpi instanceof ConstantStringInfo) {
ConstantStringInfo csi = (ConstantStringInfo)cpi;
return csi.getStringValue();
return "INVALID_CONSTANT_TYPE_" + cpi.toString();