use of org.apache.bcel.classfile.Constant in project jop by jop-devel.
the class ReplaceNativeAndCPIdx method getFieldOffset.
/**
* Get field offset: relative offset for object fields, absolute
* addresses for static fields.
*
* TODO: remove the dead code in constant pool replacement
* TODO: remove not used constants
* @param cp
* @param index
* @return
*/
private int getFieldOffset(ConstantPool cp, int index) {
// from ClassInfo.resolveCPool
Constant co = cp.getConstant(index);
int fidx = ((ConstantFieldref) co).getClassIndex();
ConstantClass fcl = (ConstantClass) cp.getConstant(fidx);
String fclname = fcl.getBytes(cp).replace('/', '.');
// got the class name
int sigidx = ((ConstantFieldref) co).getNameAndTypeIndex();
ConstantNameAndType signt = (ConstantNameAndType) cp.getConstant(sigidx);
String sigstr = signt.getName(cp) + signt.getSignature(cp);
JopClassInfo clinf = (JopClassInfo) ai.cliMap.get(fclname);
int j;
boolean found = false;
while (!found) {
for (j = 0; j < clinf.clft.len; ++j) {
if (clinf.clft.key[j].equals(sigstr)) {
found = true;
return clinf.clft.idx[j];
}
}
if (!found) {
clinf = (JopClassInfo) clinf.superClass;
if (clinf == null) {
System.out.println("Error: field " + fclname + "." + sigstr + " not found!");
break;
}
}
}
System.out.println("Error in getFieldOffset()");
System.exit(-1);
return 0;
}
use of org.apache.bcel.classfile.Constant in project candle-decompiler by bradsdavis.
the class ClassIntermediateVisitor method visitJavaClass.
@Override
public void visitJavaClass(JavaClass obj) {
this.classBlock.setClassName(javaClass.getClassName());
this.classBlock.setPackageName(obj.getPackageName());
this.classBlock.setSuperClassName(obj.getSuperclassName());
// process the pool.
Constant[] pool = obj.getConstantPool().getConstantPool();
for (Constant c : pool) {
if (c == null)
continue;
c.accept(this);
}
Field[] fields = obj.getFields();
for (int i = 0, j = fields.length; i < j; i++) {
fields[i].accept(this);
}
// run through all of the methods
Method[] methods = obj.getMethods();
for (int i = 0, j = methods.length; i < j; i++) {
methods[i].accept(this);
}
}
use of org.apache.bcel.classfile.Constant in project jop by jop-devel.
the class FindUsedConstants method find.
private void find(Method method) {
MethodGen mg = new MethodGen(method, clazz.getClassName(), cpool);
InstructionList il = mg.getInstructionList();
InstructionFinder f = new InstructionFinder(il);
// find instructions that access the constant pool
// collect all indices to constants in ClassInfo
String cpInstr = "CPInstruction";
for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
InstructionHandle[] match = (InstructionHandle[]) it.next();
InstructionHandle first = match[0];
CPInstruction ii = (CPInstruction) first.getInstruction();
int idx = ii.getIndex();
Constant co = cpool.getConstant(idx);
int len = 1;
switch(co.getTag()) {
case Constants.CONSTANT_Long:
case Constants.CONSTANT_Double:
len = 2;
break;
}
// we don't need the field references in the cpool anymore
if (co.getTag() != Constants.CONSTANT_Fieldref) {
getCli().addUsedConst(idx, len);
}
// also modify the index!
// Constant cnst = cpool.getConstant(ii.getIndex());
// int newIndex = addConstant(cnst);
//System.out.println(ii+" -> "+newIndex);
// ii.setIndex(newIndex);
}
il.dispose();
CodeExceptionGen[] et = mg.getExceptionHandlers();
for (int i = 0; i < et.length; i++) {
ObjectType ctype = et[i].getCatchType();
if (ctype != null) {
getCli().addUsedConst(cpool.lookupClass(ctype.getClassName()), 1);
}
}
}
use of org.apache.bcel.classfile.Constant in project jop by jop-devel.
the class ClassInfo method setConstantInfo.
/**
* Replace a constant with a new constant value in the constant pool.
* Be aware that this does not check for duplicate entries, and may create additional
* new entries in the constant pool.
*
* @see #addConstantInfo(ConstantInfo)
* @param i the index of the constant to replace.
* @param constant the new value.
*/
public void setConstantInfo(int i, ConstantInfo constant) {
Constant c = constant.createConstant(cpg);
cpg.setConstant(i, c);
}
use of org.apache.bcel.classfile.Constant 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