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

The following examples show how to use org.codehaus.groovy.ast.ClassNode#setGenericsPlaceHolder() . 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: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final ClassNode bound) {
    if (bound == null) return null;
    if (bound.isArray()) {
        return applyGenericsContext(spec, bound.getComponentType()).makeArray();
    }
    if (!bound.isUsingGenerics()) return bound;
    ClassNode newBound = bound.getPlainNodeReference();
    newBound.setGenericsTypes(applyGenericsContext(spec, bound.getGenericsTypes()));
    if (bound.isGenericsPlaceHolder()) {
        GenericsType[] gt = newBound.getGenericsTypes();
        boolean hasBounds = hasNonTrivialBounds(gt[0]);
        if (hasBounds || !gt[0].isPlaceholder()) return getCombinedBoundType(gt[0]);
        String placeHolderName = newBound.getGenericsTypes()[0].getName();
        if (!placeHolderName.equals(newBound.getUnresolvedName())) {
            // we should produce a clean placeholder ClassNode here
            ClassNode clean = make(placeHolderName);
            clean.setGenericsTypes(newBound.getGenericsTypes());
            clean.setRedirect(newBound);
            newBound = clean;
        }
        newBound.setGenericsPlaceHolder(true);
    }
    return newBound;
}
 
Example 2
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected boolean resolve(final ClassNode type, final boolean testModuleImports, final boolean testDefaultImports, final boolean testStaticInnerClasses) {
    resolveGenericsTypes(type.getGenericsTypes());
    if (type.isResolved() || type.isPrimaryClassNode()) return true;
    if (type.isArray()) {
        ClassNode element = type.getComponentType();
        boolean resolved = resolve(element, testModuleImports, testDefaultImports, testStaticInnerClasses);
        if (resolved) {
            ClassNode cn = element.makeArray();
            type.setRedirect(cn);
        }
        return resolved;
    }

    // test if vanilla name is current class name
    if (currentClass == type) return true;

    String typeName = type.getName();

    GenericsType genericsType = genericParameterNames.get(new GenericsTypeName(typeName));
    if (genericsType != null) {
        type.setRedirect(genericsType.getType());
        type.setGenericsTypes(new GenericsType[]{genericsType});
        type.setGenericsPlaceHolder(true);
        return true;
    }

    if (currentClass.getNameWithoutPackage().equals(typeName)) {
        type.setRedirect(currentClass);
        return true;
    }

    return  (!type.hasPackageName() && resolveNestedClass(type)) ||
            resolveFromModule(type, testModuleImports) ||
            resolveFromCompileUnit(type) ||
            (testDefaultImports && !type.hasPackageName() && resolveFromDefaultImports(type)) ||
            resolveToOuter(type) ||
            (testStaticInnerClasses && type.hasPackageName() && resolveFromStaticInnerClasses(type));
}
 
Example 3
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 4
Source File: GenericsUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ClassNode makeClassSafe0(ClassNode type, GenericsType... genericTypes) {
    ClassNode plainNodeReference = newClass(type);
    if (genericTypes != null && genericTypes.length > 0) {
        plainNodeReference.setGenericsTypes(genericTypes);
        if (type.isGenericsPlaceHolder()) plainNodeReference.setGenericsPlaceHolder(true);
    }
    return plainNodeReference;
}
 
Example 5
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 6
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 7
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 8
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);
}