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

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getPlainNodeReference() . 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: TypeCheckingExtension.java    From groovy with Apache License 2.0 6 votes vote down vote up
public ClassNode parameterizedType(ClassNode baseType, ClassNode... genericsTypeArguments) {
    ClassNode result = baseType.getPlainNodeReference();
    if (result.isUsingGenerics()) {
        GenericsType[] gts = new GenericsType[genericsTypeArguments.length];
        int expectedLength = result.getGenericsTypes().length;
        if (expectedLength!=genericsTypeArguments.length) {
            throw new GroovyBugError("Expected number of generic type arguments for "+baseType.toString(false)+" is "+expectedLength
            + " but you gave "+genericsTypeArguments.length);
        }
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericsType(genericsTypeArguments[i]);
        }
        result.setGenericsTypes(gts);
    }
    return result;
}
 
Example 2
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 3
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode makeRawType(final ClassNode receiver) {
    if (receiver.isArray()) {
        return makeRawType(receiver.getComponentType()).makeArray();
    }
    ClassNode raw = receiver.getPlainNodeReference();
    raw.setUsingGenerics(false);
    raw.setGenericsTypes(null);
    return raw;
}
 
Example 4
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 5
Source File: ImmutableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void createCopyWith(final ClassNode cNode, final List<PropertyNode> pList) {
    BlockStatement body = new BlockStatement();
    body.addStatement(ifS(
            orX(
                    equalsNullX(varX("map", ClassHelper.MAP_TYPE)),
                    eqX(callX(varX("map", HMAP_TYPE), "size"), constX(0))
            ),
            returnS(varX("this", cNode))
    ));
    body.addStatement(declS(localVarX("dirty", ClassHelper.boolean_TYPE), ConstantExpression.PRIM_FALSE));
    body.addStatement(declS(localVarX("construct", HMAP_TYPE), ctorX(HMAP_TYPE)));

    // Check for each property
    for (final PropertyNode pNode : pList) {
        body.addStatement(createCheckForProperty(pNode));
    }

    body.addStatement(returnS(ternaryX(
            isTrueX(varX("dirty", ClassHelper.boolean_TYPE)),
            ctorX(cNode, args(varX("construct", HMAP_TYPE))),
            varX("this", cNode)
    )));

    final ClassNode clonedNode = cNode.getPlainNodeReference();

    addGeneratedMethod(cNode, COPY_WITH_METHOD,
            ACC_PUBLIC | ACC_FINAL,
            clonedNode,
            params(new Parameter(new ClassNode(Map.class), "map")),
            null,
            body);
}
 
Example 6
Source File: EnumHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static FieldNode addEnumConstant(ClassNode enumClass, String name, Expression init) {
    int modifiers = PUBLIC_FS | Opcodes.ACC_ENUM;
    if (init != null && !(init instanceof ListExpression)) {
        ListExpression list = new ListExpression();
        list.addExpression(init);
        init = list;
    }
    FieldNode fn = new FieldNode(name, modifiers, enumClass.getPlainNodeReference(), enumClass, init);
    enumClass.addField(fn);
    return fn;
}
 
Example 7
Source File: TypeSignatureParser.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitEnd() {
    ClassNode base = resolver.resolveClass(baseName);
    if (arguments.isEmpty()) {
        finished(base);
        return;
    }

    ClassNode bound = base.getPlainNodeReference();
    bound.setGenericsTypes(arguments.toArray(GenericsType.EMPTY_ARRAY));
    finished(bound);
}
 
Example 8
Source File: WideningCategories.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Given a lowest upper bound computed without generic type information but which requires to be parameterized
 * and the two implementing classnodes which are parameterized with potentially two different types, returns
 * a parameterized lowest upper bound.
 *
 * For example, if LUB is Set&lt;T&gt; and a is Set&lt;String&gt; and b is Set&lt;StringBuffer&gt;, this
 * will return a LUB which parameterized type matches Set&lt;? extends CharSequence&gt;
 * @param lub the type to be parameterized
 * @param a parameterized type a
 * @param b parameterized type b
 * @param fallback if we detect a recursive call, use this LUB as the parameterized type instead of computing a value
 * @return the class node representing the parameterized lowest upper bound
 */
