Java Code Examples for org.codehaus.groovy.ast.expr.Expression#getNodeMetaData()

The following examples show how to use org.codehaus.groovy.ast.expr.Expression#getNodeMetaData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticTypesTypeChooser.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
    Expression target = exp instanceof VariableExpression && !((VariableExpression) exp).isClosureSharedVariable() ? getTarget((VariableExpression) exp) : exp;

    ClassNode inferredType = target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
    if (inferredType == null) {
        inferredType = target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    }
    if (inferredType != null && !ClassHelper.VOID_TYPE.equals(inferredType)) {
        return inferredType;
    }

    // AsmClassGenerator may create "this" expressions that the type checker knows nothing about
    if (AsmClassGenerator.isThisExpression(target)) {
        return current;
    }

    return super.resolveType(exp, current);
}
 
Example 2
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
protected void writePostOrPrefixMethod(final int op, final String method, final Expression expression, final Expression orig) {
    MethodNode mn = orig.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
    if (mn != null) {
        controller.getOperandStack().pop();
        MethodCallExpression call = callX(expression, method);
        call.setMethodTarget(mn);
        call.visit(controller.getAcg());
        return;
    }

    ClassNode top = controller.getOperandStack().getTopOperand();
    if (ClassHelper.isPrimitiveType(top) && (ClassHelper.isNumberType(top) || char_TYPE.equals(top))) {
        MethodVisitor mv = controller.getMethodVisitor();
        visitInsnByType(top, mv, ICONST_1, LCONST_1, FCONST_1, DCONST_1);
        if ("next".equals(method)) {
            visitInsnByType(top, mv, IADD, LADD, FADD, DADD);
        } else {
            visitInsnByType(top, mv, ISUB, LSUB, FSUB, DSUB);
        }
        return;
    }

    super.writePostOrPrefixMethod(op, method, expression, orig);
}
 
Example 3
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void makeSingleArgumentCall(final Expression receiver, final String message, final Expression arguments, final boolean safe) {
    TypeChooser typeChooser = controller.getTypeChooser();
    ClassNode classNode = controller.getClassNode();
    ClassNode rType = typeChooser.resolveType(receiver, classNode);
    ClassNode aType = typeChooser.resolveType(arguments, classNode);
    if (trySubscript(receiver, message, arguments, rType, aType, safe)) {
        return;
    }
    // now try with flow type instead of declaration type
    rType = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    if (receiver instanceof VariableExpression && rType == null) {
        // TODO: can STCV be made smarter to avoid this check?
        VariableExpression ve = (VariableExpression) ((VariableExpression)receiver).getAccessedVariable();
        rType = ve.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    }
    if (rType!=null && trySubscript(receiver, message, arguments, rType, aType, safe)) {
        return;
    }
    // todo: more cases
    throw new GroovyBugError(
            "At line " + receiver.getLineNumber() + " column " + receiver.getColumnNumber() + "\n" +
            "On receiver: " + receiver.getText() + " with message: " + message + " and arguments: " + arguments.getText() + "\n" +
            "This method should not have been called. Please try to create a simple example reproducing\n" +
            "this error and file a bug report at https://issues.apache.org/jira/browse/GROOVY");
}
 
Example 4
Source File: MacroClassTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp instanceof ConstructorCallExpression) {
        MethodCallExpression call = exp.getNodeMetaData(MacroTransformation.class);
        if (call != null) {
            return call;
        }
    }
    return super.transform(exp);
}
 
Example 5
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp != null) {
        Expression replacement = exp.getNodeMetaData(TraitASTTransformation.POST_TYPECHECKING_REPLACEMENT);
        if (replacement!=null) {
            return replacement;
        }
    }
    return super.transform(exp);
}
 
Example 6
Source File: StatementMetaTypeChooser.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
    ClassNode type = null;
    if (exp instanceof ClassExpression) { type = exp.getType();
        ClassNode classType = ClassHelper.makeWithoutCaching("java.lang.Class");
        classType.setGenericsTypes(new GenericsType[] {new GenericsType(type)});
        classType.setRedirect(ClassHelper.CLASS_Type);
        return classType;
    }

    OptimizingStatementWriter.StatementMeta meta = exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
    if (meta != null) type = meta.type;
    if (type != null) return type;

    if (exp instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) exp;
        if (ve.isClosureSharedVariable()) return ve.getType();
        if (ve.isSuperExpression()) return current.getSuperClass();

        type = ve.getOriginType();
    } else if (exp instanceof Variable) {
        Variable v = (Variable) exp;
        type = v.getOriginType();
    } else {
        type = exp.getType();
    }
    return type.redirect();
}
 
Example 7
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean makeDirectCall(final Expression origin, final Expression receiver, final Expression message, final Expression arguments, final MethodCallerMultiAdapter adapter, final boolean implicitThis, final boolean containsSpreadExpression) {
    if (origin instanceof MethodCallExpression && isSuperExpression(receiver)) {
        ClassNode superClass = receiver.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
        if (superClass != null && !controller.getCompileStack().isLHS()) {
            // GROOVY-7300
            MethodCallExpression mce = (MethodCallExpression) origin;
            MethodNode node = superClass.getDeclaredMethod(mce.getMethodAsString(), Parameter.EMPTY_ARRAY);
            mce.setMethodTarget(node);
        }
    }
    return super.makeDirectCall(origin, receiver, message, arguments, adapter, implicitThis, containsSpreadExpression);
}
 
