javassist.bytecode.Bytecode.toCodeAttribute()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(157)

本文整理了Java中javassist.bytecode.Bytecode.toCodeAttribute()方法的一些代码示例,展示了Bytecode.toCodeAttribute()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytecode.toCodeAttribute()方法的具体详情如下:
包路径:javassist.bytecode.Bytecode
类名称:Bytecode
方法名:toCodeAttribute

Bytecode.toCodeAttribute介绍

[英]Converts to a CodeAttribute.
[中]转换为CodeAttribute

代码示例

代码示例来源:origin: redisson/redisson

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;
}

代码示例来源:origin: redisson/redisson

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);
}

代码示例来源:origin: redisson/redisson

public static CtConstructor wrapped(CtClass[] parameterTypes,
                  CtClass[] exceptionTypes,
                  int howToCallSuper,
                  CtMethod body,
                  ConstParameter constParam,
                  CtClass declaring)
  throws CannotCompileException
{
  try {
    CtConstructor cons = new CtConstructor(parameterTypes, declaring);
    cons.setExceptionTypes(exceptionTypes);
    Bytecode code = makeBody(declaring, declaring.getClassFile2(),
                 howToCallSuper, body,
                 parameterTypes, constParam);
    cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute());
    // a stack map table is not needed.
    return cons;
  }
  catch (NotFoundException e) {
    throw new CannotCompileException(e);
  }
}

代码示例来源:origin: redisson/redisson

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);
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * 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 );
}

代码示例来源:origin: redisson/redisson

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;
  }
}

代码示例来源:origin: org.javassist/javassist

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);
}

代码示例来源:origin: redisson/redisson

public static CtMethod wrapped(CtClass returnType, String mname,
                CtClass[] parameterTypes,
                CtClass[] exceptionTypes,
                CtMethod body, ConstParameter constParam,
                CtClass declaring)
  throws CannotCompileException
{
  CtMethod mt = new CtMethod(returnType, mname, parameterTypes,
                declaring);
  mt.setModifiers(body.getModifiers());
  try {
    mt.setExceptionTypes(exceptionTypes);
  }
  catch (NotFoundException e) {
    throw new CannotCompileException(e);
  }
  Bytecode code = makeBody(declaring, declaring.getClassFile2(), body,
               parameterTypes, returnType, constParam);
  MethodInfo minfo = mt.getMethodInfo2();
  minfo.setCodeAttribute(code.toCodeAttribute());
  // a stack map has been already created. 
  return mt;
}

代码示例来源:origin: org.javassist/javassist

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);
}

代码示例来源:origin: org.javassist/javassist

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;
  }
}

代码示例来源:origin: redisson/redisson

code.addPutstatic(classname, SERIAL_VERSION_UID_FIELD, SERIAL_VERSION_UID_TYPE);
code.addOpcode(Bytecode.RETURN);
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);

代码示例来源:origin: redisson/redisson

/**
 * 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;
}

代码示例来源:origin: redisson/redisson

params, retType,
                      constParam);
CodeAttribute cattr = code.toCodeAttribute();
methodInfo.setCodeAttribute(cattr);
methodInfo.setAccessFlags(methodInfo.getAccessFlags()

代码示例来源:origin: redisson/redisson

code.addOpcode(Opcode.RETURN);
code.setMaxLocals(s + 1);
CodeAttribute ca = code.toCodeAttribute();
minfo.setCodeAttribute(ca);

代码示例来源:origin: opentripplanner/OpenTripPlanner

private void addDoubleSetter(ClassFile classFile, String fieldName)
      throws DuplicateMemberException {
    ConstPool constPool = classFile.getConstPool();

    // void setFoo(double)
    MethodInfo setter = new MethodInfo(constPool, "set" + ucfirst(fieldName), "(D)V");
    Bytecode code = new Bytecode(constPool, 3, 3);

    // load this
    code.addAload(0);

    // load param
    code.addDload(1);
    code.addPutfield(ctClass, fieldName, "D");

    code.addOpcode(Opcode.RETURN);

    setter.setCodeAttribute(code.toCodeAttribute());
    setter.setAccessFlags(AccessFlag.PUBLIC);
    classFile.addMethod(setter);

  }
}

代码示例来源:origin: redisson/redisson

minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);

代码示例来源:origin: redisson/redisson

methodInfo.setCodeAttribute(b.toCodeAttribute());
methodInfo.setAccessFlags(methodInfo.getAccessFlags()
             & ~AccessFlag.ABSTRACT);

代码示例来源:origin: redisson/redisson

minfo.setCodeAttribute(code.toCodeAttribute());
CtClass cc = field.getDeclaringClass();

代码示例来源:origin: redisson/redisson

code.setMaxLocals(++s);
code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
minfo.setCodeAttribute(code.toCodeAttribute());

代码示例来源:origin: redisson/redisson

m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
m.setAccessFlags(AccessFlag.STATIC);
m.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(m);
CtMember.Cache cache = hasMemberCache();

相关文章