Java Code Examples for org.codehaus.groovy.ast.ClassNode#redirect()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#redirect() . 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: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void printDefaultValue(PrintWriter out, ClassNode type) {
    if (type.redirect() != ClassHelper.OBJECT_TYPE) {
        out.print("(");
        printType(type, out);
        out.print(")");
    }

    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("false");
        } else {
            out.print("0");
        }
    } else {
        out.print("null");
    }
}
 
Example 2
Source File: GenericsUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static Map<GenericsType, GenericsType> makePlaceholderAndParameterizedTypeMap(ClassNode declaringClass) {
    if (null == declaringClass) {
        return Collections.emptyMap();
    }

    Map<GenericsType, GenericsType> result = new LinkedHashMap<>();

    ClassNode redirectDeclaringClass = declaringClass.redirect();
    GenericsType[] declaringGenericsTypes = declaringClass.getGenericsTypes();
    GenericsType[] redirectDeclaringGenericsTypes = redirectDeclaringClass.getGenericsTypes();

    if (null != declaringGenericsTypes && null != redirectDeclaringGenericsTypes) {
        for (int i = 0, n = declaringGenericsTypes.length; i < n; i++) {
            result.put(redirectDeclaringGenericsTypes[i], declaringGenericsTypes[i]);
        }
    }

    return result;
}
 
Example 3
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Direct access is allowed from the declaring class of the field and sometimes from inner and peer types.
 *
 * @return {@code true} if GETFIELD or GETSTATIC is safe for given field and receiver
 */
private static boolean isDirectAccessAllowed(final FieldNode field, final ClassNode receiver) {
    // first, direct access from anywhere for public fields
    if (field.isPublic()) return true;

    ClassNode declaringType = field.getDeclaringClass().redirect(), receiverType = receiver.redirect();

    // next, direct access from within the declaring class
    if (receiverType.equals(declaringType)) return true;

    if (field.isPrivate()) return false;

    // next, direct access from within the declaring package
    if (Objects.equals(receiver.getPackageName(), declaringType.getPackageName())) return true;

    // last, inner class access to outer class fields
    receiverType = receiverType.getOuterClass();
    while (receiverType != null) {
        if (receiverType.equals(declaringType)) {
            return true;
        }
        receiverType = receiverType.getOuterClass();
    }
    return false;
}
 
Example 4
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void printDefaultValue(PrintWriter out, ClassNode type) {
    if (type.redirect() != ClassHelper.OBJECT_TYPE && type.redirect() != ClassHelper.boolean_TYPE) {
        out.print("(");
        printType(out, type);
        out.print(")");
    }

    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("false");
        } else {
            out.print("0");
        }
    } else {
        out.print("null");
    }
}
 
Example 5
Source File: FindMethodUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Find and add method usage if the given type contains method corresponding
 * with the given method call. In other words we are looking for the method
 * declaration in the given type and the method we are looking for is based
 * on given method call. Might return null if the type can't be interfered
 * properly and thus we are not able to find correct method - this is typical
 * for dynamic types.
 *
 * @param type where we are looking for the specific method
 * @param methodCall method call for which we want to find method in given
 * type or null if the type is dynamic
 */
private static MethodNode findMethod(ClassNode type, MethodCallExpression methodCall) {
    String findingMethod = methodCall.getMethodAsString();
    Expression arguments = methodCall.getArguments();
    
    if (!type.isResolved()) {
        type = type.redirect();
    }

    MethodNode method = type.tryFindPossibleMethod(findingMethod, arguments);
    if (method != null) {
        return method;
    }
    return findMostAccurateMethod(methodCall, type.getMethods(findingMethod));
}
 
Example 6
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void addMissingEntries(final Map<GenericsTypeName, GenericsType> connections, final Map<GenericsTypeName, GenericsType> resolved) {
    for (Map.Entry<GenericsTypeName, GenericsType> entry : connections.entrySet()) {
        if (resolved.containsKey(entry.getKey())) continue;
        GenericsType gt = entry.getValue();
        ClassNode cn = gt.getType();
        if (cn.redirect() == UNKNOWN_PARAMETER_TYPE) continue;
        resolved.put(entry.getKey(), gt);
    }
}
 
