Package cn.bran.japid.compiler

Examples of cn.bran.japid.compiler.JavaSyntaxValidatorTest$MethodVisitor


      return null;
    }

    // destination constructors [A1]
    if (name.equals(INIT) == true) {
      MethodVisitor mv = wd.dest.visitMethod(access, name, desc, msign.getRawSignature(), null);
      return new ProxettaCtorBuilder(mv, msign, wd);
    }
    // ignore destination static block
    if (name.equals(CLINIT) == true) {
      return null;
View Full Code Here


   * Creates static initialization block that simply calls all
   * advice static init methods in correct order.
   */
  protected void makeStaticInitBlock() {
    if (wd.adviceClinits != null) {
      MethodVisitor mv = wd.dest.visitMethod(AsmUtil.ACC_STATIC, CLINIT, DESC_VOID, null, null);
      mv.visitCode();
      for (String name : wd.adviceClinits) {
        mv.visitMethodInsn(INVOKESTATIC, wd.thisReference, name, DESC_VOID);
      }
      mv.visitInsn(RETURN);
      mv.visitMaxs(0, 0);
      mv.visitEnd();
    }
  }
View Full Code Here

  /**
   * Creates init method that simply calls all advice constructor methods in correct order.
   * This created init method is called from each destination's constructor.
   */
  protected void makeProxyConstructor() {
    MethodVisitor mv = wd.dest.visitMethod(AsmUtil.ACC_PRIVATE | AsmUtil.ACC_FINAL, initMethodName, DESC_VOID, null, null);
    mv.visitCode();
    if (wd.adviceInits != null) {
      for (String name : wd.adviceInits) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, wd.thisReference, name, DESC_VOID);
      }
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }
View Full Code Here

    access &= ~ACC_NATIVE;
    access &= ~ACC_ABSTRACT;
    access = ProxettaAsmUtil.makePrivateFinalAccess(access);

    final MethodVisitor mv = wd.dest.visitMethod(access, td.methodName(), td.msign.getDescription(), null, null);
    mv.visitCode();

    //*** VISIT ADVICE - called for each aspect and each method
    aspectData.getAdviceClassReader().accept(new EmptyClassVisitor() {

      @Override
      public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {

        if (name.equals(executeMethodName) == false) {
          return null;
        }

        return new IntArgHistoryMethodAdapter(mv) {

          @Override
          public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            if (owner.equals(aspectData.adviceReference)) {
              owner = wd.thisReference;              // [F5]
              name = adviceFieldName(name, aspectData.aspectIndex);
            }
            super.visitFieldInsn(opcode, owner, name, desc);
          }


          @Override
          public void visitVarInsn(int opcode, int var) {
            var += (var == 0 ? 0 : td.msign.getAllArgumentsSize());
            super.visitVarInsn(opcode, var);   // [F1]
          }

          @Override
          public void visitIincInsn(int var, int increment) {
            var += (var == 0 ? 0 : td.msign.getAllArgumentsSize());
            super.visitIincInsn(var, increment)// [F1]
          }

          @Override
          public void visitInsn(int opcode) {
            if (opcode == ARETURN) {
              visitReturn(mv, td.msign, true);
              return;
            }
            if (traceNext == true) {
              if ((opcode == POP) || (opcode == POP2)) {      // [F3] - invoke invoked without assignment
                return;
              }
            }
            super.visitInsn(opcode);
          }

          @SuppressWarnings({"ParameterNameDiffersFromOverriddenParameter"})
          @Override
          public void visitMethodInsn(int opcode, String string, String mname, String mdesc) {
            if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE) || (opcode == INVOKESPECIAL)) {
              if (string.equals(aspectData.adviceReference)) {
                string = wd.thisReference;
                mname = adviceMethodName(mname, aspectData.aspectIndex);
              }
            } else

            if (opcode == INVOKESTATIC) {
              if (string.equals(aspectData.adviceReference)) {
                string = wd.thisReference;
                mname = adviceMethodName(mname, aspectData.aspectIndex);
              } else

              if (string.endsWith('/' + TARGET_CLASS_NAME) == true) {

                if (isInvokeMethod(mname, mdesc)) {           // [R7]
                  if (td.isLastMethodInChain()) {                            // last proxy method just calls super target method

                    if (wd.isWrapper() == false) {
                      // PROXY
                      loadSpecialMethodArguments(mv, td.msign);
                      mv.visitMethodInsn(INVOKESPECIAL, wd.superReference, td.msign.getMethodName(), td.msign.getDescription());
                    } else {
                      // WRAPPER
                      mv.visitVarInsn(ALOAD, 0);
                      mv.visitFieldInsn(GETFIELD, wd.thisReference, wd.wrapperRef, wd.wrapperType);
                      loadVirtualMethodArguments(mv, td.msign);
                      if (wd.wrapInterface) {
                        mv.visitMethodInsn(INVOKEINTERFACE, wd.wrapperType.substring(1, wd.wrapperType.length() - 1), td.msign.getMethodName(), td.msign.getDescription());
                      } else {
                        mv.visitMethodInsn(INVOKEVIRTUAL, wd.wrapperType.substring(1, wd.wrapperType.length() - 1), td.msign.getMethodName(), td.msign.getDescription());
                      }
                    }

                    prepareReturnValue(mv, td.msign, aspectData.maxLocalVarOffset);     // [F4]
                    traceNext = true;
                  } else {                                                    // calls next proxy method
                    loadSpecialMethodArguments(mv, td.msign);
                    mv.visitMethodInsn(INVOKESPECIAL, wd.thisReference, td.nextMethodName(), td.msign.getDescription());
                    visitReturn(mv, td.msign, false);
                  }
                  return;
                } else

                if (isArgumentsCountMethod(mname, mdesc)) {    // [R2]
                  ProxyTargetReplacement.argumentsCount(mv, td.msign);
                  return;
                } else

                if (isArgumentTypeMethod(mname, mdesc)) {      // [R3]
                  int argIndex = this.getArgumentIndex();
                  ProxyTargetReplacement.argumentType(mv, td.msign, argIndex);
                  return;
                } else

                if (isArgumentMethod(mname, mdesc)) {           // [R4]
                  int argIndex = this.getArgumentIndex();
                  ProxyTargetReplacement.argument(mv, td.msign, argIndex);
                  return;
                } else

                if (isSetArgumentMethod(mname, mdesc)) {           // [R5]
                  int argIndex = this.getArgumentIndex();
                  checkArgumentIndex(td.msign, argIndex);
                  mv.visitInsn(POP);
                  storeMethodArgumentFromObject(mv, td.msign, argIndex);
                  return;
                } else

                if (isCreateArgumentsArrayMethod(mname, mdesc)) {  // [R6]
                  ProxyTargetReplacement.createArgumentsArray(mv, td.msign);
                  return;
                } else

                if (isCreateArgumentsClassArrayMethod(mname, mdesc)) {     // [R11]
                  ProxyTargetReplacement.createArgumentsClassArray(mv, td.msign);
                  return;
                } else

                if (isTargetMethod(mname, mdesc)) {       // [R9.1]
                  mv.visitVarInsn(ALOAD, 0);
                  return;
                } else

                if (isTargetClassMethod(mname, mdesc)) {       // [R9]
                  ProxyTargetReplacement.targetClass(mv, td.msign);
View Full Code Here

        }

        @Override
        public MethodVisitor visitMethod(final int access, final String name, final String desc,
            final String signature, final String[] exceptions) {
            final MethodVisitor toWrap = wrapped.visitMethod(access, name, desc, signature, exceptions);
            final ClassInfo classInfo = (ClassInfo) wrapped.getInfo();

            // MethodInfo may not always come from a descriptor, so we must go by the
            // Member represented. Make sure the method either has a valid name or is a constructor:
            final MethodInfo compareMethodInfo = new MethodInfo(classInfo, name, desc);
            if (!compareMethodInfo.isConstructor() && !isJavaIdentifier(name)) {
                return toWrap;
            }
            MethodInfo testMethodInfo = null;
            final Member member;
            try {
                member = compareMethodInfo.get();
                // should be the most recently added method, so iterate backward:
                for (int i = classInfo.getMethods().size() - 1; i >= 0; i--) {
                    final MethodInfo atI = classInfo.getMethods().get(i);
                    if (atI.getName().equals(name) && atI.get().equals(member)) {
                        testMethodInfo = atI;
                        break;
                    }
                }
            } catch (final ClassNotFoundException e) {
                return toWrap;
            }
            if (testMethodInfo == null) {
                return toWrap;
            }
            final MethodInfo methodInfo = testMethodInfo;
            return new MethodVisitor(Opcodes.ASM4, toWrap) {
                @Override
                public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
                    final AnnotationVisitor toWrap = super.visitAnnotation(desc, visible);
                    return visible ? toWrap : new TopLevelAnnotationInflater(desc, toWrap, methodInfo);
                }
View Full Code Here

    @Override
    protected void createSerialisation(ClassWriter cw, String proxyClassFileName, Class<?> classToProxy, String classFileName)
    {
        String[] exceptionTypeNames = {Type.getType(ObjectStreamException.class).getInternalName()};
        MethodVisitor mv = cw.visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, exceptionTypeNames);

        // fill method body
        mv.visitCode();

        // load the contextual instance Provider
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class));

        mv.visitInsn(Opcodes.ARETURN);

        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }
View Full Code Here

                parentClassFileName = classFileName;
                superDefaultCt = classToProxy.getDeclaredConstructor(null);
            }

            final String descriptor = Type.getConstructorDescriptor(superDefaultCt);
            final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor, null, null);
            mv.visitCode();
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassFileName, "<init>", descriptor);

            // the instance provider field
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class));

            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(-1, -1);
            mv.visitEnd();
        }
        catch (NoSuchMethodException e)
        {
            throw new ProxyGenerationException(e);
        }
