private static CtMethod delegator0(CtMethod delegate, CtClass declaring) throws CannotCompileException, NotFoundException { MethodInfo deleInfo = delegate.getMethodInfo2(); String methodName = deleInfo.getName(); String desc = deleInfo.getDescriptor(); ConstPool cp = declaring.getClassFile2().getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(deleInfo.getAccessFlags()); ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute(); if (eattr != null) minfo.setExceptionsAttribute((ExceptionsAttribute) eattr.copy(cp, null)); Bytecode code = new Bytecode(cp, 0, 0); boolean isStatic = Modifier.isStatic(delegate.getModifiers()); CtClass deleClass = delegate.getDeclaringClass(); CtClass[] params = delegate.getParameterTypes(); int s; if (isStatic) { s = code.addLoadParameters(params, 0); code.addInvokestatic(deleClass, methodName, desc); } else { 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()); return new CtMethod(minfo, declaring); }
/** * Client proxies are equal to other client proxies for the same bean. * * <p>The corresponding java code: <code> * return other instanceof MyProxyClassType.class * </code> */ @Override protected MethodInfo generateEqualsMethod(ClassFile proxyClassType) { MethodInfo method = new MethodInfo(proxyClassType.getConstPool(), "equals", "(Ljava/lang/Object;)Z"); method.setAccessFlags(AccessFlag.PUBLIC); Bytecode b = new Bytecode(proxyClassType.getConstPool()); b.addAload(1); b.addInstanceof(proxyClassType.getName()); b.add(Opcode.IRETURN); b.setMaxLocals(2); b.setMaxStack(1); method.setCodeAttribute(b.toCodeAttribute()); return method; }
/** Client proxies use the following hashCode: <code>MyProxyName.class.hashCode()</code> */ @Override protected MethodInfo generateHashCodeMethod(ClassFile proxyClassType) { MethodInfo method = new MethodInfo(proxyClassType.getConstPool(), "hashCode", "()I"); method.setAccessFlags(AccessFlag.PUBLIC); Bytecode b = new Bytecode(proxyClassType.getConstPool()); // MyProxyName.class.hashCode() int classLocation = proxyClassType.getConstPool().addClassInfo(proxyClassType.getName()); b.addLdc(classLocation); // now we have the class object on top of the stack b.addInvokevirtual("java.lang.Object", "hashCode", "()I"); // now we have the hashCode b.add(Opcode.IRETURN); b.setMaxLocals(1); b.setMaxStack(1); method.setCodeAttribute(b.toCodeAttribute()); return method; }