Java Code Examples for org.objectweb.asm.MethodVisitor

The following are top voted examples for showing how to use org.objectweb.asm.MethodVisitor. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: MeziLang   File: ASTCompileVisitor.java   View source code 6 votes vote down vote up
@Override
public Object visit(ASTStreamBlockElementHdr node, Object data) throws CompileException {

  // function or stream is in here
  TContext func_context = getTopContext().getClosestAncestor(AbsType.FORM_FUNC);
  if (func_context != null) {
    MethodVisitor mv = ((TContextFunc) func_context).getMethodVisitor();
    Debug.assertion(mv != null, "mv should be valid");

    /*
     * Label linelabel = new Label();
     *
     * // line label for debugging mv.visitLabel(linelabel);
     * mv.visitLineNumber(node.jjtGetFirstToken().beginLine, linelabel);
     */

    addLineLable(mv, node.jjtGetFirstToken().beginLine);
  }

  return null;
}
 
Example 2
Project: openrasp   File: JspCompilationContextHook.java   View source code 6 votes vote down vote up
/**
 * (none-javadoc)
 *
 * @see AbstractClassHook#hookMethod(int, String, String, String, String[], MethodVisitor)
 */
@Override
public MethodVisitor hookMethod(int access, String name, String desc,
                                String signature, String[] exceptions, MethodVisitor mv) {
    if (name.equals("compile") && desc.startsWith("()V")) {
        return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
            @Override
            protected void onMethodEnter() {
                invokeStatic(Type.getType(HookHandler.class),
                        new Method("preShieldHook", "()V"));
            }

            @Override
            protected void onMethodExit(int i) {
                invokeStatic(Type.getType(HookHandler.class),
                        new Method("postShieldHook", "()V"));
            }
        };
    }
    return mv;
}
 
Example 3
Project: r8   File: TestGenerator.java   View source code 6 votes vote down vote up
/**
 *  Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
 *  MethodHandle of kind get static. The method handle read a static field from a class.
 */
private void generateMethodTest10(ClassVisitor cv) {
  MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test10", "()V",
      null, null);
  MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
      MethodType.class, MethodHandle.class);
  Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
      "bsmCreateCallCallingtargetMethod", mt.toMethodDescriptorString(), false);
  mv.visitFieldInsn(Opcodes.GETSTATIC,
      "java/lang/System",
      "out",
      "Ljava/io/PrintStream;");
  mv.visitInvokeDynamicInsn("staticField1", "()Ljava/lang/String;", bootstrap,
      new Handle(Opcodes.H_GETSTATIC, Type.getInternalName(InvokeCustom.class),
          "staticField1", "Ljava/lang/String;", false));
  mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
      "java/io/PrintStream",
      "println",
      "(Ljava/lang/String;)V", false);
  mv.visitInsn(Opcodes.RETURN);
  mv.visitMaxs(-1, -1);
}
 
Example 4
Project: Reer   File: MethodStubbingApiMemberAdapter.java   View source code 6 votes vote down vote up
/**
 * Generates an exception which is going to be thrown in each method.
 * The reason it is in a separate method is because it reduces the bytecode size.
 */
private void generateUnsupportedOperationExceptionMethod() {
    MethodVisitor mv = cv.visitMethod(
        ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, UOE_METHOD,
        "()Ljava/lang/UnsupportedOperationException;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
        "You tried to call a method on an API class. Is the API jar on the classpath instead of the runtime jar?");
    mv.visitMethodInsn(
        INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 0);
    mv.visitEnd();
}
 
Example 5
Project: javametrics   File: ClassAdapter.java   View source code 6 votes vote down vote up
/**
 * Instrument HTTP request methods
 * 
 * @param mv
 *            original MethodVisitor
 * @param access
 * @param name
 * @param desc
 * @param signature
 * @param exceptions
 * @return original MethodVisitor or new MethodVisitor chained to original
 */
