Package javassist.bytecode

Examples of javassist.bytecode.MethodInfo


  private void addSetFieldHandlerMethod(ClassFile classfile)
      throws CannotCompileException {
    ConstPool cp = classfile.getConstPool();
    int this_class_index = cp.getThisClassInfo();
    MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME,
                                      SETFIELDHANDLER_METHOD_DESCRIPTOR);
    /* local variables | this | callback | */
    Bytecode code = new Bytecode(cp, 3, 3);
    // aload_0 // load this
    code.addAload(0);
    // aload_1 // load callback
    code.addAload(1);
    // putfield // put field "$JAVASSIST_CALLBACK" defined already
    code.addOpcode(Opcode.PUTFIELD);
    int field_index = cp.addFieldrefInfo(this_class_index,
                                         HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR);
    code.addIndex(field_index);
    // return
    code.addOpcode(Opcode.RETURN);
    minfo.setCodeAttribute(code.toCodeAttribute());
    minfo.setAccessFlags(AccessFlag.PUBLIC);
    classfile.addMethod(minfo);
  }
View Full Code Here


  private void addReadMethod(ClassFile classfile, FieldInfo finfo)
      throws CannotCompileException {
    ConstPool cp = classfile.getConstPool();
    int this_class_index = cp.getThisClassInfo();
    String desc = "()" + finfo.getDescriptor();
    MethodInfo minfo = new MethodInfo(cp, EACH_READ_METHOD_PREFIX
                                          + finfo.getName(), desc);
    /* local variables | target obj | each oldvalue | */
    Bytecode code = new Bytecode(cp, 5, 3);
    // aload_0
    code.addAload(0);
    // getfield // get each field
    code.addOpcode(Opcode.GETFIELD);
    int base_field_index = cp.addFieldrefInfo(this_class_index, finfo
        .getName(), finfo.getDescriptor());
    code.addIndex(base_field_index);
    // aload_0
    code.addAload(0);
    // invokeinterface // invoke Enabled.getInterceptFieldCallback()
    int enabled_class_index = cp.addClassInfo(FIELD_HANDLED_TYPE_NAME);
    code.addInvokeinterface(enabled_class_index,
                            GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
                            1);
    // ifnonnull
    code.addOpcode(Opcode.IFNONNULL);
    code.addIndex(4);
    // *return // each type
    addTypeDependDataReturn(code, finfo.getDescriptor());
    // *store_1 // each type
    addTypeDependDataStore(code, finfo.getDescriptor(), 1);
    // aload_0
    code.addAload(0);
    // invokeinterface // invoke Enabled.getInterceptFieldCallback()
    code.addInvokeinterface(enabled_class_index,
                            GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
                            1);
    // aload_0
    code.addAload(0);
    // ldc // name of the field
    code.addLdc(finfo.getName());
    // *load_1 // each type
    addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
    // invokeinterface // invoke Callback.read*() // each type
    addInvokeFieldHandlerMethod(classfile, code, finfo.getDescriptor(),
                                true);
    // *return // each type
    addTypeDependDataReturn(code, finfo.getDescriptor());

    minfo.setCodeAttribute(code.toCodeAttribute());
    minfo.setAccessFlags(AccessFlag.PUBLIC);
    classfile.addMethod(minfo);
  }
View Full Code Here

  private void addWriteMethod(ClassFile classfile, FieldInfo finfo)
      throws CannotCompileException {
    ConstPool cp = classfile.getConstPool();
    int this_class_index = cp.getThisClassInfo();
    String desc = "(" + finfo.getDescriptor() + ")V";
    MethodInfo minfo = new MethodInfo(cp, EACH_WRITE_METHOD_PREFIX
                                          + finfo.getName(), desc);
    /* local variables | target obj | each oldvalue | */
    Bytecode code = new Bytecode(cp, 6, 3);
    // aload_0
    code.addAload(0);
    // invokeinterface // enabled.getInterceptFieldCallback()
    int enabled_class_index = cp.addClassInfo(FIELD_HANDLED_TYPE_NAME);
    code.addInvokeinterface(enabled_class_index,
                            GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
                            1);
    // ifnonnull (label1)
    code.addOpcode(Opcode.IFNONNULL);
    code.addIndex(9);
    // aload_0
    code.addAload(0);
    // *load_1
    addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
    // putfield
    code.addOpcode(Opcode.PUTFIELD);
    int base_field_index = cp.addFieldrefInfo(this_class_index, finfo
        .getName(), finfo.getDescriptor());
    code.addIndex(base_field_index);
    code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
    // return ;
    code.addOpcode(Opcode.RETURN);
    // aload_0
    code.addAload(0);
    // dup
    code.addOpcode(Opcode.DUP);
    // invokeinterface // enabled.getInterceptFieldCallback()
    code.addInvokeinterface(enabled_class_index,
                            GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
                            1);
    // aload_0
    code.addAload(0);
    // ldc // field name
    code.addLdc(finfo.getName());
    // aload_0
    code.addAload(0);
    // getfield // old value of the field
    code.addOpcode(Opcode.GETFIELD);
    code.addIndex(base_field_index);
    code.growStack(Descriptor.dataSize(finfo.getDescriptor()) - 1);
    // *load_1
    addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
    // invokeinterface // callback.write*(..)
    addInvokeFieldHandlerMethod(classfile, code, finfo.getDescriptor(),
                                false);
    // putfield // new value of the field
    code.addOpcode(Opcode.PUTFIELD);
    code.addIndex(base_field_index);
    code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
    // return
    code.addOpcode(Opcode.RETURN);

    minfo.setCodeAttribute(code.toCodeAttribute());
    minfo.setAccessFlags(AccessFlag.PUBLIC);
    classfile.addMethod(minfo);
  }
