Example usage for org.objectweb.asm.commons Method Method

List of usage examples for org.objectweb.asm.commons Method Method

Introduction

In this page you can find the example usage for org.objectweb.asm.commons Method Method.

Prototype

public Method(final String name, final Type returnType, final Type[] argumentTypes) 

Source Link

Document

Constructs a new Method .

Usage

From source file:blue.origami.asm.OAsmUtils.java

License:Apache License

public static Method asmMethod(Class<?> ret, String methodName, Class<?>... p) {
    return new Method(methodName, asmType(ret), asmTypes(p));
}

From source file:bluejelly.runtime.ModuleReader.java

License:BSD License

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

    Type[] paramTypes = Type.getArgumentTypes(desc);
    Type returnType = Type.getReturnType(desc);
    this.m = new Method(name, returnType, paramTypes);

    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java

License:Apache License

/**
 * Defines the class by rewriting the constructor to call
 * SparkRuntimeEnv#setContext(SparkContext) or SparkRuntimeEnv#setContext(StreamingContext),
 * depending on the class type./*from   w  w w  .  ja  v  a2s.  c  om*/
 */
private Class<?> defineContext(final Type contextType, InputStream byteCodeStream) throws IOException {
    return rewriteConstructorAndDefineClass(contextType, byteCodeStream, new ConstructorRewriter() {
        @Override
        public void onMethodExit(GeneratorAdapter generatorAdapter) {
            generatorAdapter.loadThis();
            generatorAdapter.invokeStatic(SPARK_RUNTIME_ENV_TYPE,
                    new Method("setContext", Type.VOID_TYPE, new Type[] { contextType }));
        }
    });
}

From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java

License:Apache License

/**
 * Defines the SparkConf class by rewriting the constructor to call SparkRuntimeEnv#setupSparkConf(SparkConf).
 *//*from  w  w w.  j  a  v a  2 s.  c  om*/
private Class<?> defineSparkConf(final Type sparkConfType, InputStream byteCodeStream) throws IOException {
    return rewriteConstructorAndDefineClass(sparkConfType, byteCodeStream, new ConstructorRewriter() {
        @Override
        public void onMethodExit(GeneratorAdapter generatorAdapter) {
            generatorAdapter.loadThis();
            generatorAdapter.invokeStatic(SPARK_RUNTIME_ENV_TYPE,
                    new Method("setupSparkConf", Type.VOID_TYPE, new Type[] { sparkConfType }));
        }
    });
}

From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java

License:Apache License

/**
 * Defines the org.apache.spark.deploy.yarn.Client class with rewriting of the createConfArchive method to
 * workaround the SPARK-13441 bug./*from w  w  w  .j  av  a 2  s. c om*/
 */
