use of org.apache.bcel.classfile.ConstantString in project jop by jop-devel.
the class ConstantInfo method createFromConstant.
/**
* Create a new constantInfo from a BCEL constant.
* If the constantpool contains invalid data, a {@link JavaClassFormatError} is thrown.
*
* @param cp the constantpool used to resolve the index references.
* @param constant the BCEL constant to convert
* @return a new ConstantInfo containing the constant value.
*/
public static ConstantInfo createFromConstant(ConstantPool cp, Constant constant) {
MemberID sig;
MethodRef methodRef;
ConstantNameAndType nRef;
AppInfo appInfo = AppInfo.getSingleton();
byte tag = constant.getTag();
switch(tag) {
case Constants.CONSTANT_Class:
ClassRef classRef = appInfo.getClassRef(((ConstantClass) constant).getBytes(cp).replace('/', '.'));
return new ConstantClassInfo(classRef);
case Constants.CONSTANT_Fieldref:
ConstantFieldref fRef = (ConstantFieldref) constant;
nRef = (ConstantNameAndType) cp.getConstant(fRef.getNameAndTypeIndex());
sig = new MemberID(fRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
FieldRef fieldRef = appInfo.getFieldRef(sig);
return new ConstantFieldInfo(fieldRef);
case Constants.CONSTANT_Methodref:
ConstantMethodref mRef = (ConstantMethodref) constant;
nRef = (ConstantNameAndType) cp.getConstant(mRef.getNameAndTypeIndex());
sig = new MemberID(mRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
methodRef = appInfo.getMethodRef(sig, false);
return new ConstantMethodInfo(methodRef);
case Constants.CONSTANT_InterfaceMethodref:
ConstantInterfaceMethodref imRef = (ConstantInterfaceMethodref) constant;
nRef = (ConstantNameAndType) cp.getConstant(imRef.getNameAndTypeIndex());
sig = new MemberID(imRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
methodRef = appInfo.getMethodRef(sig, true);
return new ConstantMethodInfo(methodRef);
case Constants.CONSTANT_String:
return new ConstantStringInfo(((ConstantString) constant).getBytes(cp), false);
case Constants.CONSTANT_Integer:
return new ConstantIntegerInfo(((ConstantInteger) constant).getBytes());
case Constants.CONSTANT_Float:
return new ConstantFloatInfo(((ConstantFloat) constant).getBytes());
case Constants.CONSTANT_Long:
return new ConstantLongInfo(((ConstantLong) constant).getBytes());
case Constants.CONSTANT_Double:
return new ConstantDoubleInfo(((ConstantDouble) constant).getBytes());
case Constants.CONSTANT_NameAndType:
String name = ((ConstantNameAndType) constant).getName(cp);
String signature = ((ConstantNameAndType) constant).getSignature(cp);
return new ConstantNameAndTypeInfo(new MemberID(name, signature));
case Constants.CONSTANT_Utf8:
return new ConstantStringInfo(((ConstantUtf8) constant).getBytes(), true);
default:
throw new JavaClassFormatError("Invalid byte tag in constant pool: " + tag);
}
}
use of org.apache.bcel.classfile.ConstantString in project jop by jop-devel.
the class ConstantPoolRebuilder method copyConstant.
public static Constant copyConstant(Map<Integer, Integer> idMap, Constant c) {
if (c instanceof ConstantClass) {
int index = ((ConstantClass) c).getNameIndex();
return new ConstantClass(idMap.get(index));
} else if (c instanceof ConstantFieldref) {
int clsIdx = ((ConstantFieldref) c).getClassIndex();
int nameIdx = ((ConstantFieldref) c).getNameAndTypeIndex();
return new ConstantFieldref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantMethodref) {
int clsIdx = ((ConstantMethodref) c).getClassIndex();
int nameIdx = ((ConstantMethodref) c).getNameAndTypeIndex();
return new ConstantMethodref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantInterfaceMethodref) {
int clsIdx = ((ConstantInterfaceMethodref) c).getClassIndex();
int nameIdx = ((ConstantInterfaceMethodref) c).getNameAndTypeIndex();
return new ConstantInterfaceMethodref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantString) {
int index = ((ConstantString) c).getStringIndex();
return new ConstantString(idMap.get(index));
} else if (c instanceof ConstantInteger) {
return new ConstantInteger((ConstantInteger) c);
} else if (c instanceof ConstantFloat) {
return new ConstantFloat((ConstantFloat) c);
} else if (c instanceof ConstantLong) {
return new ConstantLong((ConstantLong) c);
} else if (c instanceof ConstantDouble) {
return new ConstantDouble((ConstantDouble) c);
} else if (c instanceof ConstantNameAndType) {
int nameIdx = ((ConstantNameAndType) c).getNameIndex();
int sigIdx = ((ConstantNameAndType) c).getSignatureIndex();
return new ConstantNameAndType(idMap.get(nameIdx), idMap.get(sigIdx));
} else if (c instanceof ConstantUtf8) {
return new ConstantUtf8((ConstantUtf8) c);
}
throw new JavaClassFormatError("Unknown constant type " + c);
}
use of org.apache.bcel.classfile.ConstantString in project OpenGrok by OpenGrok.
the class JavaClassAnalyzer method constantToString.
public String constantToString(Constant c, ConstantPool cp, int[] v) throws ClassFormatException {
String str;
int i, j;
byte tag = c.getTag();
switch(tag) {
case org.apache.bcel.Const.CONSTANT_Class:
i = ((ConstantClass) c).getNameIndex();
v[i] = 1;
Constant con = cp.getConstant(i, org.apache.bcel.Const.CONSTANT_Utf8);
str = Utility.compactClassName(((ConstantUtf8) con).getBytes(), false);
break;
case org.apache.bcel.Const.CONSTANT_String:
i = ((ConstantString) c).getStringIndex();
v[i] = 1;
Constant con2 = cp.getConstant(i, org.apache.bcel.Const.CONSTANT_Utf8);
str = ((ConstantUtf8) con2).getBytes();
break;
case org.apache.bcel.Const.CONSTANT_Utf8:
str = ((ConstantUtf8) c).toString();
break;
case org.apache.bcel.Const.CONSTANT_Double:
str = ((ConstantDouble) c).toString();
break;
case org.apache.bcel.Const.CONSTANT_Float:
str = ((ConstantFloat) c).toString();
break;
case org.apache.bcel.Const.CONSTANT_Long:
str = ((ConstantLong) c).toString();
break;
case org.apache.bcel.Const.CONSTANT_Integer:
str = ((ConstantInteger) c).toString();
break;
case org.apache.bcel.Const.CONSTANT_NameAndType:
i = ((ConstantNameAndType) c).getNameIndex();
v[i] = 1;
j = ((ConstantNameAndType) c).getSignatureIndex();
v[j] = 1;
String sig = constantToString(cp.getConstant(j), cp, v);
if (sig.charAt(0) == '(') {
str = Utility.methodSignatureToString(sig, constantToString(cp.getConstant(i), cp, v), " ");
} else {
str = Utility.signatureToString(sig) + ' ' + constantToString(cp.getConstant(i), cp, v);
}
break;
case org.apache.bcel.Const.CONSTANT_InterfaceMethodref:
case org.apache.bcel.Const.CONSTANT_Methodref:
case org.apache.bcel.Const.CONSTANT_Fieldref:
i = ((ConstantCP) c).getClassIndex();
v[i] = 1;
j = ((ConstantCP) c).getNameAndTypeIndex();
v[j] = 1;
str = (constantToString(cp.getConstant(i), cp, v) + ' ' + constantToString(cp.getConstant(j), cp, v));
break;
default:
// Never reached
throw new ClassFormatException("Unknown constant type " + tag);
}
return str;
}
Aggregations