private MethodVisitor visitHttpMethod(MethodVisitor mv, int access, String name, String desc, String signature,
        String[] exceptions) {

    MethodVisitor httpMv = mv;

    /*
     * Instrument _jspService method for JSP. Instrument doGet, doPost and
     * service methods for servlets.
     */
    if ((httpInstrumentJsp && name.equals("_jspService")) || (httpInstrumentServlet
            && (name.equals("doGet") || name.equals("doPost") || name.equals("service")))) {

        // Only instrument if method has the correct signature
        if (HTTP_REQUEST_METHOD_DESC.equals(desc)) {
            httpMv = new ServletCallBackAdapter(className, mv, access, name, desc);
        }
    }

    return httpMv;
}
 
Example 6
Project: r8   File: TestGenerator.java   View source code 6 votes vote down vote up
/**
 *  Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
 *  MethodHandle of kind invoke interface. The target method is a default method into an interface
 *  that is at the end of a chain of interfaces.
 */
private void generateMethodTest6(ClassVisitor cv) {
  MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test6", "()V",
      null, null);
  MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
      MethodType.class, MethodHandle.class);
  Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
      "bsmCreateCallCallingtargetMethodTest7", mt.toMethodDescriptorString(), false);
  mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
  mv.visitInsn(Opcodes.DUP);
  mv.visitMethodInsn(
      Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
  mv.visitInvokeDynamicInsn("targetMethodTest7", "(Linvokecustom/J;)V", bootstrap,
      new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class),
          "targetMethodTest7", "()V", true));
  mv.visitInsn(Opcodes.RETURN);
  mv.visitMaxs(-1, -1);
}
 
Example 7
Project: fastAOP   File: ASMContentHandler.java   View source code 6 votes vote down vote up
@Override
public void begin(final String name, final Attributes attrs) {
    String desc = attrs.getValue("desc");
    boolean visible = Boolean.valueOf(attrs.getValue("visible"))
            .booleanValue();
    int typeRef = Integer.parseInt(attrs.getValue("typeRef"));
    TypePath typePath = TypePath.fromString(attrs.getValue("typePath"));

    Object v = peek();
    if (v instanceof ClassVisitor) {
        push(((ClassVisitor) v).visitTypeAnnotation(typeRef, typePath,
                desc, visible));
    } else if (v instanceof FieldVisitor) {
        push(((FieldVisitor) v).visitTypeAnnotation(typeRef, typePath,
                desc, visible));
    } else if (v instanceof MethodVisitor) {
        push(((MethodVisitor) v).visitTypeAnnotation(typeRef, typePath,
                desc, visible));
    }
}
 
Example 8
Project: DirectLeaks-AntiReleak-Remover   File: StaticInitMerger.java   View source code 6 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
        final String desc, final String signature, final String[] exceptions) {
    MethodVisitor mv;
    if ("<clinit>".equals(name)) {
        int a = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC;
        String n = prefix + counter++;
        mv = cv.visitMethod(a, n, desc, signature, exceptions);

        if (clinit == null) {
            clinit = cv.visitMethod(a, name, desc, null, null);
        }
        clinit.visitMethodInsn(Opcodes.INVOKESTATIC, this.name, n, desc,
                false);
    } else {
        mv = cv.visitMethod(access, name, desc, signature, exceptions);
    }
    return mv;
}
 
Example 9
Project: Reer   File: ManagedProxyClassGenerator.java   View source code 6 votes vote down vote up
private void writeSetter(ClassVisitor visitor, Type generatedType, String propertyName, Class<?> propertyClass, WeaklyTypeReferencingMethod<?, ?> weakSetter) {
    Type propertyType = Type.getType(propertyClass);
    Label calledOutsideOfConstructor = new Label();

    Method setter = weakSetter.getMethod();

    // the regular typed setter
    String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, propertyType);
    MethodVisitor methodVisitor = declareMethod(visitor, setter.getName(), methodDescriptor, AsmClassGeneratorUtils.signature(setter));

    putCanCallSettersFieldValueOnStack(methodVisitor, generatedType);
    jumpToLabelIfStackEvaluatesToTrue(methodVisitor, calledOutsideOfConstructor);
    throwExceptionBecauseCalledOnItself(methodVisitor);

    methodVisitor.visitLabel(calledOutsideOfConstructor);
    putStateFieldValueOnStack(methodVisitor, generatedType);
    putConstantOnStack(methodVisitor, propertyName);
    putFirstMethodArgumentOnStack(methodVisitor, propertyType);
    if (propertyClass.isPrimitive()) {
        boxType(methodVisitor, propertyClass);
    }
    invokeStateSetMethod(methodVisitor);

    finishVisitingMethod(methodVisitor);
}
 