Example 7
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ClassNode resolveClassNodeGenerics(Map<GenericsTypeName, GenericsType> resolvedPlaceholders, final Map<GenericsTypeName, GenericsType> placeholdersFromContext, final ClassNode currentType) {
    ClassNode target = currentType.redirect();
    resolvedPlaceholders = new HashMap<>(resolvedPlaceholders);
    applyContextGenerics(resolvedPlaceholders, placeholdersFromContext);

    Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
    extractGenericsConnections(connections, currentType, target);
    applyGenericsConnections(connections, resolvedPlaceholders);
    return applyGenericsContext(resolvedPlaceholders, currentType);
}
 
Example 8
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Apply the bounds from the declared type when the using type simply declares a parameter as an unbounded wildcard.
 *
 * @param type A parameterized type
 * @return A parameterized type with more precise wildcards
 */
static ClassNode boundUnboundedWildcards(final ClassNode type) {
    if (type.isArray()) {
        return boundUnboundedWildcards(type.getComponentType()).makeArray();
    }
    ClassNode target = type.redirect();
    if (target == null || type == target || !isUsingGenericsOrIsArrayUsingGenerics(target)) return type;
    ClassNode newType = type.getPlainNodeReference();
    newType.setGenericsPlaceHolder(type.isGenericsPlaceHolder());
    newType.setGenericsTypes(boundUnboundedWildcards(type.getGenericsTypes(), target.getGenericsTypes()));
    return newType;
}
 
Example 9
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 10
Source File: BytecodeHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * array types are special:
 * eg.: String[]: classname: [Ljava/lang/String;
 * int[]: [I
 *
 * @return the ASM type description
 */
private static String getTypeDescription(ClassNode c, boolean end) {
    ClassNode d = c;
    if (ClassHelper.isPrimitiveType(d.redirect())) {
        d = d.redirect();
    }
    String desc = TypeUtil.getDescriptionByType(d);
    if (!end && desc.endsWith(";")) {
        desc = desc.substring(0, desc.length() - 1);
    }
    return desc;
}
 
Example 11
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected static boolean isPrivateBridgeMethodsCallAllowed(final ClassNode receiver, final ClassNode caller) {
    if (receiver == null) return false;
    if (receiver.redirect() == caller) return true;
    if (isPrivateBridgeMethodsCallAllowed(receiver.getOuterClass(), caller)) return true;
    if (caller.getOuterClass() != null && isPrivateBridgeMethodsCallAllowed(receiver, caller.getOuterClass())) return true;
    return false;
}
 
Example 12
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean isCandidateTraitMethod(ClassNode trait, MethodNode traitMethod) {
    boolean precompiled = trait.redirect() instanceof DecompiledClassNode;
    if (!precompiled) return !traitMethod.isAbstract();
    List<MethodNode> helperMethods = Traits.findHelper(trait).getMethods();
    for (MethodNode helperMethod : helperMethods) {
        boolean isSynthetic = (traitMethod.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0;
        if (helperMethod.getName().equals(traitMethod.getName()) && !isSynthetic && !traitMethod.getName().contains("$")) {
            Parameter[] origParams = helperMethod.getParameters();
            Parameter[] newParams = Arrays.copyOfRange(origParams, 1, origParams.length);
            if (sameParameterTypes(newParams, traitMethod.getParameters())) return true;
        }
    }
    return false;
}
 
Example 13
Source File: Java8.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static GenericsType configureTypeVariableDefinition(ClassNode base, ClassNode[] cBounds) {
    ClassNode redirect = base.redirect();
    base.setRedirect(null);
    GenericsType gt;
    if (cBounds == null || cBounds.length == 0) {
        gt = new GenericsType(base);
    } else {
        gt = new GenericsType(base, cBounds, null);
        gt.setName(base.getName());
        gt.setPlaceholder(true);
    }
    base.setRedirect(redirect);
    return gt;
}
 
Example 14
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final ClassNode right, final Expression rightExpression, final boolean allowConstructorCoercion) {
    ClassNode leftRedirect = left.redirect();
    ClassNode rightRedirect = right.redirect();
    if (leftRedirect == rightRedirect) return true;

    if (leftRedirect.isArray() && rightRedirect.isArray()) {
        return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType(), rightExpression, false);
    }

    if (right == VOID_TYPE || right == void_WRAPPER_TYPE) {
        return left == VOID_TYPE || left == void_WRAPPER_TYPE;
    }

    if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
        if (BigDecimal_TYPE == leftRedirect) {
            // any number can be assigned to a big decimal
            return true;
        }
        if (BigInteger_TYPE == leftRedirect) {
            return WideningCategories.isBigIntCategory(getUnwrapper(rightRedirect)) || rightRedirect.isDerivedFrom(BigInteger_TYPE);
        }
    }

    // if rightExpression is null and leftExpression is not a primitive type, it's ok
    boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
    if (rightExpressionIsNull && !isPrimitiveType(left)) {
        return true;
    }

    // on an assignment everything that can be done by a GroovyCast is allowed

    // anything can be assigned to an Object, String, Boolean or Class typed variable
    if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;

    // char as left expression
    if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
        if (rightExpression instanceof ConstantExpression) {
            String value = rightExpression.getText();
            return value.length() == 1;
        }
    }
    if (leftRedirect == Character_TYPE && (rightRedirect == STRING_TYPE || rightExpressionIsNull)) {
        return rightExpressionIsNull || (rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1);
    }

    // if left is Enum and right is String or GString we do valueOf
    if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == GSTRING_TYPE || rightRedirect == STRING_TYPE)) {
        return true;
    }

    // if right is array, map or collection we try invoking the constructor
    if (allowConstructorCoercion && isGroovyConstructorCompatible(rightExpression)) {
        // TODO: in case of the array we could maybe make a partial check
        if (leftRedirect.isArray() && rightRedirect.isArray()) {
            return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType());
        } else if (rightRedirect.isArray() && !leftRedirect.isArray()) {
            return false;
        }
        return true;
    }

    // simple check on being subclass
    if (right.isDerivedFrom(left) || (left.isInterface() && right.implementsInterface(left))) return true;

    // if left and right are primitives or numbers allow
    if (isPrimitiveType(leftRedirect) && isPrimitiveType(rightRedirect)) return true;
    if (isNumberType(leftRedirect) && isNumberType(rightRedirect)) return true;

    // left is a float/double and right is a BigDecimal
    if (WideningCategories.isFloatingCategory(leftRedirect) && BigDecimal_TYPE.equals(rightRedirect)) {
        return true;
    }

    if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
        return true;
    }

    if (left.isGenericsPlaceHolder()) {
        // GROOVY-7307
        GenericsType[] genericsTypes = left.getGenericsTypes();
        if (genericsTypes != null && genericsTypes.length == 1) {
            // should always be the case, but safe guard is better
            return genericsTypes[0].isCompatibleWith(right);
        }
    }

    // GROOVY-7316: It is an apparently legal thing to allow this. It's not type safe, but it is allowed...
    return right.isGenericsPlaceHolder();
}
 
