I am using BCEL since one week, and I have a problems
inserting a method in a method.
I want to insert in every method of every class, a method.
But my class doesn't work and I have a
IncompatibleClassChangeError error and I don't understand
from where, could be the error.
Sorry for my english.
this is my code:
try {
JavaClass clazz =
Repository.lookupClass("front.persistance.action.BlobCl
obInitAction");
ConstantPool constants =
clazz.getConstantPool();
cp = new ConstantPoolGen(constants);
String nomStr = clazz.getClassName();
nomClasseStr =
nomStr.substring(nomStr.lastIndexOf(".") + 1) +
".class";
//si les variables de la classe ne contiennent
pas de logger
//on insere partt
Method[] methods = clazz.getMethods();
//on parcours toutes les methodes pour les
rajouter
for (int i = 0; i < methods.length; i++) {
String methodeStr = methods[i].toString();
String nomMethodeStr =
methodeStr.substring(0, methodeStr.indexOf("("));
nomMethodeStr =
nomMethodeStr.substring(nomMethodeStr.lastIndexOf("
") + 1);
if (!(nomMethodeStr.charAt(0) == '<'
&&
nomMethodeStr.charAt(nomMethodeStr.length() - 1) == '>'))
{
Method stripped = ajoutLogger(new
MethodGen(methods[i], clazz.getClassName(), cp));
if (stripped != null) {
methods[i] = stripped; // on écrase
la méthode par celle recrée
}
}
}
clazz.setConstantPool(cp.getFinalConstantPool());
clazz.dump(nomClasseStr);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
private Method ajoutLogger(MethodGen methodGen) {
try {
InstructionFactory factory = new
InstructionFactory(cp);
InstructionList instructionFinalList = new
InstructionList();
InstructionList instructionList =
methodGen.getInstructionList();
instructionFinalList.append(new PUSH(cp,
nomClasseStr));
instructionFinalList.append(factory.createInvoke("java.
io.PrintStream", "println",
Type.VOID, new Type[] { Type.STRING },
Constants.INVOKESTATIC));
instructionFinalList.append(instructionList);
methodGen.setInstructionList(instructionFinalList);
methodGen.setMaxStack();
Method method = methodGen.getMethod();
instructionFinalList.dispose();
System.out.println(methodGen.getMethod().toString());
return method;
} catch (Exception e) {
System.err.println(e.getMessage());
return null;
}
}
Matthew
This e-mail and any attachment is for authorised use by the
intended recipient(s) only. It may contain proprietary
material, confidential information and/or be subject to
legal privilege. It should not be copied, disclosed to,
retained or used by, any other party. If you are not an
intended recipient then please promptly delete this e-mail
and any attachment and all copies and inform the sender.
Thank you.
|