Example 10
Project: fastAOP   File: ClassConstantsCollector.java   View source code 6 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
        final String desc, final String signature, final String[] exceptions) {
    if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
        cp.newUTF8("Synthetic");
    }
    if ((access & Opcodes.ACC_DEPRECATED) != 0) {
        cp.newUTF8("Deprecated");
    }
    cp.newUTF8(name);
    cp.newUTF8(desc);
    if (signature != null) {
        cp.newUTF8("Signature");
        cp.newUTF8(signature);
    }
    if (exceptions != null) {
        cp.newUTF8("Exceptions");
        for (int i = 0; i < exceptions.length; ++i) {
            cp.newClass(exceptions[i]);
        }
    }
    return new MethodConstantsCollector(cv.visitMethod(access, name, desc,
            signature, exceptions), cp);
}
 
Example 11
Project: Spigot-Nonce-ID-Finder   File: FrameNode.java   View source code 6 votes vote down vote up
/**
 * Makes the given visitor visit this stack map frame.
 * 
 * @param mv
 *            a method visitor.
 */
@Override
public void accept(final MethodVisitor mv) {
    switch (type) {
    case Opcodes.F_NEW:
    case Opcodes.F_FULL:
        mv.visitFrame(type, local.size(), asArray(local), stack.size(),
                asArray(stack));
        break;
    case Opcodes.F_APPEND:
        mv.visitFrame(type, local.size(), asArray(local), 0, null);
        break;
    case Opcodes.F_CHOP:
        mv.visitFrame(type, local.size(), null, 0, null);
        break;
    case Opcodes.F_SAME:
        mv.visitFrame(type, 0, null, 0, null);
        break;
    case Opcodes.F_SAME1:
        mv.visitFrame(type, 0, null, 1, asArray(stack));
        break;
    }
}
 
Example 12
Project: openrasp   File: ProxyDirContextHook.java   View source code 6 votes vote down vote up
/**
 * (none-javadoc)
 *
 * @see AbstractClassHook#hookMethod(int, String, String, String, String[], MethodVisitor)
 */
@Override
protected MethodVisitor hookMethod(int access, String name, String desc, String signature, String[] exceptions, MethodVisitor mv) {
    if ("lookup".equals(name) && desc.startsWith("(Ljava/lang/String;)")) {
        return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
            @Override
            public void onMethodExit(int opcode) {
                if (opcode == Opcodes.ARETURN) {
                    mv.visitVarInsn(ALOAD, 2);
                    mv.visitMethodInsn(INVOKESTATIC, "com/fuxi/javaagent/hook/ProxyDirContextHook", "checkResourceCacheEntry",
                            "(Ljava/lang/Object;)V", false);
                }
                super.onMethodExit(opcode);
            }

        };
    }
    return mv;
}
 
Example 13
Project: debug   File: InterceptClassGenerator.java   View source code 6 votes vote down vote up
private static void callUserMethodOrDirect(ClassLoader classLoader, InterceptedCall call, MethodVisitor mv) {
    /* Check if we have a user-provided validation method */
    String validationMethodOwnerName = getClassForMethod(classLoader, call.desc, call);
    if (validationMethodOwnerName != null) {
        /* we have, so call it... */
        mv.visitMethodInsn(INVOKESTATIC, validationMethodOwnerName, call.name, call.desc, false);
    } else {
        /* we don't have a user-defined validation method yet, so just call the target method directly */
        mv.visitMethodInsn(INVOKESTATIC, call.receiverInternalName, call.name, call.desc, false);
    }
    /* Check GL error if it was a GL call */
    if (VALIDATE.enabled && call.glName != null && !call.glName.equals("glGetError")) {
        mv.visitLdcInsn(call.name);
        mv.visitMethodInsn(INVOKESTATIC, RT_InternalName, "checkError", "(Ljava/lang/String;)V", false);
    }
}
 