View Full Code Here

                exceptionTypeNames[i] = Type.getType(exceptionTypes[i]).getInternalName();
            }

            int targetModifiers = delegatedMethod.getModifiers() & (Modifier.PROTECTED | Modifier.PUBLIC);

            MethodVisitor mv = cw.visitMethod(targetModifiers, delegatedMethod.getName(), methodDescriptor, null, exceptionTypeNames);

            // fill method body
            mv.visitCode();

            // load the contextual instance Provider
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class));

            // invoke the get() method on the Provider
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Provider.class), "get", "()Ljava/lang/Object;");

            // and convert the Object to the target class type
            mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(classToProxy));


            // now calculate the parameters
            int offset = 1;
            for (Class<?> aClass : delegatedMethod.getParameterTypes())
            {
                final Type type = Type.getType(aClass);
                mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), offset);
                offset += type.getSize();
            }

            // and finally invoke the target method on the provided Contextual Instance
            final Type declaringClass = Type.getType(delegatedMethod.getDeclaringClass());
            boolean interfaceMethod = Modifier.isInterface(delegatedMethod.getDeclaringClass().getModifiers());
            mv.visitMethodInsn(interfaceMethod ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL,
                               declaringClass.getInternalName(), delegatedMethod.getName(), methodDescriptor);

            generateReturn(mv, delegatedMethod);

            mv.visitMaxs(-1, -1);

            mv.visitEnd();
        }

    }