private Class<?> defineClient(String name, InputStream createConfArchive)
        throws IOException, ClassNotFoundException {
    // We only need to rewrite if listing either HADOOP_CONF_DIR or YARN_CONF_DIR return null.
    boolean needRewrite = false;
    for (String env : ImmutableList.of("HADOOP_CONF_DIR", "YARN_CONF_DIR")) {
        String value = System.getenv(env);
        if (value != null) {
            File path = new File(value);
            if (path.isDirectory() && path.listFiles() == null) {
                needRewrite = true;
                break;
            }
        }
    }

    // If rewrite is not needed
    if (!needRewrite) {
        return findClass(name);
    }

    ClassReader cr = new ClassReader(createConfArchive);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cr.accept(new ClassVisitor(Opcodes.ASM5, cw) {
        @Override
        public MethodVisitor visitMethod(final int access, final String name, final String desc,
                String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);

            // Only rewrite the createConfArchive method
            if (!"createConfArchive".equals(name)) {
                return mv;
            }

            // Check if it's a recognizable return type.
            // Spark 1.5+ return type is File
            boolean isReturnFile = Type.getReturnType(desc).equals(Type.getType(File.class));
            Type optionType = Type.getObjectType("scala/Option");
            if (!isReturnFile) {
                // Spark 1.4 return type is Option<File>
                if (!Type.getReturnType(desc).equals(optionType)) {
                    // Unknown type. Not going to modify the code.
                    return mv;
                }
            }

            // Generate this for Spark 1.5+
            // return SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
            //                                            LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP);
            // Generate this for Spark 1.4
            // return Option.apply(SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
            //                                                         LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP));
            GeneratorAdapter mg = new GeneratorAdapter(mv, access, name, desc);

            // load this.sparkConf to the stack
            mg.loadThis();
            mg.getField(Type.getObjectType("org/apache/spark/deploy/yarn/Client"), "sparkConf",
                    SPARK_CONF_TYPE);

            // push three constants to the stack
            mg.visitLdcInsn(SPARK_CONF_FILE);
            mg.visitLdcInsn(LOCALIZED_CONF_DIR);
            mg.visitLdcInsn(LOCALIZED_CONF_DIR_ZIP);

            // call SparkRuntimeUtils.createConfArchive, return a File and leave it in stack
            Type stringType = Type.getType(String.class);
            mg.invokeStatic(SPARK_RUNTIME_UTILS_TYPE, new Method("createConfArchive", Type.getType(File.class),
                    new Type[] { SPARK_CONF_TYPE, stringType, stringType, stringType }));
            if (isReturnFile) {
                // Spark 1.5+ return type is File, hence just return the File from the stack
                mg.returnValue();
                mg.endMethod();
            } else {
                // Spark 1.4 return type is Option<File>
                // return Option.apply(<file from stack>);
                // where the file is actually just popped from the stack
                mg.invokeStatic(optionType,
                        new Method("apply", optionType, new Type[] { Type.getType(Object.class) }));
                mg.checkCast(optionType);
                mg.returnValue();
                mg.endMethod();
            }

            return null;
        }
    }, ClassReader.EXPAND_FRAMES);

    byte[] byteCode = cw.toByteArray();
    return defineClass(name, byteCode, 0, byteCode.length);
}

From source file:co.cask.cdap.internal.asm.Methods.java

License:Apache License