Example 14
Project: FastMustache   File: ClassDataManager.java   View source code 6 votes vote down vote up
@Override
public void insertBooleanGetter(MethodVisitor mv, int var, String key) throws CompilerException {
    Class<?> type = insertGetter(mv, var, key, true).clazz;

    if(type == boolean.class) {
        // The correct type. We don't need to adapt it :D
        return;
    }

    if(type == Boolean.class) {
        // b.booleanValue()
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
    } else if(type.isPrimitive()) {
        throw new CompilerException("Can't parse a primitive into a boolean: " + type);
    } else {
        // Boolean.parseBoolean(object.toString())
        mv.visitMethodInsn(INVOKEVIRTUAL, OBJECT.getInternalName(), "toString", Type.getMethodDescriptor(STRING), false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "parseBoolean", Type.getMethodDescriptor(STRING, Type.BOOLEAN_TYPE), false);
    }
}
 
Example 15
Project: fastAOP   File: ASMContentHandler.java   View source code 5 votes vote down vote up
@Override
public void begin(final String name, final Attributes attrs) {
    String desc = attrs.getValue("desc");
    boolean visible = Boolean.valueOf(attrs.getValue("visible"))
            .booleanValue();
    int typeRef = Integer.parseInt(attrs.getValue("typeRef"));
    TypePath typePath = TypePath.fromString(attrs.getValue("typePath"));
    push(((MethodVisitor) peek()).visitTryCatchAnnotation(typeRef,
            typePath, desc, visible));
}
 
Example 16
Project: businessworks   File: LineNumbers.java   View source code 5 votes vote down vote up
public MethodVisitor visitMethod(int access, String name, String desc,
    String signature, String[] exceptions) {
  if ((access & Opcodes.ACC_PRIVATE) != 0) {
    return null;
  }
  pendingMethod = name + desc;
  line = -1;
  return new LineNumberMethodVisitor();
}
 
Example 17
Project: openrasp   File: ThreadHookClassVisitor.java   View source code 5 votes vote down vote up
/**
 * hook线程子类
 *
 * @see ClassVisitor#visitMethod(int, String, String, String, String[])
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (isRunnable && name.equals("run") && desc.equals("()V")) {
        return new CommonMethodVisitor(mv, this.className, access, name, desc);
    }
    if (isCallable && name.equals("call") && desc.equals("()V")) {
        return new CommonMethodVisitor(mv, this.className, access, name, desc);
    }
    return mv;
}
 
Example 18
Project: Robust   File: RobustAsmUtils.java   View source code 5 votes vote down vote up
private static void createBooleanObj(MethodVisitor mv, int argsPostion) {
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/Byte");
    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ILOAD, argsPostion);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Byte", "<init>", "(B)V");
    mv.visitInsn(Opcodes.AASTORE);
}
 
Example 19
Project: BukkitPlugin-Message-Injector   File: InsnList.java   View source code 5 votes vote down vote up
/**
 * Makes the given visitor visit all of the instructions in this list.
 * 
 * @param mv
 *            the method visitor that must visit the instructions.
 */
public void accept(final MethodVisitor mv) {
    AbstractInsnNode insn = first;
    while (insn != null) {
        insn.accept(mv);
        insn = insn.next;
    }
}
 
