use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class NewArray method replace2.
private void replace2(String statement) throws CompileError, NotFoundException, BadBytecode, CannotCompileException {
// to call checkModify().
thisClass.getClassFile();
ConstPool constPool = getConstPool();
int pos = currentPos;
CtClass retType;
int codeLength;
int index = 0;
int dim = 1;
String desc;
if (opcode == Opcode.NEWARRAY) {
// atype
index = iterator.byteAt(currentPos + 1);
CtPrimitiveType cpt = (CtPrimitiveType) getPrimitiveType(index);
desc = "[" + cpt.getDescriptor();
codeLength = 2;
} else if (opcode == Opcode.ANEWARRAY) {
index = iterator.u16bitAt(pos + 1);
desc = constPool.getClassInfo(index);
if (desc.startsWith("["))
desc = "[" + desc;
else
desc = "[L" + desc + ";";
codeLength = 3;
} else if (opcode == Opcode.MULTIANEWARRAY) {
index = iterator.u16bitAt(currentPos + 1);
desc = constPool.getClassInfo(index);
dim = iterator.byteAt(currentPos + 3);
codeLength = 4;
} else
throw new RuntimeException("bad opcode: " + opcode);
retType = Descriptor.toCtClass(desc, thisClass.getClassPool());
Javac jc = new Javac(thisClass);
CodeAttribute ca = iterator.get();
CtClass[] params = new CtClass[dim];
for (int i = 0; i < dim; ++i) params[i] = CtClass.intType;
int paramVar = ca.getMaxLocals();
jc.recordParams(javaLangObject, params, true, paramVar, withinStatic());
/* Is $_ included in the source code?
*/
checkResultValue(retType, statement);
int retVar = jc.recordReturnType(retType, true);
jc.recordProceed(new ProceedForArray(retType, opcode, index, dim));
Bytecode bytecode = jc.getBytecode();
storeStack(params, true, paramVar, bytecode);
jc.recordLocalVariables(ca, pos);
// initialize $_
bytecode.addOpcode(ACONST_NULL);
bytecode.addAstore(retVar);
jc.compileStmnt(statement);
bytecode.addAload(retVar);
replace0(pos, bytecode, codeLength);
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class ExprEditor method doit.
/**
* Undocumented method. Do not use; internal-use only.
*/
public boolean doit(CtClass clazz, MethodInfo minfo) throws CannotCompileException {
CodeAttribute codeAttr = minfo.getCodeAttribute();
if (codeAttr == null)
return false;
CodeIterator iterator = codeAttr.iterator();
boolean edited = false;
LoopContext context = new LoopContext(codeAttr.getMaxLocals());
while (iterator.hasNext()) if (loopBody(iterator, clazz, minfo, context))
edited = true;
ExceptionTable et = codeAttr.getExceptionTable();
int n = et.size();
for (int i = 0; i < n; ++i) {
Handler h = new Handler(et, i, iterator, clazz, minfo);
edit(h);
if (h.edited()) {
edited = true;
context.updateMax(h.locals(), h.stack());
}
}
// so I check the current value of max-locals.
if (codeAttr.getMaxLocals() < context.maxLocals)
codeAttr.setMaxLocals(context.maxLocals);
codeAttr.setMaxStack(codeAttr.getMaxStack() + context.maxStack);
try {
if (edited)
minfo.rebuildStackMapIf6(clazz.getClassPool(), clazz.getClassFile2());
} catch (BadBytecode b) {
throw new CannotCompileException(b.getMessage(), b);
}
return edited;
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class NewExpr method replace.
/**
* Replaces the <code>new</code> expression with the bytecode derived from
* the given source text.
*
* <p>$0 is available but the value is null.
*
* @param statement a Java statement except try-catch.
*/
@Override
public void replace(String statement) throws CannotCompileException {
// to call checkModify().
thisClass.getClassFile();
final int bytecodeSize = 3;
int pos = newPos;
int newIndex = iterator.u16bitAt(pos + 1);
/* delete the preceding NEW and DUP (or DUP_X1, SWAP) instructions.
*/
int codeSize = canReplace();
int end = pos + codeSize;
for (int i = pos; i < end; ++i) iterator.writeByte(NOP, i);
ConstPool constPool = getConstPool();
pos = currentPos;
// constructor
int methodIndex = iterator.u16bitAt(pos + 1);
String signature = constPool.getMethodrefType(methodIndex);
Javac jc = new Javac(thisClass);
ClassPool cp = thisClass.getClassPool();
CodeAttribute ca = iterator.get();
try {
CtClass[] params = Descriptor.getParameterTypes(signature, cp);
CtClass newType = cp.get(newTypeName);
int paramVar = ca.getMaxLocals();
jc.recordParams(newTypeName, params, true, paramVar, withinStatic());
int retVar = jc.recordReturnType(newType, true);
jc.recordProceed(new ProceedForNew(newType, newIndex, methodIndex));
/* Is $_ included in the source code?
*/
checkResultValue(newType, statement);
Bytecode bytecode = jc.getBytecode();
storeStack(params, true, paramVar, bytecode);
jc.recordLocalVariables(ca, pos);
bytecode.addConstZero(newType);
// initialize $_
bytecode.addStore(retVar, newType);
jc.compileStmnt(statement);
if (// if the original code includes DUP.
codeSize > 3)
bytecode.addAload(retVar);
replace0(pos, bytecode, bytecodeSize);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException("broken method");
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class Cast method replace.
/**
* Replaces the explicit cast operator with the bytecode derived from
* the given source text.
*
* <p>$0 is available but the value is <code>null</code>.
*
* @param statement a Java statement except try-catch.
*/
@Override
public void replace(String statement) throws CannotCompileException {
// to call checkModify().
thisClass.getClassFile();
@SuppressWarnings("unused") ConstPool constPool = getConstPool();
int pos = currentPos;
int index = iterator.u16bitAt(pos + 1);
Javac jc = new Javac(thisClass);
ClassPool cp = thisClass.getClassPool();
CodeAttribute ca = iterator.get();
try {
CtClass[] params = new CtClass[] { cp.get(javaLangObject) };
CtClass retType = getType();
int paramVar = ca.getMaxLocals();
jc.recordParams(javaLangObject, params, true, paramVar, withinStatic());
int retVar = jc.recordReturnType(retType, true);
jc.recordProceed(new ProceedForCast(index, retType));
/* Is $_ included in the source code?
*/
checkResultValue(retType, statement);
Bytecode bytecode = jc.getBytecode();
storeStack(params, true, paramVar, bytecode);
jc.recordLocalVariables(ca, pos);
bytecode.addConstZero(retType);
// initialize $_
bytecode.addStore(retVar, retType);
jc.compileStmnt(statement);
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");
}
}
use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.
the class Javac method compile.
/**
* Compiles a method, constructor, or field declaration
* to a class.
* A field declaration can declare only one field.
*
* <p>In a method or constructor body, $0, $1, ... and $_
* are not available.
*
* @return a <code>CtMethod</code>, <code>CtConstructor</code>,
* or <code>CtField</code> object.
* @see #recordProceed(String,String)
*/
public CtMember compile(String src) throws CompileError {
Parser p = new Parser(new Lex(src));
ASTList mem = p.parseMember1(stable);
try {
if (mem instanceof FieldDecl)
return compileField((FieldDecl) mem);
CtBehavior cb = compileMethod(p, (MethodDecl) mem);
CtClass decl = cb.getDeclaringClass();
cb.getMethodInfo2().rebuildStackMapIf6(decl.getClassPool(), decl.getClassFile2());
return cb;
} catch (BadBytecode bb) {
throw new CompileError(bb.getMessage());
} catch (CannotCompileException e) {
throw new CompileError(e.getMessage());
}
}
Aggregations