private static ClassNode parameterizeLowestUpperBound(final ClassNode lub, final ClassNode a, final ClassNode b, final ClassNode fallback) {
    if (!lub.isUsingGenerics()) return lub;
    // a common super type exists, all we have to do is to parameterize
    // it according to the types provided by the two class nodes
    ClassNode holderForA = findGenericsTypeHolderForClass(a, lub);
    ClassNode holderForB = findGenericsTypeHolderForClass(b, lub);
    // let's compare their generics type
    GenericsType[] agt = holderForA == null ? null : holderForA.getGenericsTypes();
    GenericsType[] bgt = holderForB == null ? null : holderForB.getGenericsTypes();
    if (agt==null || bgt==null || agt.length!=bgt.length) {
        return lub;
    }
    GenericsType[] lubgt = new GenericsType[agt.length];
    for (int i = 0; i < agt.length; i++) {
        ClassNode t1 = agt[i].getType();
        ClassNode t2 = bgt[i].getType();
        ClassNode basicType;
        if (areEqualWithGenerics(t1, a) && areEqualWithGenerics(t2,b)) {
            // we are facing a self referencing type !
            basicType = fallback;
        } else {
             basicType = lowestUpperBound(t1, t2);
        }
        if (t1.equals(t2)) {
            lubgt[i] = new GenericsType(basicType);
        } else {
            lubgt[i] = GenericsUtils.buildWildcardType(basicType);
        }
    }
    ClassNode plain = lub.getPlainNodeReference();
    plain.setGenericsTypes(lubgt);
    return plain;
}
 
Example 9
Source File: WideningCategories.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode findGenericsTypeHolderForClass(ClassNode source, ClassNode type) {
    if (isPrimitiveType(source)) source = getWrapper(source);
    if (source.equals(type)) return source;
    if (type.isInterface()) {
        for (ClassNode interfaceNode : source.getAllInterfaces()) {
            if (interfaceNode.equals(type)) {
                ClassNode parameterizedInterface = GenericsUtils.parameterizeType(source, interfaceNode);
                return parameterizedInterface;
            }
        }
    }
    ClassNode superClass = source.getUnresolvedSuperClass();
    // copy generic type information if available
    if (superClass!=null && superClass.isUsingGenerics()) {
        Map<GenericsTypeName, GenericsType> genericsTypeMap = GenericsUtils.extractPlaceholders(source);
        GenericsType[] genericsTypes = superClass.getGenericsTypes();
        if (genericsTypes!=null) {
            GenericsType[] copyTypes = new GenericsType[genericsTypes.length];
            for (int i = 0; i < genericsTypes.length; i++) {
                GenericsType genericsType = genericsTypes[i];
                GenericsTypeName gtn = new GenericsTypeName(genericsType.getName());
                if (genericsType.isPlaceholder() && genericsTypeMap.containsKey(gtn)) {
                    copyTypes[i] = genericsTypeMap.get(gtn);
                } else {
                    copyTypes[i] = genericsType;
                }
            }
            superClass = superClass.getPlainNodeReference();
            superClass.setGenericsTypes(copyTypes);
        }
    }
    if (superClass!=null) return findGenericsTypeHolderForClass(superClass, type);
    return null;
}
 
Example 10
Source File: Java8.java    From groovy with Apache License 2.0 5 votes vote down vote up
private ClassNode makeClassNode(CompileUnit cu, Type t, Class<?> c) {
    ClassNode back = null;
    if (cu != null) back = cu.getClass(c.getName());
    if (back == null) back = ClassHelper.make(c);
    if (!(t instanceof Class)) {
        ClassNode front = configureType(t);
        front.setRedirect(back);
        return front;
    }
    return back.getPlainNodeReference();
}
 
Example 11
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static PropertyExpression newStaticPropertyX(ClassNode type, String name) {
    return new PropertyExpression(new ClassExpression(type.getPlainNodeReference()), name);
}
 
Example 12
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static StaticMethodCallExpression newStaticMethodCallX(ClassNode type, String name, Expression args) {
    return new StaticMethodCallExpression(type.getPlainNodeReference(), name, args);
}
 
Example 13
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static ClassNode getCorrectedClassNode(final ClassNode type, final ClassNode superClass, final boolean handlingGenerics) {
    if (handlingGenerics && missesGenericsTypes(type)) return superClass.getPlainNodeReference();
    return GenericsUtils.correctToGenericsSpecRecurse(GenericsUtils.createGenericsSpec(type), superClass);
}
 
Example 14
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static Parameter createSelfParameter(final ClassNode traitClass, boolean isStatic) {
    final ClassNode rawType = traitClass.getPlainNodeReference();
    ClassNode type = createReceiverType(isStatic, rawType);
    return new Parameter(type, isStatic?Traits.STATIC_THIS_OBJECT:Traits.THIS_OBJECT);
}
 
Example 15
Source File: GenericsUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static ClassNode newClass(ClassNode type) {
    return type.getPlainNodeReference();
}
 
Example 16
Source File: Verifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static ClassNode cleanType(ClassNode type) {
    // todo: should this be directly handled by getPlainNodeReference?
    if (type.isArray()) return cleanType(type.getComponentType()).makeArray();
    return type.getPlainNodeReference();
}