Example 20
Project: primeval-reflex   File: MethodArgumentsGenerator.java   View source code 5 votes vote down vote up
private static void genExceptionThrowForUnknownParam(String paramType, MethodVisitor mv) {
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("No " + paramType + " parameter named ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
}
 
Example 21
Project: MeziLang   File: ASTCompileVisitor.java   View source code 5 votes vote down vote up
@Override
public Object visit(ASTIfCaseExpr node, Object data) throws CompileException {

  MethodVisitor mv = getClosestFunContext().getMethodVisitor();
  Label ifend_label = node.getBrLabel();

  if (node.hasDefinitionRefernce()) {
    Reduction reduce = topReduction();
    Debug.assertion(reduce != null, "Reduction should not be invalid");
    if (reduce.isContainer()) {
      Container cont = (Container) reduce;
      AbsType type = cont.getType();
      Debug.assertion(type != null, "type should be valid");

      if (cont.isForm(Container.FORM_OPSTACK_VAR) && !type.isName(TPrimitiveClass.NAME_VOID)) {
        type.op().pop(cont, new OpInfo(getTopContext()));

        popReduction(); // consume op stack var non void container
        pushReduction(new Control(Control.FORM_BRANCH));
      }
    }
  }

  if (ifend_label != null && node.isAliveCtrlFlow()) {
    mv.visitJumpInsn(GOTO, ifend_label);
  }

  return null;
}
 
Example 22
Project: MeziLang   File: ASTCompileVisitor.java   View source code 5 votes vote down vote up
@Override
public Object visit(ASTForAction node, Object data) throws CompileException {

  MethodVisitor mv = getClosestFunContext().getMethodVisitor();

  if (node.jjtGetNumChildren() == 0) {
    // do nothing
  } else if (node.jjtGetNumChildren() == 1) {
    Reduction reduce = popReduction();
    Debug.assertion(reduce != null, "Reductin should not be invalid");
    Debug.assertion(reduce.isContainer(), "Reductin should be container");

    Container cont = (Container) reduce;
    Debug.assertion(cont.isTypeInitialized(), "cont should be type initialized");

    cont.getType().op().pop(cont, new OpInfo(getTopContext()));
  } else {
    throw new CompileException("This case is not considered");
  }

  LangUnitNode parent = (LangUnitNode) node.jjtGetParent();
  Debug.assertion(parent != null, "parent should not be invalid");
  Debug.assertion(parent.isNodeId(JJTCSTYLELOOPEXPR), "parent should be c style loop");

  Label loop_cond_label = parent.getLoopCondLabel();
  Debug.assertion(loop_cond_label != null, "loop condition label should not be invalid");

  mv.visitLabel(loop_cond_label);

  return null;
}
 
Example 23
Project: openrasp   File: HttpClientHook.java   View source code 5 votes vote down vote up
@Override
protected MethodVisitor hookMethod(int access, String name, String desc, String signature, String[] exceptions, MethodVisitor mv) {
    if (name.equals("setURI") && desc.equals("(Ljava/net/URI;)V")) {
        return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
            @Override
            protected void onMethodEnter() {
                loadArg(0);
                invokeStatic(Type.getType(HttpClientHook.class),
                        new Method("checkHttpUri", "(Ljava/net/URI;)V"));
            }
        };
    }
    return mv;
}
 
Example 24
Project: DirectLeaks-AntiReleak-Remover   File: ASMContentHandler.java   View source code 5 votes vote down vote up
@Override
public void begin(final String name, final Attributes attrs) {
    int parameter = Integer.parseInt(attrs.getValue("parameter"));
    String desc = attrs.getValue("desc");
    boolean visible = Boolean.valueOf(attrs.getValue("visible"))
            .booleanValue();

    push(((MethodVisitor) peek()).visitParameterAnnotation(parameter,
            desc, visible));
}
 
Example 25
Project: openrasp   File: FileOutputStream2Hook.java   View source code 5 votes vote down vote up
/**
 * (none-javadoc)
 *
 * @see com.fuxi.javaagent.hook.AbstractClassHook#hookMethod(int, String, String, String, String[], MethodVisitor)
 */
