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

The following examples show how to use org.codehaus.groovy.ast.ClassHelper#makeWithoutCaching() . 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: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassNode deriveExpressonType(Expression expression) {
    ClassNode derivedExpressionType = null;
    if (expression instanceof ConstantExpression
            && !expression.getText().equals("null")) { // NOI18N
        derivedExpressionType = ((ConstantExpression) expression).getType();
    } else if (expression instanceof ConstructorCallExpression) {
        derivedExpressionType = ((ConstructorCallExpression) expression).getType();
    } else if (expression instanceof MethodCallExpression) {
        int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
        AstPath newPath = new AstPath(path.root(), newOffset, doc);
        derivedExpressionType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
    } else if (expression instanceof StaticMethodCallExpression) {
        derivedExpressionType = MethodInference.findCallerType(expression, path, doc, cursorOffset);
    } else if (expression instanceof ListExpression) {
        derivedExpressionType = ((ListExpression) expression).getType();
    } else if (expression instanceof MapExpression) {
        derivedExpressionType = ((MapExpression) expression).getType();
    } else if (expression instanceof RangeExpression) {
        // this should work, but the type is Object - nut sure why
        // guessedType = ((RangeExpression)initialExpression).getType();
        derivedExpressionType = ClassHelper.makeWithoutCaching(Range.class, true);                
    } 
    return derivedExpressionType;
}
 
Example 2
Source File: Java8.java    From groovy with Apache License 2.0 6 votes vote down vote up
private ClassNode configureWildcardType(final WildcardType wildcardType) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    base.setRedirect(ClassHelper.OBJECT_TYPE);

    ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds());
    ClassNode[] uppers = configureTypes(wildcardType.getUpperBounds());
    // beware of [Object] upper bounds; often it's <?> or <? super T>
    if (lowers != null || wildcardType.getTypeName().equals("?")) {
        uppers = null;
    }

    GenericsType gt = new GenericsType(base, uppers, lowers != null ? lowers[0] : null);
    gt.setWildcard(true);

    ClassNode wt = ClassHelper.makeWithoutCaching(Object.class, false);
    wt.setGenericsTypes(new GenericsType[]{gt});
    return wt;
}
 
Example 3
Source File: Java8.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression annotationValueToExpression (Object value) {
    if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
        return new ConstantExpression(value);

    if (value instanceof Class)
        return new ClassExpression(ClassHelper.makeWithoutCaching((Class<?>)value));

    if (value.getClass().isArray()) {
        ListExpression elementExprs = new ListExpression();
        int len = Array.getLength(value);
        for (int i = 0; i != len; ++i)
            elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
        return elementExprs;
    }

    return null;
}
 
Example 4
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression expression) {
        if (expression.isSuperExpression()) {
            guessedType = expression.getType().getSuperClass();
        }
        if (null != expression.getAccessedVariable()) {
            Variable accessedVariable = expression.getAccessedVariable();

            if (accessedVariable.hasInitialExpression()) {
                Expression initialExpression = expression.getAccessedVariable().getInitialExpression();
                if (initialExpression instanceof ConstantExpression
                        && !initialExpression.getText().equals("null")) { // NOI18N
                    guessedType = ((ConstantExpression) initialExpression).getType();
                } else if (initialExpression instanceof ConstructorCallExpression) {
                    guessedType = ClassHelper.make(((ConstructorCallExpression) initialExpression).getType().getName());
                } else if (initialExpression instanceof MethodCallExpression) {
                    int newOffset = ASTUtils.getOffset(doc, initialExpression.getLineNumber(), initialExpression.getColumnNumber());
                    AstPath newPath = new AstPath(path.root(), newOffset, doc);
                    guessedType = MethodInference.findCallerType(initialExpression, newPath, doc, newOffset);
                } else if (initialExpression instanceof ListExpression) {
                    guessedType = ((ListExpression) initialExpression).getType();
                } else if (initialExpression instanceof MapExpression) {
                    guessedType = ((MapExpression) initialExpression).getType();
                } else if (initialExpression instanceof RangeExpression) {
                    // this should work, but the type is Object - nut sure why
                    // guessedType = ((RangeExpression)initialExpression).getType();
                    guessedType = ClassHelper.makeWithoutCaching(Range.class, true);                
                }
            } else if (accessedVariable instanceof Parameter) {
                Parameter param = (Parameter) accessedVariable;
                guessedType = param.getType();
            }
        } else if (!expression.getType().getName().equals("java.lang.Object")) {
            guessedType = expression.getType();

        }
    super.visitVariableExpression(expression);
}
 