View Full Code Here

  private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile)
      throws CannotCompileException {
    List methods = classfile.getMethods();
    for (Iterator method_iter = methods.iterator(); method_iter.hasNext();) {
      MethodInfo minfo = (MethodInfo) method_iter.next();
      String methodName = minfo.getName();
      if (methodName.startsWith(EACH_READ_METHOD_PREFIX)
          || methodName.startsWith(EACH_WRITE_METHOD_PREFIX)
          || methodName.equals(GETFIELDHANDLER_METHOD_NAME)
          || methodName.equals(SETFIELDHANDLER_METHOD_NAME)) {
        continue;
      }
      CodeAttribute codeAttr = minfo.getCodeAttribute();
      if (codeAttr == null) {
        continue;
      }
      CodeIterator iter = codeAttr.iterator();
      while (iter.hasNext()) {
View Full Code Here

        String classname = annotation.getTypeName();
        if (pool != null) {
            try {
                CtClass cc = pool.get(classname);
                ClassFile cf = cc.getClassFile2();
                MethodInfo minfo = cf.getMethod(name);
                if (minfo != null) {
                    AnnotationDefaultAttribute ainfo
                        = (AnnotationDefaultAttribute)
                          minfo.getAttribute(AnnotationDefaultAttribute.tag);
                    if (ainfo != null) {
                        MemberValue mv = ainfo.getDefaultValue();
                        return mv.getValue(classLoader, pool, method);
                    }
                }
View Full Code Here

            return;
        }

        for (Object obj : methods)
        {
            MethodInfo method = (MethodInfo) obj;
            if (scanMethodAnnotations)
            {
                AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
                AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);

                if (visible != null)
                {
                    populate(visible.getAnnotations(), cf.getName());
                }
                if (invisible != null)
                {
                    populate(invisible.getAnnotations(), cf.getName());
                }
            }
            if (scanParameterAnnotations)
            {
                ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
                ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);

                if (paramsVisible != null && paramsVisible.getAnnotations() != null)
                {
                    for (Annotation[] anns : paramsVisible.getAnnotations())
                    {
View Full Code Here

    {
      return;
    }
    for (Object obj : methods)
    {
      MethodInfo method = (MethodInfo) obj;
      if (scanMethodAnnotations)
      {
        AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
        AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);

        if (visible != null)
        {
          populate(visible.getAnnotations(), cf.getName());
        }
        if (invisible != null)
        {
          populate(invisible.getAnnotations(), cf.getName());
        }
      }
      if (scanParameterAnnotations)
      {
        ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
        ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);

        if (paramsVisible != null && paramsVisible.getAnnotations() != null)
        {
          for (Annotation[] anns : paramsVisible.getAnnotations())
          {
View Full Code Here

    private void makeBehaviorCache(CtMember.Cache cache) {
        List list = getClassFile2().getMethods();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            MethodInfo minfo = (MethodInfo)list.get(i);
            if (minfo.isMethod()) {
                CtMethod newMethod = new CtMethod(minfo, this);
                cache.addMethod(newMethod);
            }
            else {
                CtConstructor newCons = new CtConstructor(minfo, this);
View Full Code Here

        getClassFile2().addMethod(c.getMethodInfo2());
    }

    public void removeConstructor(CtConstructor m) throws NotFoundException {
        checkModify();
        MethodInfo mi = m.getMethodInfo2();
        ClassFile cf = getClassFile2();
        if (cf.getMethods().remove(mi)) {
            getMembers().remove(m);
            gcConstPool = true;
        }
View Full Code Here

            setModifiers(getModifiers() | Modifier.ABSTRACT);
    }

    public void removeMethod(CtMethod m) throws NotFoundException {
        checkModify();
        MethodInfo mi = m.getMethodInfo2();
        ClassFile cf = getClassFile2();
        if (cf.getMethods().remove(mi)) {
            getMembers().remove(m);
            gcConstPool = true;
        }
View Full Code Here

TOP

Related Classes of javassist.bytecode.MethodInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.