@Override
protected MethodVisitor hookMethod(int access, String name, String desc, String signature, String[] exceptions, MethodVisitor mv) {
    // store file info in lockClose Object
    if ("<init>".equals(name) && "(Ljava/io/File;Z)V".equals(desc)) {
        return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
            @Override
            public void onMethodExit(int opcode) {
                if (opcode == Opcodes.RETURN) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitTypeInsn(NEW, "com/fuxi/javaagent/tool/hook/CustomLockObject");
                    mv.visitInsn(DUP);
                    mv.visitMethodInsn(INVOKESPECIAL, "com/fuxi/javaagent/tool/hook/CustomLockObject",
                            "<init>", "()V", false);
                    mv.visitFieldInsn(PUTFIELD, "java/io/FileOutputStream", "closeLock", "Ljava/lang/Object;");
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitFieldInsn(GETFIELD, "java/io/FileOutputStream", "closeLock", "Ljava/lang/Object;");
                    mv.visitVarInsn(ALOAD, 3);
                    mv.visitMethodInsn(INVOKESTATIC, "com/fuxi/javaagent/hook/file/FileOutputStream2Hook", "checkFileOutputStreamInit",
                            "(Ljava/lang/Object;Ljava/lang/String;)V", false);
                }
                super.onMethodExit(opcode);
            }
        };
    }
    if (name.equals("write") && desc.startsWith("([B")) {
        return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
            @Override
            public void onMethodEnter() {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, "java/io/FileOutputStream", "closeLock", "Ljava/lang/Object;");
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKESTATIC, "com/fuxi/javaagent/hook/file/FileOutputStream2Hook", "checkFileOutputStreamWrite",
                        "(Ljava/lang/Object;[B)V", false);
            }
        };
    }
    return mv;
}
 
Example 26
Project: BukkitPlugin-Message-Injector   File: TableSwitchInsnNode.java   View source code 5 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    Label[] labels = new Label[this.labels.size()];
    for (int i = 0; i < labels.length; ++i) {
        labels[i] = this.labels.get(i).getLabel();
    }
    mv.visitTableSwitchInsn(min, max, dflt.getLabel(), labels);
    acceptAnnotations(mv);
}
 
Example 27
Project: fastAOP   File: SerialVersionUIDAdder.java   View source code 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
        final String desc, final String signature, final String[] exceptions) {
    if (computeSVUID) {
        if ("<clinit>".equals(name)) {
            hasStaticInitializer = true;
        }
        /*
         * Remembers non private constructors and methods for SVUID
         * computation For constructor and method modifiers, only the
         * ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
         * ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT and ACC_STRICT flags
         * are used.
         */
        int mods = access
                & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE
                        | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC
                        | Opcodes.ACC_FINAL | Opcodes.ACC_SYNCHRONIZED
                        | Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STRICT);

        // all non private methods
        if ((access & Opcodes.ACC_PRIVATE) == 0) {
            if ("<init>".equals(name)) {
                svuidConstructors.add(new Item(name, mods, desc));
            } else if (!"<clinit>".equals(name)) {
                svuidMethods.add(new Item(name, mods, desc));
            }
        }
    }

    return super.visitMethod(access, name, desc, signature, exceptions);
}
 
Example 28
Project: primeval-reflex   File: MethodArgumentsGenerator.java   View source code 5 votes vote down vote up
private static void generateParametersGetter(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "parameters", "()Ljava/util/List;", "()Ljava/util/List<Ljava/lang/reflect/Parameter;>;", null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, selfClassInternalName, "parameters", "Ljava/util/List;");
    mv.visitInsn(ARETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l1, 0);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
 
Example 29
Project: faster-than-reflection   File: AsmUtils.java   View source code 5 votes vote down vote up
public static void visitDefaultConstructor(ClassWriter cw, String classTypeDescriptor) {
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
	mv.visitCode();
	Label l0 = new Label();
	mv.visitLabel(l0);
	mv.visitVarInsn(ALOAD, 0);
	mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
	mv.visitInsn(RETURN);
	Label l1 = new Label();
	mv.visitLabel(l1);
	mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l1, 0);
	mv.visitMaxs(1, 1);
	mv.visitEnd();
}
 
Example 30
Project: MCheatEngine   File: MethodChangeClassAdapter.java   View source code 5 votes vote down vote up
@Override  
public MethodVisitor visitMethod(  
    int access,  
    String name,  
    String desc,  
    String signature,  
    String[] exceptions)  
{  
       
   
        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);//�ȵõ�ԭʼ�ķ���    
        MethodVisitor newMethod = new AsmMethodVisit(mv,targetMethodname,toName);              
        return newMethod;    
}
 