Example 15
Source File: BooleanExpressionTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
public OptimizingBooleanExpression(final Expression expression, final ClassNode type) {
    super(expression);
    this.expression = expression;
    // we must use the redirect node, otherwise InnerClassNode would not have the "correct" type
    this.type = type.redirect();
}
 
Example 16
Source File: OperandStack.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void doConvertAndCast(ClassNode targetType, boolean coerce) {
    int size = stack.size();
    throwExceptionForNoStackElement(size, targetType, coerce);

    ClassNode top = stack.get(size-1);
    targetType = targetType.redirect();
    if (targetType == top) return;

    if (coerce) {
        controller.getInvocationWriter().coerce(top,targetType);
        return;
    }

    boolean primTarget = ClassHelper.isPrimitiveType(targetType);
    boolean primTop = ClassHelper.isPrimitiveType(top);

    if (primTop && primTarget) {
        // here we box and unbox to get the goal type
        if (convertPrimitive(top, targetType)) {
            replace(targetType);
            return;
        }
        box();
    } else if (primTarget) {
        // top is not primitive so unbox
        // leave that BH#doCast later
    } else {
        // top might be primitive, target is not
        // so let invocation writer box if needed and do groovy cast otherwise
        controller.getInvocationWriter().castToNonPrimitiveIfNecessary(top, targetType);
    }

    MethodVisitor mv = controller.getMethodVisitor();
    if (primTarget && !ClassHelper.boolean_TYPE.equals(targetType) && !primTop && ClassHelper.getWrapper(targetType).equals(top)) {
        BytecodeHelper.doCastToPrimitive(mv, top, targetType);
    } else {
        top = stack.get(size-1);
        if (!WideningCategories.implementsInterfaceOrSubclassOf(top, targetType)) {
            BytecodeHelper.doCast(mv,targetType);
        }
    }
    replace(targetType);
}