Java Code Examples for org.codehaus.groovy.ast.ClassHelper#getWrapper()

The following examples show how to use org.codehaus.groovy.ast.ClassHelper#getWrapper() . 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: AnnotationConstantsVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression revertType(Expression val, ClassNode returnWrapperType) {
    ConstantExpression ce = (ConstantExpression) val;
    if (ClassHelper.Character_TYPE.equals(returnWrapperType) && ClassHelper.STRING_TYPE.equals(val.getType())) {
        return configure(val, Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) val));
    }
    ClassNode valWrapperType = ClassHelper.getWrapper(val.getType());
    if (ClassHelper.Integer_TYPE.equals(valWrapperType)) {
        Integer i = (Integer) ce.getValue();
        if (ClassHelper.Character_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression((char) i.intValue(), true));
        }
        if (ClassHelper.Short_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(i.shortValue(), true));
        }
        if (ClassHelper.Byte_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(i.byteValue(), true));
        }
    }
    if (ClassHelper.BigDecimal_TYPE.equals(valWrapperType)) {
        BigDecimal bd = (BigDecimal) ce.getValue();
        if (ClassHelper.Float_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(bd.floatValue(), true));
        }
        if (ClassHelper.Double_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(bd.doubleValue(), true));
        }
    }
    return null;
}
 
Example 2
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean matchWithOrWithourBoxing(final ClassNode argType, final Class aClass) {
    final boolean match;
    ClassNode type = ClassHelper.make(aClass);
    if (ClassHelper.isPrimitiveType(type) && !ClassHelper.isPrimitiveType(argType)) {
        type = ClassHelper.getWrapper(type);
    } else if (ClassHelper.isPrimitiveType(argType) && !ClassHelper.isPrimitiveType(type)) {
        type = ClassHelper.getUnwrapper(type);
    }
    match = argType.equals(type);
    return match;
}
 
Example 3
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) {
    ClassNode constType = constExpr.getType();
    ClassNode wrapperType = ClassHelper.getWrapper(constType);
    if (!hasCompatibleType(attrType, wrapperType)) {
        addError("Attribute '" + attrName + "' should have type '" + attrType.getName()
                + "'; but found type '" + constType.getName() + "'", constExpr);
    }
}
 
Example 4
Source File: InvokeDynamicWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(ClassNode from, ClassNode target) {
    ClassNode wrapper = ClassHelper.getWrapper(target);
    makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
    if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
        writeIndyCast(ClassHelper.OBJECT_TYPE,target);
    } else {
        BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
        controller.getOperandStack().replace(wrapper);
        controller.getOperandStack().doGroovyCast(target);
    }
}
 
Example 5
Source File: InvokeDynamicWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void castToNonPrimitiveIfNecessary(ClassNode sourceType, ClassNode targetType) {
    ClassNode boxedType = ClassHelper.getWrapper(sourceType);
    if (WideningCategories.implementsInterfaceOrSubclassOf(boxedType, targetType)) {
        controller.getOperandStack().box();
        return;
    }
    writeIndyCast(sourceType, targetType);
}
 
Example 6
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public ClassNode getType() {
    if (resolvedType != null) {
        return resolvedType;
    }
    ClassNode type;
    if (target instanceof ExtensionMethodNode) {
        type = ((ExtensionMethodNode) target).getExtensionMethodNode().getDeclaringClass();
    } else {
        type = ClassHelper.getWrapper(controller.getTypeChooser().resolveType(receiver, controller.getClassNode()));
        ClassNode declaringClass = target.getDeclaringClass();
        if (type.getClass() != ClassNode.class
                && type.getClass() != InnerClassNode.class
                && type.getClass() != DecompiledClassNode.class) {
            type = declaringClass; // ex: LUB type
        }
        if (ClassHelper.OBJECT_TYPE.equals(type) && !ClassHelper.OBJECT_TYPE.equals(declaringClass)) {
            // can happen for compiler rewritten code, where type information is missing
            type = declaringClass;
        }
        if (ClassHelper.OBJECT_TYPE.equals(declaringClass)) {
            // check cast not necessary because Object never evolves
            // and it prevents a potential ClassCastException if the delegate of a closure
            // is changed in a statically compiled closure
            type = ClassHelper.OBJECT_TYPE;
        }
    }
    resolvedType = type;
    return type;
}
 
Example 7
Source File: OperandStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
public ClassNode box() {
    MethodVisitor mv = controller.getMethodVisitor();
    int size = stack.size();
    ClassNode type = stack.get(size-1);
    if (ClassHelper.isPrimitiveType(type) && ClassHelper.VOID_TYPE!=type) {
        ClassNode wrapper = ClassHelper.getWrapper(type);
        BytecodeHelper.doCastToWrappedType(mv, type, wrapper);
        type = wrapper;
    } // else nothing to box
    stack.set(size-1, type);
    return type;
}
 