Example 31
Project: Spigot-Nonce-ID-Finder   File: TableSwitchInsnNode.java   View source code 5 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    Label[] labels = new Label[this.labels.size()];
    for (int i = 0; i < labels.length; ++i) {
        labels[i] = this.labels.get(i).getLabel();
    }
    mv.visitTableSwitchInsn(min, max, dflt.getLabel(), labels);
    acceptAnnotations(mv);
}
 
Example 32
Project: Reer   File: ManagedProxyClassGenerator.java   View source code 5 votes vote down vote up
private void writeReadOnlySetter(ClassVisitor visitor, Class<?> viewClass, boolean writable, ModelProperty<?> property) {
    if (!writable) {
        // Adds a void set$PropName(Object value) method that fails
        String setterName = "set" + StringUtils.capitalize(property.getName());
        MethodVisitor methodVisitor = declareMethod(visitor, setterName, SET_OBJECT_PROPERTY_DESCRIPTOR, null, ACC_PUBLIC | ACC_SYNTHETIC);

        // throw new ReadOnlyPropertyException(name, <view-type>.class)
        methodVisitor.visitTypeInsn(NEW, READ_ONLY_PROPERTY_EXCEPTION_TYPE);
        methodVisitor.visitInsn(DUP);
        putConstantOnStack(methodVisitor, property.getName());
        putClassOnStack(methodVisitor, viewClass);
        methodVisitor.visitMethodInsn(INVOKESPECIAL, READ_ONLY_PROPERTY_EXCEPTION_TYPE, "<init>", MISSING_PROPERTY_CONSTRUCTOR_DESCRIPTOR, false);
        finishVisitingMethod(methodVisitor, ATHROW);
    }
}
 
Example 33
Project: Robust   File: RobustAsmUtils.java   View source code 5 votes vote down vote up
private static void createLongObj(MethodVisitor mv, int argsPostion) {
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/Long");
    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.LLOAD, argsPostion);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Long", "<init>", "(J)V");
    mv.visitInsn(Opcodes.AASTORE);
}
 
Example 34
Project: Spigot-Attribute-Remover   File: RemappingClassAdapter.java   View source code 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
        String signature, String[] exceptions) {
    String newDesc = remapper.mapMethodDesc(desc);
    MethodVisitor mv = super.visitMethod(access, remapper.mapMethodName(
            className, name, desc), newDesc, remapper.mapSignature(
            signature, false),
            exceptions == null ? null : remapper.mapTypes(exceptions));
    return mv == null ? null : createRemappingMethodAdapter(access,
            newDesc, mv);
}
 
Example 35
Project: authlib-injector   File: LdcTransformUnit.java   View source code 5 votes vote down vote up
@Override
public Optional<ClassVisitor> transform(String className, ClassVisitor writer, Runnable modifiedCallback) {
	return Optional.of(new ClassVisitor(ASM5, writer) {

		@Override
		public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
			return new MethodVisitor(ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {

				@Override
				public void visitLdcInsn(Object cst) {
					if (cst instanceof String) {
						Optional<String> transformed = ldcMapper.apply((String) cst);
						if (transformed.isPresent() && !transformed.get().equals(cst)) {
							modifiedCallback.run();
							log("transform [{0}] to [{1}]", cst, transformed.get());
							super.visitLdcInsn(transformed.get());
						} else {
							super.visitLdcInsn(cst);
						}
					} else {
						super.visitLdcInsn(cst);
					}
				}
			};
		}
	});
}
 
Example 36
Project: primeval-reflex   File: ProxyClassGenerator.java   View source code 5 votes vote down vote up
static void visitIntInsn(MethodVisitor mv, int i) {
    switch (i) {
    case -1:
        mv.visitInsn(ICONST_M1);
        break;
    case 0:
        mv.visitInsn(ICONST_0);
        break;
    case 1:
        mv.visitInsn(ICONST_1);
        break;
    case 2:
        mv.visitInsn(ICONST_2);
        break;
    case 3:
        mv.visitInsn(ICONST_3);
        break;
    case 4:
        mv.visitInsn(ICONST_4);
        break;
    case 5:
        mv.visitInsn(ICONST_5);
        break;

    default:
        mv.visitIntInsn(BIPUSH, i);
        break;
    }
}
 