public static Method getMethod(Class<?> returnType, String name, Class<?>... args) {
    Type[] argTypes = new Type[args.length];
    for (int i = 0; i < args.length; i++) {
        argTypes[i] = Type.getType(args[i]);
    }//from   www .j  a  v a 2s .  c o  m

    return new Method(name, Type.getType(returnType), argTypes);
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

@Override
public Void visitProcedureCall(ProcedureCallContext ctx) {
    //      super.visitProcedureCall(ctx);
    debug("visitProcedureCall():" + currentModule.getClassFullyQualifiedName());

    String procedureCall = "";
    if (ctx.START() != null) {
        procedureCall = "START";
    }//  w w  w .j  a  v a 2s. c  o  m
    if (ctx.SL_STRING_REVERSE() != null) {
        procedureCall = "SL_STRING.REVERSE";
    }
    if (ctx.SOCKET_DICONNECT() != null) {
        procedureCall = "SOCKET.DISCONNECT";
    }

    String procedureName = procedureCall;
    String moduleName = "";
    if (procedureCall.contains(".")) {
        // call to external procedure
        moduleName = procedureCall.split("\\.")[0];
        procedureName = procedureCall.split("\\.")[1];
    }

    try {
        if (currentMethod == null) {
            if (moduleName.equals("")) {
                Type classType = Type.getObjectType(
                        Utils.getInternalFQClassName(currentModule.getClassFullyQualifiedName()));
                Methods.callLocalProcedure(mainMethod, classType,
                        new Method("getModule", classType, new Type[0]),
                        new Method(Utils.getJavaMethodName(procedureName), Type.VOID_TYPE, new Type[0]));
            } else {
                callRemoteProcedure(moduleName, procedureName, ctx);
            }
        } else {
            if (moduleName.equals("")) {
                Type classType = Type.getObjectType(
                        Utils.getInternalFQClassName(currentModule.getClassFullyQualifiedName()));
                Methods.callLocalProcedure(currentMethod, classType,
                        new Method("getModule", classType, new Type[0]),
                        new Method(Utils.getJavaMethodName(procedureName), Type.VOID_TYPE, new Type[0]));
            } else {
                callRemoteProcedure(moduleName, procedureName, ctx);
            }
        }
    } catch (CompileException e) {
        e.printStackTrace();
        System.exit(1);
    }

    return null;
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

@Override
public Void visitFunctionInvocation(FunctionInvocationContext ctx) {
    debug("visitFunctionInvocation()");
    String functionFullName = ctx.functionName().getText();
    String moduleName = "";
    String functionName = functionFullName;

    isStatementCall = true;/*  ww w .  j a v a 2  s.  c o  m*/
    statementCallCurrentParameterIndex = 0;

    if (functionFullName.contains(".")) {
        moduleName = functionFullName.split("\\.")[0];
        functionName = functionFullName.split("\\.")[1];
    }
    debug("call function :" + Utils.getJavaFullyQualifiedClassName(moduleName) + "."
            + Utils.getJavaMethodName(functionName) + "(...)");

    Type moduleType = Utils.getJavaFullyQualifiedClassType(moduleName);

    Method getModuleMethod = new Method("getModule", moduleType, new Type[0]);
    Method calledFunction = new Method(Utils.getJavaMethodName(functionName),
            Type.getObjectType("java/lang/Object"), new Type[] { Type.getObjectType("[Ljava/lang/Object;") });
    int arraySize = ctx.argumentList() != null ? ctx.argumentList().expression().size() : 0;

    if ("".equals(moduleName)) {
        // internal module in an executable module
    } else {
        if (currentMethod != null) {
            currentMethod.invokeStatic(moduleType, getModuleMethod);
            currentMethod.visitInsn(Opcodes.ICONST_0 + arraySize);
            currentMethod.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        } else {
            currentConstructor.invokeStatic(moduleType, getModuleMethod);
            currentConstructor.visitInsn(Opcodes.ICONST_0 + arraySize);
            currentConstructor.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        }
    }

    super.visitFunctionInvocation(ctx);

    if ("".equals(moduleName)) {
        // internal module in an executable module
    } else {
        if (currentMethod != null) {
            currentMethod.invokeVirtual(moduleType, calledFunction);
        } else {
            currentConstructor.invokeVirtual(moduleType, calledFunction);
        }
    }

    isStatementCall = false;

    return null;
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

private void callRemoteProcedure(String moduleName, String procedureName, ProcedureCallContext ctx)
        throws CompileException {
    debug("callRemoteProcedure():" + moduleName + "." + procedureName);
    String moduleAsJavaClass = Utils.getJavaFullyQualifiedClassName(moduleName);
    String procName = Utils.getJavaMethodName(procedureName);

    ProcedureParameters params = (ProcedureParameters) Reflections.getAnnotationOfMethod(moduleAsJavaClass,
            procName, ProcedureParameters.class);
    if (params == null) {
        throw new CompileException("Call remote statement " + moduleName + "." + procedureName
                + "  doesn't have defined necesary annotations.");
    }/*from ww w  .  j a v  a  2s. co  m*/

    Type moduleType = Utils.getJavaFullyQualifiedClassType(moduleName);
    Method getModuleMethod = new Method("getModule", moduleType, new Type[0]);
    Method calledProcedure = new Method(Utils.getJavaMethodName(procName),
            Type.getObjectType("[Ljava/lang/Object;"),
            new Type[] { Type.getObjectType("[Ljava/lang/Object;") });

    int arraySizeIn = 0;
    int arraySizeOut = 0;
    for (ProcedureParameter param : params.value()) {
        if (param.paramIOType() == ProcedureParameterIOType.IN
                || param.paramIOType() == ProcedureParameterIOType.INOUT) {
            arraySizeIn++;
        }
        if (param.paramIOType() == ProcedureParameterIOType.OUT
                || param.paramIOType() == ProcedureParameterIOType.INOUT) {
            arraySizeOut++;
        }
    }
    MethodBuilder crtMet = currentMethod != null ? currentMethod : currentConstructor;
    Methods.beginProcedureCall(crtMet, moduleType, getModuleMethod, arraySizeIn);

    int indexInParams = 0;
    boolean workWithNoiseWords = false;
    int indexInValues = 0;
    int indexOfInParametersInCallArray = 0;
    // cycle for input before method call
    for (ProcedureParameter param : params.value()) {
        if (param.paramIOType() == ProcedureParameterIOType.IN
                || param.paramIOType() == ProcedureParameterIOType.INOUT) {
            if (indexInParams == 1 && param.noiseWord().value() == ProcParameterNoiseWord.Word.DEFINED) {
                workWithNoiseWords = true;
            }
            if (!workWithNoiseWords) {
                if (ctx.procedureRealParameterList() != null) {
                    ProcedureRealParameterContext rp = ctx.procedureRealParameterList().procedureRealParameter()
                            .get(indexInValues);
                    if (param.paramIOType() == ProcedureParameterIOType.INOUT
                            && rp.expression().primary().variableIdentifier() == null) {
                        throw new CompileException("Call remote statement " + moduleName + "." + procedureName
                                + "  with OUT parameter " + indexInParams
                                + " as a primitive and not as a variable.");
                    }
                    // put reference on stack
                    String varName = convertVariableName(
                            rp.expression().primary().variableIdentifier().getText());
                    varName = "y";
                    if (crtMet.isLocalVariableDefined(varName)) {
                        crtMet.loadFromLocalVar(varName, true, indexOfInParametersInCallArray);
                    } else {
                        crtMet.loadFromField(currentModule, varName, true, indexOfInParametersInCallArray);
                    }
                    indexOfInParametersInCallArray++;
                } else {
                }
            } else {

            }
        }

        indexInParams++;
        indexInValues++;
    }

    Methods.doProcedureCall(crtMet, moduleType, calledProcedure);

    indexInParams = 0;
    workWithNoiseWords = false;
    indexInValues = 0;
    int indexOfInParametersInResultsArray = 0;
    // cycle for output after method call
    for (ProcedureParameter param : params.value()) {
        if (param.paramIOType() == ProcedureParameterIOType.OUT
                || param.paramIOType() == ProcedureParameterIOType.INOUT) {
            if (indexInParams == 1 && param.noiseWord().value() == ProcParameterNoiseWord.Word.DEFINED) {
                workWithNoiseWords = true;
            }
            if (!workWithNoiseWords) {
                if (ctx.procedureRealParameterList() != null) {
                    ProcedureRealParameterContext rp = ctx.procedureRealParameterList().procedureRealParameter()
                            .get(indexInValues);
                    // get reference on stack
                    String varName = convertVariableName(
                            rp.expression().primary().variableIdentifier().getText());
                    varName = "y";
                    if (crtMet.isLocalVariableDefined(varName)) {
                        crtMet.storeInLocalVar(varName, true, indexOfInParametersInResultsArray);
                    } else {
                        crtMet.storeInField(currentModule, varName, true, indexOfInParametersInResultsArray);
                    }
                    indexOfInParametersInResultsArray++;

                }
            }

        }

    }

    Methods.endProcedureCall(crtMet);
}

From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w  w .j  av a 2  s.c o  m*/
 */
public Class<?> visitProperty(int pos, String property, java.lang.reflect.Method getter, boolean hasNext,
        Class<?> beanClass) {
    checkNull(pos);

    // If expected class is not equal to the type on top of the stack, do
    // the cast
    if (declaredClass != beanClass) {
        accessormg.checkCast(Type.getType(beanClass));
    }

    if (!hasNext && isSetter) {
        java.lang.reflect.Method setter = IntrospectUtils.findSetter(beanClass, property);
        Type type = Type.getType(setter.getParameterTypes()[0]);

        // Cast parameter to required type
        accessormg.loadArg(1);
        accessormg.unbox(type);

        Method method = new Method(setter.getName(), Type.VOID_TYPE, new Type[] { type });
        accessormg.invokeVirtual(Type.getType(beanClass), method);
    } else {

        Type type = Type.getType(getter.getReturnType());
        Method method = new Method(getter.getName(), type, new Type[0]);

        accessormg.invokeVirtual(Type.getType(beanClass), method);

        // If this is last property in the path, box the result to match
        // Getter#getValue return value
        if (!hasNext) {
            accessormg.box(type);
        }

        // Remember type on top of the stack
        declaredClass = getter.getReturnType();
    }
    return null;
}