Example 5
Source File: TypeSignatureParser.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static GenericsType createWildcard(final ClassNode[] upper, final ClassNode lower) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    base.setRedirect(ClassHelper.OBJECT_TYPE);
    GenericsType t = new GenericsType(base, upper, lower);
    t.setWildcard(true);
    return t;
}
 
Example 6
Source File: GenericsUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ClassNode nonGeneric(ClassNode type) {
    if (type.isUsingGenerics()) {
        final ClassNode nonGen = ClassHelper.makeWithoutCaching(type.getName());
        nonGen.setRedirect(type);
        nonGen.setGenericsTypes(null);
        nonGen.setUsingGenerics(false);
        return nonGen;
    }
    if (type.isArray() && type.getComponentType().isUsingGenerics()) {
        return type.getComponentType().getPlainNodeReference().makeArray();
    }
    return type;
}
 
Example 7
Source File: GenericsUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Map<String, ClassNode> addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec) {
    Map<String, ClassNode> newSpec = new HashMap<>(oldSpec);
    GenericsType[] gts = current.getGenericsTypes();
    if (gts != null) {
        for (GenericsType gt : gts) {
            String name = gt.getName();
            ClassNode type = gt.getType();
            if (gt.isPlaceholder()) {
                ClassNode redirect;
                if (gt.getUpperBounds() != null) {
                    redirect = gt.getUpperBounds()[0];
                } else if (gt.getLowerBound() != null) {
                    redirect = gt.getLowerBound();
                } else {
                    redirect = ClassHelper.OBJECT_TYPE;
                }
                if (redirect.isGenericsPlaceHolder()) {
                    // "T extends U" or "T super U"
                    type = redirect;
                } else {
                    // "T" or "T extends Thing" or "T super Thing"
                    type = ClassHelper.makeWithoutCaching(name);
                    type.setGenericsPlaceHolder(true);
                    type.setRedirect(redirect);
                }
            }
            newSpec.put(name, type);
        }
    }
    return newSpec;
}
 
Example 8
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 9
Source File: Java8.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode configureClass(Class<?> c) {
    if (c.isPrimitive()) {
        return ClassHelper.make(c);
    } else {
        return ClassHelper.makeWithoutCaching(c, false);
    }
}
 
Example 10
Source File: Java8.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ClassNode configureTypeVariableReference(String name) {
    ClassNode cn = ClassHelper.makeWithoutCaching(name);
    cn.setGenericsPlaceHolder(true);
    ClassNode cn2 = ClassHelper.makeWithoutCaching(name);
    cn2.setGenericsPlaceHolder(true);
    GenericsType[] gts = new GenericsType[]{new GenericsType(cn2)};
    cn.setGenericsTypes(gts);
    cn.setRedirect(ClassHelper.OBJECT_TYPE);
    return cn;
}
 
Example 11
Source File: GenericsUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Given a parameterized type and a generic type information, aligns actual type parameters. For example, if a
 * class uses generic type <pre>&lt;T,U,V&gt;</pre> (redirectGenericTypes), is used with actual type parameters
 * <pre>&lt;java.lang.String, U,V&gt;</pre>, then a class or interface using generic types <pre>&lt;T,V&gt;</pre>
 * will be aligned to <pre>&lt;java.lang.String,V&gt;</pre>
 *
 * @param redirectGenericTypes the type arguments or the redirect class node
 * @param parameterizedTypes   the actual type arguments used on this class node
 * @param alignmentTarget      the generic type arguments to which we want to align to
 * @return aligned type arguments
 * @deprecated You shouldn't call this method because it is inherently unreliable
 */
@Deprecated
public static GenericsType[] alignGenericTypes(final GenericsType[] redirectGenericTypes, final GenericsType[] parameterizedTypes, final GenericsType[] alignmentTarget) {
    if (alignmentTarget == null) return EMPTY_GENERICS_ARRAY;
    if (parameterizedTypes == null || parameterizedTypes.length == 0) return alignmentTarget;
    GenericsType[] generics = new GenericsType[alignmentTarget.length];
    for (int i = 0, scgtLength = alignmentTarget.length; i < scgtLength; i++) {
        final GenericsType currentTarget = alignmentTarget[i];
        GenericsType match = null;
        if (redirectGenericTypes != null) {
            for (int j = 0; j < redirectGenericTypes.length && match == null; j++) {
                GenericsType redirectGenericType = redirectGenericTypes[j];
                if (redirectGenericType.isCompatibleWith(currentTarget.getType())) {
                    if (currentTarget.isPlaceholder() && redirectGenericType.isPlaceholder() && !currentTarget.getName().equals(redirectGenericType.getName())) {
                        // check if there's a potential better match
                        boolean skip = false;
                        for (int k = j + 1; k < redirectGenericTypes.length && !skip; k++) {
                            GenericsType ogt = redirectGenericTypes[k];
                            if (ogt.isPlaceholder() && ogt.isCompatibleWith(currentTarget.getType()) && ogt.getName().equals(currentTarget.getName())) {
                                skip = true;
                            }
                        }
                        if (skip) continue;
                    }
                    match = parameterizedTypes[j];
                    if (currentTarget.isWildcard()) {
                        // if alignment target is a wildcard type
                        // then we must make best effort to return a parameterized
                        // wildcard
                        ClassNode lower = currentTarget.getLowerBound() != null ? match.getType() : null;
                        ClassNode[] currentUpper = currentTarget.getUpperBounds();
                        ClassNode[] upper = currentUpper != null ? new ClassNode[currentUpper.length] : null;
                        if (upper != null) {
                            for (int k = 0; k < upper.length; k++) {
                                upper[k] = currentUpper[k].isGenericsPlaceHolder() ? match.getType() : currentUpper[k];
                            }
                        }
                        match = new GenericsType(ClassHelper.makeWithoutCaching("?"), upper, lower);
                        match.setWildcard(true);
                    }
                }
            }
        }
        if (match == null) {
            match = currentTarget;
        }
        generics[i] = match;
    }
    return generics;
}
 
