public static ClassNode makeWithoutCaching(Class c) { return makeWithoutCaching(c, true); }
private static ClassNode configureClass(Class c) { if (c.isPrimitive()) { return ClassHelper.make(c); } else { return ClassHelper.makeWithoutCaching(c, false); } }
/** * Creates a ClassNode using a given class. * If the name is one of the predefined ClassNodes then the * corresponding ClassNode instance will be returned. If the * name is null or of length 0 the dynamic type is returned * * @param name of the class the ClassNode is representing */ public static ClassNode make(String name) { if (name == null || name.length() == 0) return DYNAMIC_TYPE; for (int i = 0; i < primitiveClassNames.length; i++) { if (primitiveClassNames[i].equals(name)) return types[i]; } for (int i = 0; i < classes.length; i++) { String cname = classes[i].getName(); if (name.equals(cname)) return types[i]; } return makeWithoutCaching(name); }
public static ClassNode make(Class c, boolean includeGenerics) { for (int i = 0; i < classes.length; i++) { if (c == classes[i]) return types[i]; } if (c.isArray()) { ClassNode cn = make(c.getComponentType(), includeGenerics); return cn.makeArray(); } return makeWithoutCaching(c, includeGenerics); }
public static ClassNode makeWithoutCaching(Class c, boolean includeGenerics) { if (c.isArray()) { ClassNode cn = makeWithoutCaching(c.getComponentType(), includeGenerics); return cn.makeArray(); } final ClassNode cached = makeCached(c); if (includeGenerics) { return cached; } else { ClassNode t = makeWithoutCaching(c.getName()); t.setRedirect(cached); return t; } }
/** * 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; }
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; }
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; }
private ClassNode configureWildcardType(WildcardType wildcardType) { ClassNode base = ClassHelper.makeWithoutCaching("?"); base.setRedirect(ClassHelper.OBJECT_TYPE); //TODO: more than one lower bound for wildcards? ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds()); ClassNode lower = null; // TODO: is it safe to remove this? What was the original intention? if (lowers != null) lower = lowers[0]; ClassNode[] upper = configureTypes(wildcardType.getUpperBounds()); GenericsType t = new GenericsType(base, upper, lower); t.setWildcard(true); ClassNode ref = ClassHelper.makeWithoutCaching(Object.class, false); ref.setGenericsTypes(new GenericsType[]{t}); return ref; }
private static GenericsType makePlaceholder(int i) { ClassNode type = ClassHelper.makeWithoutCaching("T" + i); type.setRedirect(OBJECT_TYPE); type.setGenericsPlaceHolder(true); return new GenericsType(type); }
private static GenericsType createWildcard(ClassNode[] upper, ClassNode lower) { ClassNode base = ClassHelper.makeWithoutCaching("?"); base.setRedirect(ClassHelper.OBJECT_TYPE); GenericsType t = new GenericsType(base, upper, lower); t.setWildcard(true); return t; }
private static GenericsType boundUnboundedWildcard(GenericsType gt, GenericsType spec) { if (isUnboundedWildcard(gt)) { ClassNode base = makeWithoutCaching("?"); // The bounds on the declared type are at least as good as the ones on an unbounded wildcard, since it has // none! GenericsType newGt = new GenericsType(base, spec.getUpperBounds(), spec.getLowerBound()); newGt.setWildcard(true); return newGt; } return gt; }
private static GenericsType buildWildcardType(GenericsType origin) { ClassNode lowerBound = origin.getType().getPlainNodeReference(); if (hasNonTrivialBounds(origin)) { lowerBound.setGenericsTypes(new GenericsType[]{origin}); } ClassNode base = makeWithoutCaching("?"); GenericsType gt = new GenericsType(base, null, lowerBound); gt.setWildcard(true); return gt; }
public static Map<String, ClassNode> addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec) { Map<String, ClassNode> ret = new HashMap<String, ClassNode>(oldSpec); // ret starts with the original type specs, now add gts for the current method if any GenericsType[] sgts = current.getGenericsTypes(); if (sgts != null) { for (GenericsType sgt : sgts) { String name = sgt.getName(); if (sgt.isPlaceholder()) { ClassNode redirect; if (sgt.getUpperBounds() != null) { redirect = sgt.getUpperBounds()[0]; } else if (sgt.getLowerBound() != null) { redirect = sgt.getLowerBound(); } else { redirect = ClassHelper.OBJECT_TYPE; } ClassNode type = ClassHelper.makeWithoutCaching(name); type.setGenericsPlaceHolder(true); type.setRedirect(redirect); ret.put(name, type); } else { ret.put(name, sgt.getType()); } } } return ret; }
private static void scanClassesForDGMMethods(Map<String, List<MethodNode>> accumulator, Iterable<Class> allClasses, boolean isStatic) { for (Class dgmLikeClass : allClasses) { ClassNode cn = makeWithoutCaching(dgmLikeClass, true); for (MethodNode metaMethod : cn.getMethods()) { Parameter[] types = metaMethod.getParameters();
public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] interfaces, ClassNode outerClass) { modifiers = modifiers | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM; ClassNode enumClass; if (outerClass==null) { enumClass = new ClassNode(name,modifiers,null,interfaces,MixinNode.EMPTY_ARRAY); } else { name = outerClass.getName() + "$" + name; modifiers |= Opcodes.ACC_STATIC; enumClass = new InnerClassNode(outerClass,name,modifiers,null,interfaces,MixinNode.EMPTY_ARRAY); } // set super class and generics info // "enum X" -> class X extends Enum<X> GenericsType gt = new GenericsType(enumClass); ClassNode superClass = ClassHelper.makeWithoutCaching("java.lang.Enum"); superClass.setGenericsTypes(new GenericsType[]{gt}); enumClass.setSuperClass(superClass); superClass.setRedirect(ClassHelper.Enum_Type); return enumClass; }
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; }
protected ClassNode makeType(AST typeNode) { ClassNode answer = ClassHelper.DYNAMIC_TYPE; AST node = typeNode.getFirstChild(); if (node != null) { if (isType(INDEX_OP, node) || isType(ARRAY_DECLARATOR, node)) { answer = makeType(node).makeArray(); } else { checkTypeArgs(node, false); answer = ClassHelper.make(qualifiedName(node)); if (answer.isUsingGenerics()) { ClassNode newAnswer = ClassHelper.makeWithoutCaching(answer.getName()); newAnswer.setRedirect(answer); answer = newAnswer; } } configureAST(answer, node); } return answer; }
private MethodCallExpression referenceToCurrentClosure() { return new MethodCallExpression( new VariableExpression("this"), new ConstantExpression("each"), new ArgumentListExpression( new PropertyExpression( new ClassExpression(ClassHelper.makeWithoutCaching(Closure.class)), new ConstantExpression("IDENTITY") ) ) ); }
private GenericsType makeGenericsArgumentType(AST typeArgument) { GenericsType gt; AST rootNode = typeArgument.getFirstChild(); if (isType(WILDCARD_TYPE, rootNode)) { ClassNode base = ClassHelper.makeWithoutCaching("?"); if (rootNode.getNextSibling() != null) { int boundType = getBoundType(rootNode.getNextSibling()); ClassNode[] gts = makeGenericsBounds(rootNode, boundType); if (boundType == TYPE_UPPER_BOUNDS) { gt = new GenericsType(base, gts, null); } else { gt = new GenericsType(base, null, gts[0]); } } else { gt = new GenericsType(base, null, null); } gt.setName("?"); gt.setWildcard(true); } else { ClassNode argument = makeTypeWithArguments(rootNode); gt = new GenericsType(argument); } configureAST(gt, typeArgument); return gt; }