Example 8
Source File: CompileStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
public BytecodeVariable defineVariable(final Variable v, final ClassNode variableType, final boolean initFromStack) {
    String name = v.getName();
    BytecodeVariable answer = defineVar(name, variableType, v.isClosureSharedVariable(), v.isClosureSharedVariable());
    stackVariables.put(name, answer);

    MethodVisitor mv = controller.getMethodVisitor();
    Label startLabel  = new Label();
    answer.setStartLabel(startLabel);
    ClassNode type = answer.getType().redirect();
    OperandStack operandStack = controller.getOperandStack();

    if (!initFromStack) {
        if (ClassHelper.isPrimitiveType(v.getOriginType()) && ClassHelper.getWrapper(v.getOriginType()) == variableType) {
            pushInitValue(v.getOriginType(), mv);
            operandStack.push(v.getOriginType());
            operandStack.box();
            operandStack.remove(1);
        } else {
            pushInitValue(type, mv);
        }
    }
    operandStack.push(answer.getType());
    if (answer.isHolder())  {
        operandStack.box();
        operandStack.remove(1);
        createReference(answer);
    } else {
        operandStack.storeVar(answer);
    }

    mv.visitLabel(startLabel);
    return answer;
}
 
Example 9
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
static int getDistance(final ClassNode receiver, final ClassNode compare) {
    if (receiver.isArray() && compare.isArray()) {
        return getDistance(receiver.getComponentType(), compare.getComponentType());
    }
    int dist = 0;
    ClassNode unwrapReceiver = getUnwrapper(receiver);
    ClassNode unwrapCompare = getUnwrapper(compare);
    if (isPrimitiveType(unwrapReceiver)
            && isPrimitiveType(unwrapCompare)
            && unwrapReceiver != unwrapCompare) {
        dist = getPrimitiveDistance(unwrapReceiver, unwrapCompare);
    }
    // Add a penalty against boxing or unboxing, to get a resolution similar to JLS 15.12.2
    // (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2).
    if (isPrimitiveType(receiver) ^ isPrimitiveType(compare)) {
        dist = (dist + 1) << 1;
    }
    if (unwrapCompare.equals(unwrapReceiver)) return dist;
    if (receiver.isArray() && !compare.isArray()) {
        // Object[] vs Object
        dist += 256;
    }

    if (receiver == UNKNOWN_PARAMETER_TYPE) {
        return dist;
    }

    ClassNode ref = isPrimitiveType(receiver) && !isPrimitiveType(compare) ? ClassHelper.getWrapper(receiver) : receiver;
    while (ref != null) {
        if (compare.equals(ref)) {
            break;
        }
        if (compare.isInterface() && ref.implementsInterface(compare)) {
            dist += getMaximumInterfaceDistance(ref, compare);
            break;
        }
        ref = ref.getSuperClass();
        dist += 1;
        if (OBJECT_TYPE.equals(ref))
            dist += 1;
        dist = (dist + 1) << 1;
    }
    return dist;
}
 
Example 10
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void evaluateElvisOperatorExpression(final ElvisOperatorExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    TypeChooser typeChooser = controller.getTypeChooser();

    Expression boolPart = expression.getBooleanExpression().getExpression();
    Expression falsePart = expression.getFalseExpression();

    ClassNode truePartType = typeChooser.resolveType(boolPart, controller.getClassNode());
    ClassNode falsePartType = typeChooser.resolveType(falsePart, controller.getClassNode());
    ClassNode common = WideningCategories.lowestUpperBound(truePartType, falsePartType);

    // x?:y is equal to x?x:y, which evals to
    //      var t=x; boolean(t)?t:y
    // first we load x, dup it, convert the dupped to boolean, then
    // jump depending on the value. For true we are done, for false we
    // have to load y, thus we first remove x and then load y.
    // But since x and y may have different stack lengths, this cannot work
    // Thus we have to have to do the following:
    // Be X the type of x, Y the type of y and S the common supertype of
    // X and Y, then we have to see x?:y as
    //      var t=x;boolean(t)?S(t):S(y)
    // so we load x, dup it, store the value in a local variable (t), then
    // do boolean conversion. In the true part load t and cast it to S,
    // in the false part load y and cast y to S

    // load x, dup it, store one in $t and cast the remaining one to boolean
    int mark = operandStack.getStackLength();
    boolPart.visit(controller.getAcg());
    operandStack.dup();
    if (ClassHelper.isPrimitiveType(truePartType) && !ClassHelper.isPrimitiveType(operandStack.getTopOperand())) {
        truePartType = ClassHelper.getWrapper(truePartType);
    }
    int retValueId = compileStack.defineTemporaryVariable("$t", truePartType, true);
    operandStack.castToBool(mark, true);

    Label l0 = operandStack.jump(IFEQ);
    // true part: load $t and cast to S
    operandStack.load(truePartType, retValueId);
    operandStack.doGroovyCast(common);
    Label l1 = new Label();
    mv.visitJumpInsn(GOTO, l1);

    // false part: load false expression and cast to S
    mv.visitLabel(l0);
    falsePart.visit(controller.getAcg());
    operandStack.doGroovyCast(common);

    // finish and cleanup
    mv.visitLabel(l1);
    compileStack.removeVar(retValueId);
    operandStack.replace(common, 2);
}