private static void addGetter(String classname, ClassFile cf, ConstPool cp) throws CannotCompileException { MethodInfo minfo = new MethodInfo(cp, HANDLER_GETTER, HANDLER_GETTER_TYPE); minfo.setAccessFlags(AccessFlag.PUBLIC); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); code.addGetfield(classname, HANDLER, HANDLER_TYPE); code.addOpcode(Bytecode.ARETURN); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo); }
/** * Appends MULTINEWARRAY. * * @param desc the type descriptor of the created array. * @param dim dimensions. * @return the value of <code>dim</code>. */ public int addMultiNewarray(String desc, int dim) { add(MULTIANEWARRAY); addIndex(constPool.addClassInfo(desc)); add(dim); growStack(1 - dim); return dim; }
private static int addLoad(Bytecode code, int n, Class type) { if (type.isPrimitive()) { if (type == Long.TYPE) { code.addLload(n); return 2; } else if (type == Float.TYPE) code.addFload(n); else if (type == Double.TYPE) { code.addDload(n); return 2; } else code.addIload(n); } else code.addAload(n); return 1; }
final Bytecode code = new Bytecode( constPool, 4, 6 ); StackMapTable stackmap = null; code.addIconst( 0 ); code.addIstore( 3 ); code.addAload( 1 ); code.addCheckcast( this.targetBean.getName() ); code.addAstore( 4 ); start = code.currentPc(); int lastIndex = 0; for ( int i = 0; i < setters.length; ++i ) { code.addOpcode( Opcode.IINC ); code.add( 3 ); code.add( diff ); lastIndex = i; code.addAload( 4 ); code.addAload( 2 ); code.addIconst( i ); code.addOpcode( Opcode.AALOAD );
final Bytecode code = new Bytecode( constPool, 6, 4 ); code.addAload( 1 ); code.addCheckcast( this.targetBean.getName() ); code.addAstore( 3 ); for ( int i = 0; i < getters.length; ++i ) { if ( getters[i] != null ) { final Method getter = getters[i]; code.addAload( 2 ); code.addIconst( i ); final Class returnType = getter.getReturnType(); int typeIndex = -1; code.addNew( FactoryHelper.wrapperTypes[typeIndex] ); code.addOpcode( Opcode.DUP ); code.addAload( 3 ); final String getterSignature = RuntimeSupport.makeDescriptor( getter ); final String getterName = getter.getName(); if ( this.targetBean.isInterface() ) { code.addInvokeinterface( targetBeanConstPoolIndex, getterName, getterSignature, 1 ); code.addInvokevirtual( targetBeanConstPoolIndex, getterName, getterSignature );
bytecode.addDload(var); if (doDup && isPost) bytecode.addOpcode(DUP2); bytecode.addDconst(1.0); bytecode.addOpcode(token == PLUSPLUS ? DADD : DSUB); if (doDup && !isPost) bytecode.addOpcode(DUP2); bytecode.addDstore(var); bytecode.addLload(var); if (doDup && isPost) bytecode.addOpcode(DUP2); bytecode.addLconst((long)1); bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB); if (doDup && !isPost) bytecode.addOpcode(DUP2); bytecode.addLstore(var); bytecode.addFload(var); if (doDup && isPost) bytecode.addOpcode(DUP); bytecode.addFconst(1.0f); bytecode.addOpcode(token == PLUSPLUS ? FADD : FSUB); if (doDup && !isPost)
break; case ARETURN : b.addAstore(getVar(1)); jsrJmp(b); b.addAload(var); break; case IRETURN : b.addIstore(getVar(1)); jsrJmp(b); b.addIload(var); break; case LRETURN : b.addLstore(getVar(2)); jsrJmp(b); b.addLload(var); break; case DRETURN : b.addDstore(getVar(2)); jsrJmp(b); b.addDload(var); break; case FRETURN : b.addFstore(getVar(1)); jsrJmp(b); b.addFload(var); break; default :
Bytecode code = new Bytecode(cp, 0, 0); code.addAload(0); code.addGetstatic(thisClassName, DEFAULT_INTERCEPTOR, HANDLER_TYPE); code.addPutfield(thisClassName, HANDLER, HANDLER_TYPE); code.addGetstatic(thisClassName, DEFAULT_INTERCEPTOR, HANDLER_TYPE); code.addOpcode(Opcode.IFNONNULL); code.addIndex(10); code.addAload(0); code.addGetstatic(NULL_INTERCEPTOR_HOLDER, DEFAULT_INTERCEPTOR, HANDLER_TYPE); code.addPutfield(thisClassName, HANDLER, HANDLER_TYPE); int pc = code.currentPc(); code.addAload(0); int s = addLoadParameters(code, cons.getParameterTypes(), 1); code.addInvokespecial(superClass.getName(), "<init>", desc); code.addOpcode(Opcode.RETURN); code.setMaxLocals(s + 1); CodeAttribute ca = code.toCodeAttribute(); minfo.setCodeAttribute(ca);
public static int compileParameterList(Bytecode code, CtClass[] params, int regno) { if (params == null) { code.addIconst(0); // iconst_0 code.addAnewarray(javaLangObject); // anewarray Object return 1; CtClass[] args = new CtClass[1]; int n = params.length; code.addIconst(n); // iconst_<n> code.addAnewarray(javaLangObject); // anewarray Object for (int i = 0; i < n; ++i) { code.addOpcode(Bytecode.DUP); // dup code.addIconst(i); // iconst_<i> if (params[i].isPrimitive()) { CtPrimitiveType pt = (CtPrimitiveType)params[i]; String wrapper = pt.getWrapperName(); code.addNew(wrapper); // new <wrapper> code.addOpcode(Bytecode.DUP); // dup int s = code.addLoad(regno, pt); // ?load <regno> regno += s; args[0] = pt; code.addInvokespecial(wrapper, "<init>", Descriptor.ofMethod(CtClass.voidType, args)); code.addAload(regno); // aload <regno> ++regno; code.addOpcode(Bytecode.AASTORE); // aastore
private static void addSetter(String classname, ClassFile cf, ConstPool cp) throws CannotCompileException { MethodInfo minfo = new MethodInfo(cp, HANDLER_SETTER, HANDLER_SETTER_TYPE); minfo.setAccessFlags(AccessFlag.PUBLIC); Bytecode code = new Bytecode(cp, 2, 2); code.addAload(0); code.addAload(1); code.addPutfield(classname, HANDLER, HANDLER_TYPE); code.addOpcode(Bytecode.RETURN); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo); }
(ExceptionsAttribute)eattr.copy(cp, null)); Bytecode code = new Bytecode(cp, 0, 0); boolean isStatic = Modifier.isStatic(delegate.getModifiers()); CtClass deleClass = delegate.getDeclaringClass(); int s; if (isStatic) { s = code.addLoadParameters(params, 0); code.addInvokestatic(deleClass, methodName, desc); code.addLoad(0, deleClass); s = code.addLoadParameters(params, 1); code.addInvokespecial(deleClass, methodName, desc); code.addReturn(delegate.getReturnType()); code.setMaxLocals(++s); code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value minfo.setCodeAttribute(code.toCodeAttribute());
/** * Produces codes in which a new object is created and assigned to * the field as the initial value. */ int compile(CtClass type, String name, Bytecode code, CtClass[] parameters, Javac drv) throws CannotCompileException { int stacksize; code.addAload(0); code.addNew(objectType); code.add(Bytecode.DUP); code.addAload(0); if (stringParams == null) stacksize = 4; else stacksize = compileStringParameter(code) + 4; if (withConstructorParams) stacksize += CtNewWrappedMethod.compileParameterList(code, parameters, 1); code.addInvokespecial(objectType, "<init>", getDescriptor()); code.addPutfield(Bytecode.THIS, name, Descriptor.of(type)); return stacksize; }
/** * Declares a constructor that takes no parameter. * * @param classfile The class descriptor * * @throws CannotCompileException Indicates trouble with the underlying Javassist calls */ private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException { final ConstPool constPool = classfile.getConstPool(); final String constructorSignature = "()V"; final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature ); final Bytecode code = new Bytecode( constPool, 0, 1 ); // aload_0 code.addAload( 0 ); // invokespecial code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature ); // return code.addOpcode( Opcode.RETURN ); constructorMethodInfo.setCodeAttribute( code.toCodeAttribute() ); constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC ); classfile.addMethod( constructorMethodInfo ); }
private static MethodInfo makeWriteReplace(ConstPool cp) { MethodInfo minfo = new MethodInfo(cp, "writeReplace", "()Ljava/lang/Object;"); String[] list = new String[1]; list[0] = "java.io.ObjectStreamException"; ExceptionsAttribute ea = new ExceptionsAttribute(cp); ea.setExceptions(list); minfo.setExceptionsAttribute(ea); Bytecode code = new Bytecode(cp, 0, 1); code.addAload(0); code.addInvokestatic("javassist.util.proxy.RuntimeSupport", "makeSerializedProxy", "(Ljava/lang/Object;)Ljavassist/util/proxy/SerializedProxy;"); code.addOpcode(Opcode.ARETURN); minfo.setCodeAttribute(code.toCodeAttribute()); return minfo; } }
/** * Creates a default (public) constructor. * * <p>The created constructor takes no parameter. It calls * <code>super()</code>. */ public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException { CtConstructor cons = new CtConstructor((CtClass[])null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); try { code.addInvokespecial(declaring.getSuperclass(), "<init>", "()V"); } catch (NotFoundException e) { throw new CannotCompileException(e); } code.add(Bytecode.RETURN); // no need to construct a stack map table. cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); return cons; }
protected void makeCode(CtClass[] paramTypes, ConstPool cp) { Bytecode save = new Bytecode(cp, 0, 0); Bytecode load = new Bytecode(cp, 0, 0); int var = maxLocals; int len = (paramTypes == null) ? 0 : paramTypes.length; load.addAload(var); makeCode2(save, load, 0, len, paramTypes, var + 1); save.addAstore(var); saveCode = save.get(); loadCode = load.get(); }
private MethodInfo makeDelegator(Method meth, String desc, ConstPool cp, Class declClass, String delegatorName) { MethodInfo delegator = new MethodInfo(cp, delegatorName, desc); delegator.setAccessFlags(Modifier.FINAL | Modifier.PUBLIC | (meth.getModifiers() & ~(Modifier.PRIVATE | Modifier.PROTECTED | Modifier.ABSTRACT | Modifier.NATIVE | Modifier.SYNCHRONIZED))); setThrows(delegator, cp, meth); Bytecode code = new Bytecode(cp, 0, 0); code.addAload(0); int s = addLoadParameters(code, meth.getParameterTypes(), 1); Class targetClass = invokespecialTarget(declClass); code.addInvokespecial(targetClass.isInterface(), cp.addClassInfo(targetClass.getName()), meth.getName(), desc); addReturn(code, meth.getReturnType()); code.setMaxLocals(++s); delegator.setCodeAttribute(code.toCodeAttribute()); return delegator; }