Search in sources :

Example 1 with BytecodeSequence

use of org.codehaus.groovy.classgen.BytecodeSequence in project groovy by apache.

the class NullCheckASTTransformation method adjustMethod.

private void adjustMethod(MethodNode mn, boolean includeGenerated) {
    BlockStatement newCode = getCodeAsBlock(mn);
    if (mn.getParameters().length == 0)
        return;
    boolean generated = isGenerated(mn);
    int startingIndex = 0;
    if (!includeGenerated && generated)
        return;
    if (isMarkedAsProcessed(mn))
        return;
    if (mn instanceof ConstructorNode) {
        // some transform has been here already and we assume it knows what it is doing
        if (mn.getFirstStatement() instanceof BytecodeSequence)
            return;
        // ignore any constructors calling this(...) or super(...)
        ConstructorCallExpression cce = ConstructorNodeUtils.getFirstIfSpecialConstructorCall(mn.getCode());
        if (cce != null) {
            if (generated) {
                return;
            } else {
                // skip over this/super() call
                startingIndex = 1;
            }
        }
    }
    for (Parameter p : mn.getParameters()) {
        if (ClassHelper.isPrimitiveType(p.getType()))
            continue;
        newCode.getStatements().add(startingIndex, ifS(isNullX(varX(p)), makeThrowStmt(p.getName())));
    }
    mn.setCode(newCode);
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BytecodeSequence(org.codehaus.groovy.classgen.BytecodeSequence)

Example 2 with BytecodeSequence

use of org.codehaus.groovy.classgen.BytecodeSequence in project groovy by apache.

the class StaticTypesLambdaWriter method addDeserializeLambdaMethodForEachLambdaExpression.

private void addDeserializeLambdaMethodForEachLambdaExpression(final LambdaExpression expression, final ClassNode lambdaClass) {
    ClassNode enclosingClass = controller.getClassNode();
    Statement code = block(new BytecodeSequence(new BytecodeInstruction() {

        @Override
        public void visit(final MethodVisitor mv) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitInsn(ICONST_0);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/SerializedLambda", "getCapturedArg", "(I)Ljava/lang/Object;", false);
            mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(lambdaClass));
            OperandStack operandStack = controller.getOperandStack();
            operandStack.push(lambdaClass);
        }
    }), returnS(expression));
    enclosingClass.addSyntheticMethod(createDeserializeLambdaMethodName(lambdaClass), ACC_PUBLIC | ACC_STATIC, OBJECT_TYPE, createDeserializeLambdaMethodParams(), ClassNode.EMPTY_ARRAY, code);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) BytecodeInstruction(org.codehaus.groovy.classgen.BytecodeInstruction) BytecodeSequence(org.codehaus.groovy.classgen.BytecodeSequence) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)2 BytecodeSequence (org.codehaus.groovy.classgen.BytecodeSequence)2 ClassNode (org.codehaus.groovy.ast.ClassNode)1 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 Parameter (org.codehaus.groovy.ast.Parameter)1 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 BytecodeInstruction (org.codehaus.groovy.classgen.BytecodeInstruction)1 OperandStack (org.codehaus.groovy.classgen.asm.OperandStack)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1