Example 37
Project: fastAOP   File: LookupSwitchInsnNode.java   View source code 5 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    int[] keys = new int[this.keys.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = this.keys.get(i).intValue();
    }
    Label[] labels = new Label[this.labels.size()];
    for (int i = 0; i < labels.length; ++i) {
        labels[i] = this.labels.get(i).getLabel();
    }
    mv.visitLookupSwitchInsn(dflt.getLabel(), keys, labels);
    acceptAnnotations(mv);
}
 
Example 38
Project: debug   File: InterceptClassGenerator.java   View source code 5 votes vote down vote up
private static void generateDefaultTraceBefore(InterceptedCall call, MethodVisitor mv, Type[] paramTypes, MethodInfo minfo) {
    int var = 0;
    int glEnumIndex = 0;
    for (int i = 0; i < paramTypes.length; i++) {
        Type paramType = paramTypes[i];
        String nativeType = minfo.parameterNativeTypes[i];
        if ("GLenum".equals(nativeType) || "GLboolean".equals(nativeType)) {
            glEnumIndex = loadGLenum(call.glName, "glEnumFor", mv, var, glEnumIndex);
        } else if ("GLbitfield".equals(nativeType)) {
            glEnumIndex = loadGLenum(call.glName, "decodeBitField", mv, var, glEnumIndex);
        } else if ("GLFWwindow *".equals(nativeType)) {
            mv.visitVarInsn(paramType.getOpcode(ILOAD), var);
            mv.visitMethodInsn(INVOKESTATIC, RT_InternalName, "paramGlfwWindow", "(" + MethodCall_Desc + paramType.getDescriptor() + ")" + MethodCall_Desc, false);
        } else if ("GLFWmonitor *".equals(nativeType)) {
            mv.visitVarInsn(paramType.getOpcode(ILOAD), var);
            mv.visitMethodInsn(INVOKESTATIC, RT_InternalName, "paramGlfwMonitor", "(" + MethodCall_Desc + paramType.getDescriptor() + ")" + MethodCall_Desc, false);
        } else {
            mv.visitVarInsn(paramType.getOpcode(ILOAD), var);
            if (paramType.getSort() == Type.ARRAY || paramType.getSort() == Type.OBJECT) {
                mv.visitMethodInsn(INVOKEVIRTUAL, MethodCall_InternalName, "param", "(Ljava/lang/Object;)" + MethodCall_Desc, false);
            } else {
                mv.visitMethodInsn(INVOKEVIRTUAL, MethodCall_InternalName, "param", "(" + paramType.getDescriptor() + ")" + MethodCall_Desc, false);
            }
        }
        var += paramType.getSize();
    }
    mv.visitInsn(POP);
}
 
Example 39
Project: Reer   File: ManagedProxyClassGenerator.java   View source code 5 votes vote down vote up
private void writeGetter(ClassVisitor visitor, Type generatedType, String propertyName, Class<?> propertyClass, WeaklyTypeReferencingMethod<?, ?> weakGetter) {
    Method getter = weakGetter.getMethod();
    Type propertyType = Type.getType(propertyClass);
    MethodVisitor methodVisitor = declareMethod(
        visitor,
        getter.getName(),
        Type.getMethodDescriptor(propertyType),
        AsmClassGeneratorUtils.signature(getter));

    putStateFieldValueOnStack(methodVisitor, generatedType);
    putConstantOnStack(methodVisitor, propertyName);
    invokeStateGetMethod(methodVisitor);
    castFirstStackElement(methodVisitor, propertyClass);
    finishVisitingMethod(methodVisitor, returnCode(propertyType));
}
 
Example 40
Project: Reer   File: MixInLegacyTypesClassLoader.java   View source code 5 votes vote down vote up
private void addSetMetaClass() {
    MethodVisitor methodVisitor = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "setMetaClass", Type.getMethodDescriptor(Type.VOID_TYPE, META_CLASS_TYPE), null, null);
    methodVisitor.visitCode();

    // this.metaClass = <value>
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, className, META_CLASS_FIELD, META_CLASS_TYPE.getDescriptor());

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(2, 2);
    methodVisitor.visitEnd();
}