use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.
the class ConstantPoolRebuilder method createNewConstantPool.
public ConstantPoolGen createNewConstantPool(ConstantPoolGen oldPool, Set<Integer> usedIndices) {
// We add all used entries to the new pool in the same order as in the old pool
// to avoid indices getting larger than before
Integer[] ids = new ArrayList<Integer>(usedIndices).toArray(new Integer[usedIndices.size()]);
Arrays.sort(ids);
// First thing we need is a map, since we need to relink the constantpool entries
idMap.clear();
// Note that index 0 is not valid, so skip it
List<Constant> constants = new ArrayList<Constant>(usedIndices.size() + 1);
constants.add(null);
int newPos = 1;
for (int id : ids) {
Constant c = oldPool.getConstant(id);
// we cannot use newPool.addConstant here, because this would add all referenced constants too,
// and we do not want that..
constants.add(c);
idMap.put(id, newPos++);
if (c instanceof ConstantLong || c instanceof ConstantDouble) {
// reserve an additional slot for those
constants.add(null);
newPos++;
}
}
// now we create new constants and map the references
Constant[] newConstants = new Constant[constants.size()];
for (int i = 1; i < constants.size(); i++) {
Constant c = constants.get(i);
if (c == null)
continue;
newConstants[i] = copyConstant(idMap, c);
}
newPool = new ConstantPoolGen(newConstants);
return newPool;
}
use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.
the class DFATool method buildPrologue.
private MethodInfo buildPrologue(MethodInfo mainMethod, List<InstructionHandle> statements, Flow flow, List<ClassInfo> clinits) {
// we use a prologue sequence for startup
InstructionList prologue = new InstructionList();
ConstantPoolGen prologueCP = mainMethod.getConstantPoolGen();
Instruction instr;
int idx;
// add magic initializers to prologue sequence
if (!analyzeBootMethod) {
instr = new ICONST(0);
prologue.append(instr);
instr = new ICONST(0);
prologue.append(instr);
idx = prologueCP.addMethodref("com.jopdesign.sys.GC", "init", "(II)V");
instr = new INVOKESTATIC(idx);
prologue.append(instr);
}
// add class initializers
for (ClassInfo clinit : clinits) {
MemberID cSig = appInfo.getClinitSignature(clinit.getClassName());
idx = prologueCP.addMethodref(cSig.getClassName(), cSig.getMemberName(), cSig.getDescriptor().toString());
instr = new INVOKESTATIC(idx);
prologue.append(instr);
}
if (analyzeBootMethod) {
// Let's just analyze the full boot method, so that the callgraph-builder is happy.
// Doing this after clinit, so that we have DFA infos on fields in JVMHelp.init()
idx = prologueCP.addMethodref("com.jopdesign.sys.Startup", "boot", "()V");
instr = new INVOKESTATIC(idx);
prologue.append(instr);
}
// add main method
instr = new ACONST_NULL();
prologue.append(instr);
idx = prologueCP.addMethodref(mainMethod.getClassName(), mainMethod.getShortName(), mainMethod.getDescriptor().toString());
instr = new INVOKESTATIC(idx);
prologue.append(instr);
// // invoke startMission() to ensure analysis of threads
// idx = prologueCP.addMethodref("joprt.RtThread", "startMission", "()V");
// instr = new INVOKESTATIC(idx);
// prologue.append(instr);
instr = new NOP();
prologue.append(instr);
prologue.setPositions(true);
// add prologue to program structure
for (Iterator l = prologue.iterator(); l.hasNext(); ) {
InstructionHandle handle = (InstructionHandle) l.next();
statements.add(handle);
if (handle.getInstruction() instanceof GOTO) {
GOTO g = (GOTO) handle.getInstruction();
flow.addEdge(new FlowEdge(handle, g.getTarget(), FlowEdge.NORMAL_EDGE));
} else if (handle.getNext() != null) {
flow.addEdge(new FlowEdge(handle, handle.getNext(), FlowEdge.NORMAL_EDGE));
}
}
MemberID pSig = new MemberID(prologueName, Descriptor.parse(prologueSig));
MethodInfo mi = mainMethod.getClassInfo().createMethod(pSig, null, prologue);
mi.setAccessType(AccessType.ACC_PRIVATE);
return mi;
}
use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.
the class DescendingClassTraverser method visitConstantPool.
///////////////////////////////////////////////////////////////////
// Other methods to visit only parts of a class
///////////////////////////////////////////////////////////////////
public void visitConstantPool(ClassInfo classInfo) {
ConstantPoolGen cpg = classInfo.getConstantPoolGen();
if (visitor.visitConstantPoolGen(classInfo, cpg)) {
bcelVisitor.setClassInfo(classInfo);
for (int i = 1; i < cpg.getSize(); i++) {
Constant c = cpg.getConstant(i);
// Some entries might be null (continuation of previous entry)
if (c == null)
continue;
c.accept(bcelVisitor);
}
visitor.finishConstantPoolGen(classInfo, cpg);
}
}
use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.
the class DescendingClassTraverser method visitConstant.
public void visitConstant(ClassInfo classInfo, int index) {
ConstantPoolGen cpg = classInfo.getConstantPoolGen();
visitConstant(classInfo, cpg.getConstant(index));
}
use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.
the class ConstantPoolReferenceFinder method visitPoolReferences.
/**
* Helper method which applies a visitor to all constants given by a set of ids.
* @param classInfo the class containing the constant pool
* @param visitor the visitor to apply
* @param ids a set of ids of pool entries to visit
*/
private static void visitPoolReferences(ClassInfo classInfo, ClassElementVisitor visitor, Set<Integer> ids) {
DescendingClassTraverser traverser = new DescendingClassTraverser(visitor);
ConstantPoolGen cpg = classInfo.getConstantPoolGen();
// iterate over the given pool entries, call the visitor
for (Integer id : ids) {
traverser.visitConstant(classInfo, cpg.getConstant(id));
}
}
Aggregations