Java Code Examples for org.codehaus.groovy.ast.ClassNode#setGenericsTypes()
The following examples show how to use
org.codehaus.groovy.ast.ClassNode#setGenericsTypes() .
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: Java8.java From groovy with Apache License 2.0 | 6 votes |
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 2
Source File: TypeCheckingExtension.java From groovy with Apache License 2.0 | 6 votes |
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 3
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 6 votes |
private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode) { ClassNode dummyClass = new ClassNode("dummy", 0, ClassHelper.OBJECT_TYPE); dummyClass.setModule(new ModuleNode(sourceUnit)); dummyClass.setGenericsTypes(mn.getDeclaringClass().getGenericsTypes()); MethodNode dummyMN = new MethodNode( "dummy", 0, parsedNode, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE ); dummyMN.setGenericsTypes(mn.getGenericsTypes()); dummyClass.addMethod(dummyMN); ResolveVisitor visitor = new ResolveVisitor(compilationUnit) { @Override public void addError(final String msg, final ASTNode expr) { sourceUnit.addError(new IncorrectTypeHintException(mn, msg, usage.getLineNumber(), usage.getColumnNumber())); } }; visitor.startResolving(dummyClass, sourceUnit); return dummyMN.getReturnType(); }
Example 4
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 6 votes |
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 5
Source File: TypeSignatureParser.java From groovy with Apache License 2.0 | 5 votes |
@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 6
Source File: InitializerStrategy.java From groovy with Apache License 2.0 | 5 votes |
private static ClassNode createInnerHelperClass(ClassNode buildee, String builderClassName, int fieldsSize) { final String fullName = buildee.getName() + "$" + builderClassName; ClassNode builder = new InnerClassNode(buildee, fullName, ACC_PUBLIC | ACC_STATIC, OBJECT_TYPE); GenericsType[] gtypes = new GenericsType[fieldsSize]; for (int i = 0; i < gtypes.length; i++) { gtypes[i] = makePlaceholder(i); } builder.setGenericsTypes(gtypes); return builder; }
Example 7
Source File: Java8.java From groovy with Apache License 2.0 | 5 votes |
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 8
Source File: StatementMetaTypeChooser.java From groovy with Apache License 2.0 | 5 votes |
@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: GenericsUtils.java From groovy with Apache License 2.0 | 5 votes |
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 10
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 5 votes |
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 11
Source File: WideningCategories.java From groovy with Apache License 2.0 | 5 votes |
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 12
Source File: WideningCategories.java From groovy with Apache License 2.0 | 5 votes |
/** * 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<T> and a is Set<String> and b is Set<StringBuffer>, this * will return a LUB which parameterized type matches Set<? extends CharSequence> * @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 13
Source File: CompilationUnit.java From netbeans with Apache License 2.0 | 5 votes |
private ClassNode createClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, Set<GenericsType> generics) { if ("java.lang.Object".equals(name) && superClass == null) { // NOI18N return ClassHelper.OBJECT_TYPE; } ClassNode classNode = new ClassNode(name, modifiers, superClass, interfaces, MixinNode.EMPTY_ARRAY); if (generics != null) { classNode.setGenericsTypes(generics.toArray(new GenericsType[generics.size()])); } return classNode; }
Example 14
Source File: TraitASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
private static ClassNode createReceiverType(final boolean isStatic, final ClassNode rawType) { ClassNode type; if (isStatic) { // Class<TraitClass> type = ClassHelper.CLASS_Type.getPlainNodeReference(); type.setGenericsTypes(new GenericsType[]{ new GenericsType(rawType) }); } else { // TraitClass type = rawType; } return type; }
Example 15
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 5 votes |
/** * 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 16
Source File: MapEntryOrKeyValue.java From groovy with Apache License 2.0 | 5 votes |
public List<ClassNode[]> getClosureSignatures(final MethodNode node, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final String[] options, final ASTNode usage) { Options opt; try { opt = Options.parse(node, usage, options); } catch (IncorrectTypeHintException e) { sourceUnit.addError(e); return Collections.emptyList(); } GenericsType[] genericsTypes = node.getParameters()[opt.parameterIndex].getOriginType().getGenericsTypes(); if (genericsTypes==null) { // would happen if you have a raw Map type for example genericsTypes = new GenericsType[] { new GenericsType(ClassHelper.OBJECT_TYPE), new GenericsType(ClassHelper.OBJECT_TYPE) }; } ClassNode[] firstSig; ClassNode[] secondSig; ClassNode mapEntry = MAPENTRY_TYPE.getPlainNodeReference(); mapEntry.setGenericsTypes(genericsTypes); if (opt.generateIndex) { firstSig = new ClassNode[] {genericsTypes[0].getType(), genericsTypes[1].getType(), ClassHelper.int_TYPE}; secondSig = new ClassNode[] {mapEntry, ClassHelper.int_TYPE}; } else { firstSig = new ClassNode[] {genericsTypes[0].getType(), genericsTypes[1].getType()}; secondSig = new ClassNode[] {mapEntry}; } return Arrays.asList(firstSig, secondSig); }
Example 17
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 5 votes |
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 18
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
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 19
Source File: Java8.java From groovy with Apache License 2.0 | 4 votes |
private ClassNode configureParameterizedType(ParameterizedType parameterizedType) { ClassNode base = configureType(parameterizedType.getRawType()); GenericsType[] gts = configureTypeArguments(parameterizedType.getActualTypeArguments()); base.setGenericsTypes(gts); return base; }
Example 20
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 4 votes |
static void addMethodLevelDeclaredGenerics(final MethodNode method, final Map<GenericsTypeName, GenericsType> resolvedPlaceholders) { ClassNode dummy = OBJECT_TYPE.getPlainNodeReference(); dummy.setGenericsTypes(method.getGenericsTypes()); GenericsUtils.extractPlaceholders(dummy, resolvedPlaceholders); }