Hello
I want to write a firmware with the bcel library and catch all exceptions
in the jar it is connected to.
I follow this way:
If there is no try catch in a method, the method's codes will be placed in
the try-catch block.
I want to make a manipulation that will add a new handler to the try catch,
if there is one.
I also want to assign the information of the exception I caught to a
variable with bcel and save it to the database.
On the code I tried: org.apache.bcel.classfile.ClassFormatException:
handlerPc [Value out of range (0 - 65.535) for type u2: -1] I'm getting the
error.
Can you help me catch the exception?
best regards
`private static void addExceptionHandling(InstructionList instructionList,
MethodGen methodGen, JavaClass javaClass, String className, String
methodName) throws TargetLostException {
if (instructionList != null) {
CodeExceptionGen[] exceptionHandlers = methodGen.getExceptionHandlers();
if (exceptionHandlers.length == 0) {
ClassGen classGen = new ClassGen(javaClass);
ObjectType exceptionType = new ObjectType("java.lang.Exception");
InstructionFactory factory = new InstructionFactory(classGen);
InstructionList il = new InstructionList();
il.append(factory.createFieldAccess("java/lang/System",
"out", new ObjectType("java/io/PrintStream"), Constants.GETSTATIC));
il.append(factory.createInvoke("java.io.PrintStream",
"println", Type.VOID, new Type[]{Type.STRING},
Constants.INVOKEVIRTUAL));
il.append(factory.createReturn(Type.VOID));
InstructionList catchList = new InstructionList();
catchList.append(factory.createPrintln("Exception caught:"));
catchList.append(factory.createReturn(Type.VOID));
InstructionHandle tryStart = il.getStart();
InstructionHandle tryEnd = il.getEnd();
InstructionHandle catchTarget = catchList.getStart();
InstructionHandle catchEnd = catchList.getEnd();
methodGen.addExceptionHandler(tryStart, tryEnd,
catchTarget, exceptionType);
methodGen.setInstructionList(il);
logger.info(String.valueOf(methodGen.getMethod().getCode())+"\n
"+methodGen.getMethod().getCode().getCode());
} else {
logger.info("Exception count{}",exceptionHandlers.length);
for(CodeExceptionGen tek: exceptionHandlers) {
}
}
}`
full code: https://github.com/okaypadak/logtrack-agent.git