Example 12
Source File: GenericsUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions) {
    if (type.isArray()) {
        return correctToGenericsSpecRecurse(genericsSpec, type.getComponentType(), exclusions).makeArray();
    }
    if (type.isGenericsPlaceHolder() && !exclusions.contains(type.getUnresolvedName())) {
        String name = type.getGenericsTypes()[0].getName();
        exclusions = plus(exclusions, name); // GROOVY-7722
        type = genericsSpec.get(name);
        if (type != null && type.isGenericsPlaceHolder()) {
            if (type.getGenericsTypes() == null) {
                ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
                placeholder.setGenericsPlaceHolder(true);
                return makeClassSafeWithGenerics(type, new GenericsType(placeholder));
            } else if (!name.equals(type.getUnresolvedName())) {
                return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
            }
        }
    }
    if (type == null) type = ClassHelper.OBJECT_TYPE;
    GenericsType[] oldgTypes = type.getGenericsTypes();
    GenericsType[] newgTypes = EMPTY_GENERICS_ARRAY;
    if (oldgTypes != null) {
        newgTypes = new GenericsType[oldgTypes.length];
        for (int i = 0; i < newgTypes.length; i++) {
            GenericsType oldgType = oldgTypes[i];
            if (oldgType.isWildcard()) {
                ClassNode[] oldUpper = oldgType.getUpperBounds();
                ClassNode[] upper = null;
                if (oldUpper != null) {
                    // correct "? extends T" or "? extends T & I"
                    upper = new ClassNode[oldUpper.length];
                    for (int j = 0; j < oldUpper.length; j++) {
                        upper[j] = correctToGenericsSpecRecurse(genericsSpec, oldUpper[j], exclusions);
                    }
                }
                ClassNode oldLower = oldgType.getLowerBound();
                ClassNode lower = null;
                if (oldLower != null) {
                    // correct "? super T"
                    lower = correctToGenericsSpecRecurse(genericsSpec, oldLower, exclusions);
                }
                GenericsType fixed = new GenericsType(oldgType.getType(), upper, lower);
                fixed.setWildcard(true);
                newgTypes[i] = fixed;
            } else if (oldgType.isPlaceholder()) {
                // correct "T"
                newgTypes[i] = new GenericsType(genericsSpec.getOrDefault(oldgType.getName(), ClassHelper.OBJECT_TYPE));
            } else {
                // correct "List<T>", etc.
                newgTypes[i] = new GenericsType(correctToGenericsSpecRecurse(genericsSpec, correctToGenericsSpec(genericsSpec, oldgType), exclusions));
            }
        }
    }
    return makeClassSafeWithGenerics(type, newgTypes);
}
 
Example 13
Source File: InitializerStrategy.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static GenericsType makePlaceholder(int i) {
    ClassNode type = ClassHelper.makeWithoutCaching("T" + i);
    type.setRedirect(OBJECT_TYPE);
    type.setGenericsPlaceHolder(true);
    return new GenericsType(type);
}
 
Example 14
Source File: GenericsUtils.java    From groovy with Apache License 2.0 3 votes vote down vote up
/**
 * Generates a wildcard generic type in order to be used for checks against class nodes.
 * See {@link GenericsType#isCompatibleWith(org.codehaus.groovy.ast.ClassNode)}.
 *
 * @param types the type to be used as the wildcard upper bound
 * @return a wildcard generics type
 */
public static GenericsType buildWildcardType(final ClassNode... types) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    GenericsType gt = new GenericsType(base, types, null);
    gt.setWildcard(true);
    return gt;
}