Example 8
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean tryImplicitReceiver(final Expression origin, final Expression message, final Expression arguments, final MethodCallerMultiAdapter adapter, final boolean safe, final boolean spreadSafe) {
    Object implicitReceiver = origin.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (implicitReceiver == null && origin instanceof MethodCallExpression) {
        implicitReceiver = ((MethodCallExpression) origin).getObjectExpression().getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    }
    if (implicitReceiver != null) {
        String[] path = ((String) implicitReceiver).split("\\.");
        // GROOVY-6021
        PropertyExpression pexp = propX(varX("this", ClassHelper.CLOSURE_TYPE), path[0]);
        pexp.setImplicitThis(true);
        for (int i = 1, n = path.length; i < n; i += 1) {
            pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.CLOSURE_TYPE);
            pexp = propX(pexp, path[i]);
        }
        pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, implicitReceiver);
        origin.removeNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
        if (origin instanceof PropertyExpression) {
            PropertyExpression rewritten = propX(pexp, ((PropertyExpression) origin).getProperty(), ((PropertyExpression) origin).isSafe());
            rewritten.setSpreadSafe(((PropertyExpression) origin).isSpreadSafe());
            rewritten.visit(controller.getAcg());

            rewritten.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, origin.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE));
        } else {
            makeCall(origin, pexp, message, arguments, adapter, safe, spreadSafe, false);
        }
        return true;
    }
    return false;
}
 
Example 9
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void makeGroovyObjectGetPropertySite(final Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
    ClassNode receiverType = controller.getClassNode();
    if (!AsmClassGenerator.isThisExpression(receiver) || controller.isInGeneratedFunction()) {
        receiverType = controller.getTypeChooser().resolveType(receiver, receiverType);
    }

    String property = propertyName;
    if (implicitThis && controller.getInvocationWriter() instanceof StaticInvocationWriter) {
        Expression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
        if (currentCall != null && currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) != null) {
            property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
            String[] props = property.split("\\.");
            BytecodeExpression thisLoader = bytecodeX(CLOSURE_TYPE, mv -> mv.visitVarInsn(ALOAD, 0));
            PropertyExpression pexp = propX(thisLoader, constX(props[0]), safe);
            for (int i = 1, n = props.length; i < n; i += 1) {
                pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, CLOSURE_TYPE);
                pexp = propX(pexp, props[i]);
            }
            pexp.visit(controller.getAcg());
            return;
        }
    }

    if (makeGetPropertyWithGetter(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;

    MethodCallExpression call = callX(receiver, "getProperty", args(constX(property)));
    call.setImplicitThis(implicitThis);
    call.setMethodTarget(GROOVYOBJECT_GETPROPERTY_METHOD);
    call.setSafe(safe);
    call.visit(controller.getAcg());
}
 
Example 10
Source File: AbstractFunctionalInterfaceWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
default ClassNode getFunctionalInterfaceType(Expression expression) {
    ClassNode type = expression.getNodeMetaData(PARAMETER_TYPE);

    if (null == type) {
        type = expression.getNodeMetaData(INFERRED_FUNCTIONAL_INTERFACE_TYPE);
    }
    return type;
}
 
Example 11
Source File: TypeCheckingExtension.java    From groovy with Apache License 2.0 4 votes vote down vote up
public MethodNode getTargetMethod(final Expression expression) {
    return (MethodNode) expression.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
}
 
Example 12
Source File: TryCatchStatement.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static boolean isResource(Expression expression) {
    Boolean r = expression.getNodeMetaData(IS_RESOURCE);
    return null != r && r;
}
 
Example 13
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void writeListDotProperty(final Expression receiver, final String propertyName, final MethodVisitor mv, final boolean safe) {
    ClassNode componentType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.COMPONENT_TYPE);
    if (componentType == null) {
        componentType = OBJECT_TYPE;
    }
    // for lists, replace list.foo with:
    // def result = new ArrayList(list.size())
    // for (e in list) { result.add (e.foo) }
    // result
    CompileStack compileStack = controller.getCompileStack();

    Label exit = new Label();
    if (safe) {
        receiver.visit(controller.getAcg());
        Label doGet = new Label();
        mv.visitJumpInsn(IFNONNULL, doGet);
        controller.getOperandStack().remove(1);
        mv.visitInsn(ACONST_NULL);
        mv.visitJumpInsn(GOTO, exit);
        mv.visitLabel(doGet);
    }

    Variable tmpList = varX("tmpList", ClassHelper.make(ArrayList.class));
    int var = compileStack.defineTemporaryVariable(tmpList, false);
    Variable iterator = varX("iterator", Iterator_TYPE);
    int it = compileStack.defineTemporaryVariable(iterator, false);
    Variable nextVar = varX("next", componentType);
    final int next = compileStack.defineTemporaryVariable(nextVar, false);

    mv.visitTypeInsn(NEW, "java/util/ArrayList");
    mv.visitInsn(DUP);
    receiver.visit(controller.getAcg());
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
    controller.getOperandStack().remove(1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false);
    mv.visitVarInsn(ASTORE, var);
    Label l1 = new Label();
    mv.visitLabel(l1);
    receiver.visit(controller.getAcg());
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true);
    controller.getOperandStack().remove(1);
    mv.visitVarInsn(ASTORE, it);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, it);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
    Label l3 = new Label();
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitVarInsn(ALOAD, it);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
    mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(componentType));
    mv.visitVarInsn(ASTORE, next);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, var);
    PropertyExpression pexp = propX(
            bytecodeX(componentType, v -> v.visitVarInsn(ALOAD, next)),
            propertyName
    );
    pexp.visit(controller.getAcg());
    controller.getOperandStack().box();
    controller.getOperandStack().remove(1);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
    mv.visitInsn(POP);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l3);
    mv.visitVarInsn(ALOAD, var);
    if (safe) {
        mv.visitLabel(exit);
    }
    controller.getOperandStack().push(ClassHelper.make(ArrayList.class));
    controller.getCompileStack().removeVar(next);
    controller.getCompileStack().removeVar(it);
    controller.getCompileStack().removeVar(var);
}