use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class CtConstructor method removeConsCall.
private static void removeConsCall(CodeAttribute ca) throws CannotCompileException {
CodeIterator iterator = ca.iterator();
try {
int pos = iterator.skipConstructor();
if (pos >= 0) {
int mref = iterator.u16bitAt(pos + 1);
String desc = ca.getConstPool().getMethodrefType(mref);
int num = Descriptor.numOfParameters(desc) + 1;
if (num > 3)
pos = iterator.insertGapAt(pos, num - 3, false).position;
// this
iterator.writeByte(Opcode.POP, pos++);
iterator.writeByte(Opcode.NOP, pos);
iterator.writeByte(Opcode.NOP, pos + 1);
Descriptor.Iterator it = new Descriptor.Iterator(desc);
while (true) {
it.next();
if (it.isParameter())
iterator.writeByte(it.is2byte() ? Opcode.POP2 : Opcode.POP, pos++);
else
break;
}
}
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class CtBehavior method insertAt.
/**
* Inserts bytecode at the specified line in the body.
*
* <p>If there is not
* a statement at the specified line, the bytecode might be inserted
* at the line including the first statement after that line specified.
* For example, if there is only a closing brace at that line, the
* bytecode would be inserted at another line below.
* To know exactly where the bytecode will be inserted, call with
* <code>modify</code> set to <code>false</code>.
*
* @param lineNum the line number. The bytecode is inserted at the
* beginning of the code at the line specified by this
* line number.
* @param modify if false, this method does not insert the bytecode.
* It instead only returns the line number at which
* the bytecode would be inserted.
* @param src the source code representing the inserted bytecode.
* It must be a single statement or block.
* If modify is false, the value of src can be null.
* @return the line number at which the bytecode has been inserted.
*/
public int insertAt(int lineNum, boolean modify, String src) throws CannotCompileException {
CodeAttribute ca = methodInfo.getCodeAttribute();
if (ca == null)
throw new CannotCompileException("no method body");
LineNumberAttribute ainfo = (LineNumberAttribute) ca.getAttribute(LineNumberAttribute.tag);
if (ainfo == null)
throw new CannotCompileException("no line number info");
LineNumberAttribute.Pc pc = ainfo.toNearPc(lineNum);
lineNum = pc.line;
int index = pc.index;
if (!modify)
return lineNum;
CtClass cc = declaringClass;
cc.checkModify();
CodeIterator iterator = ca.iterator();
Javac jv = new Javac(cc);
try {
jv.recordLocalVariables(ca, index);
jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers()));
jv.setMaxLocals(ca.getMaxLocals());
jv.compileStmnt(src);
Bytecode b = jv.getBytecode();
int locals = b.getMaxLocals();
int stack = b.getMaxStack();
ca.setMaxLocals(locals);
/* We assume that there is no values in the operand stack
* at the position where the bytecode is inserted.
*/
if (stack > ca.getMaxStack())
ca.setMaxStack(stack);
index = iterator.insertAt(index, b.get());
iterator.insert(b.getExceptionTable(), index);
methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
return lineNum;
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class CtBehavior method addCatch.
/**
* Adds a catch clause that handles an exception thrown in the
* body. The catch clause must end with a return or throw statement.
*
* @param src the source code representing the catch clause.
* It must be a single statement or block.
* @param exceptionType the type of the exception handled by the
* catch clause.
* @param exceptionName the name of the variable containing the
* caught exception, for example,
* <code>$e</code>.
*/
public void addCatch(String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
CtClass cc = declaringClass;
cc.checkModify();
ConstPool cp = methodInfo.getConstPool();
CodeAttribute ca = methodInfo.getCodeAttribute();
CodeIterator iterator = ca.iterator();
Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
b.setStackDepth(1);
Javac jv = new Javac(b, cc);
try {
jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers()));
int var = jv.recordVariable(exceptionType, exceptionName);
b.addAstore(var);
jv.compileStmnt(src);
int stack = b.getMaxStack();
int locals = b.getMaxLocals();
if (stack > ca.getMaxStack())
ca.setMaxStack(stack);
if (locals > ca.getMaxLocals())
ca.setMaxLocals(locals);
int len = iterator.getCodeLength();
int pos = iterator.append(b.get());
ca.getExceptionTable().add(getStartPosOfBody(ca), len, len, cp.addClassInfo(exceptionType));
iterator.append(b.getExceptionTable(), pos);
methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class Reflection method start.
/**
* Initializes the object.
*/
@Override
public void start(ClassPool pool) throws NotFoundException {
classPool = pool;
final String msg = "javassist.tools.reflect.Sample is not found or broken.";
try {
CtClass c = classPool.get("javassist.tools.reflect.Sample");
rebuildClassFile(c.getClassFile());
trapMethod = c.getDeclaredMethod("trap");
trapStaticMethod = c.getDeclaredMethod("trapStatic");
trapRead = c.getDeclaredMethod("trapRead");
trapWrite = c.getDeclaredMethod("trapWrite");
readParam = new CtClass[] { classPool.get("java.lang.Object") };
} catch (NotFoundException e) {
throw new RuntimeException(msg);
} catch (BadBytecode e) {
throw new RuntimeException(msg);
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class FieldAccess method replace.
/**
* Replaces the method call with the bytecode derived from
* the given source text.
*
* <p>$0 is available even if the called method is static.
* If the field access is writing, $_ is available but the value
* of $_ is ignored.
*
* @param statement a Java statement except try-catch.
*/
@Override
public void replace(String statement) throws CannotCompileException {
// to call checkModify().
thisClass.getClassFile();
ConstPool constPool = getConstPool();
int pos = currentPos;
int index = iterator.u16bitAt(pos + 1);
Javac jc = new Javac(thisClass);
CodeAttribute ca = iterator.get();
try {
CtClass[] params;
CtClass retType;
CtClass fieldType = Descriptor.toCtClass(constPool.getFieldrefType(index), thisClass.getClassPool());
boolean read = isReader();
if (read) {
params = new CtClass[0];
retType = fieldType;
} else {
params = new CtClass[1];
params[0] = fieldType;
retType = CtClass.voidType;
}
int paramVar = ca.getMaxLocals();
jc.recordParams(constPool.getFieldrefClassName(index), params, true, paramVar, withinStatic());
/* Is $_ included in the source code?
*/
boolean included = checkResultValue(retType, statement);
if (read)
included = true;
int retVar = jc.recordReturnType(retType, included);
if (read)
jc.recordProceed(new ProceedForRead(retType, opcode, index, paramVar));
else {
// because $type is not the return type...
jc.recordType(fieldType);
jc.recordProceed(new ProceedForWrite(params[0], opcode, index, paramVar));
}
Bytecode bytecode = jc.getBytecode();
storeStack(params, isStatic(), paramVar, bytecode);
jc.recordLocalVariables(ca, pos);
if (included)
if (retType == CtClass.voidType) {
bytecode.addOpcode(ACONST_NULL);
bytecode.addAstore(retVar);
} else {
bytecode.addConstZero(retType);
// initialize $_
bytecode.addStore(retVar, retType);
}
jc.compileStmnt(statement);
if (read)
bytecode.addLoad(retVar, retType);
replace0(pos, bytecode, 3);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException("broken method");
}
}
Aggregations