View Full Code Here

        final int modifiers = method.getModifiers();

        // push the method definition
        int modifier = modifiers & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED);

        MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null);
        mv.visitCode();


        mv.visitVarInsn(Opcodes.ALOAD, 0);

        // add the Method from the static array as first parameter
        mv.visitFieldInsn(Opcodes.GETSTATIC, proxyClassFileName, FIELD_PROTECTED_METHODS, Type.getDescriptor(Method[].class));

        // push the methodIndex of the current method
        mv.visitIntInsn(Opcodes.BIPUSH, methodIndex);

        // and now load the Method from the array
        mv.visitInsn(Opcodes.AALOAD);


        // now invoke the get() on the contextual instance Provider<T>
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class));

        // invoke the get() method on the Provider
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Provider.class), "get", "()Ljava/lang/Object;");


        // prepare the parameter array as Object[] and store it on the stack
        pushMethodParameterArray(mv, parameterTypes);


        // this invokes NormalScopeProxyFactory.delegateProtectedMethod
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(NormalScopeProxyFactory.class), "delegateProtectedMethod",
                "(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");

        // cast the result
        mv.visitTypeInsn(Opcodes.CHECKCAST, getCastType(returnType));


        if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType)))
        {
            // get the primitive value
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType),
                    "()" + Type.getDescriptor(returnType));
        }

        mv.visitInsn(getReturnInsn(returnType));

        // finish this method
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

    @Override
    protected void createSerialisation(ClassWriter cw, String proxyClassFileName, Class<?> classToProxy, String classFileName)
    {
        String[] exceptionTypeNames = {Type.getType(ObjectStreamException.class).getInternalName()};
        MethodVisitor mv = cw.visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, exceptionTypeNames);

        // fill method body
        mv.visitCode();

        // load the contextual instance Provider
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));

        mv.visitInsn(Opcodes.ARETURN);

        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }
View Full Code Here

TOP

Related Classes of cn.bran.japid.compiler.JavaSyntaxValidatorTest$MethodVisitor

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.