private ClassNode configureGenericArray(GenericArrayType genericArrayType) { Type component = genericArrayType.getGenericComponentType(); ClassNode node = configureType(component); return node.makeArray(); }
@Override void finished(ClassNode result) { outer.finished(result.makeArray()); } };
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; } }
/** * Returns a ClassNode representing an array of the class * represented by this ClassNode */ public ClassNode makeArray() { if (redirect!=null) { ClassNode res = redirect().makeArray(); res.componentType = this; return res; } ClassNode cn; if (clazz!=null) { Class ret = Array.newInstance(clazz,0).getClass(); // don't use the ClassHelper here! cn = new ClassNode(ret,this); } else { cn = new ClassNode(this); } return cn; }
public static ClassNode makeClassSafeWithGenerics(ClassNode type, GenericsType... genericTypes) { if (type.isArray()) { return makeClassSafeWithGenerics(type.getComponentType(), genericTypes).makeArray(); } GenericsType[] gtypes = GenericsType.EMPTY_ARRAY; if (genericTypes != null) { gtypes = new GenericsType[genericTypes.length]; System.arraycopy(genericTypes, 0, gtypes, 0, gtypes.length); } return makeClassSafe0(type, gtypes); }
/** * Finds a class node given a string representing the type. Performs a lookup in the compilation unit to check if it is done in the same source unit. * @param sourceUnit source unit * @param compilationUnit compilation unit * @param className the name of the class we want to get a {@link org.codehaus.groovy.ast.ClassNode} for * @return a ClassNode representing the type */ protected ClassNode findClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final String className) { if (className.endsWith("[]")) { return findClassNode(sourceUnit, compilationUnit, className.substring(0, className.length() - 2)).makeArray(); } ClassNode cn = compilationUnit.getClassNode(className); if (cn == null) { try { cn = ClassHelper.make(Class.forName(className, false, sourceUnit.getClassLoader())); } catch (ClassNotFoundException e) { cn = ClassHelper.make(className); } } return cn; } }
private Expression serialize(Expression e) { if (e instanceof AnnotationConstantExpression) { AnnotationConstantExpression ace = (AnnotationConstantExpression) e; return serialize((AnnotationNode) ace.getValue()); } else if (e instanceof ListExpression) { boolean annotationConstant = false; ListExpression le = (ListExpression) e; List<Expression> list = le.getExpressions(); List<Expression> newList = new ArrayList<Expression>(list.size()); for (Expression exp: list) { annotationConstant = annotationConstant || exp instanceof AnnotationConstantExpression; newList.add(serialize(exp)); } ClassNode type = ClassHelper.OBJECT_TYPE; if (annotationConstant) type = type.makeArray(); return new ArrayExpression(type, newList); } return e; }
public static Expression cloneArrayOrCloneableExpr(Expression fieldExpr, ClassNode type) { Expression smce = callX( REFLECTION_INVOKER_TYPE, "invoke", args( fieldExpr, constX("clone"), new ArrayExpression(ClassHelper.OBJECT_TYPE.makeArray(), Collections.<Expression>emptyList()) ) ); return castX(type, smce); }
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(); }
public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, ClassNode type) { if (type.isArray()) { return correctToGenericsSpec(genericsSpec, type.getComponentType()).makeArray(); } if (type.isGenericsPlaceHolder()) { String name = type.getGenericsTypes()[0].getName(); type = genericsSpec.get(name); } if (type == null) type = ClassHelper.OBJECT_TYPE; return type; }
public ClassNode resolveType(Type type) { if (type.getSort() == Type.ARRAY) { ClassNode result = resolveNonArrayType(type.getElementType()); for (int i = 0; i < type.getDimensions(); i++) { result = result.makeArray(); } return result; } return resolveNonArrayType(type); }
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; }
private static void adjustTypesIfStaticMainMethod(MethodNode node) { if (node.getName().equals("main") && node.isStatic()) { Parameter[] params = node.getParameters(); if (params.length == 1) { Parameter param = params[0]; if (param.getType() == null || param.getType() == ClassHelper.OBJECT_TYPE) { param.setType(ClassHelper.STRING_TYPE.makeArray()); ClassNode returnType = node.getReturnType(); if (returnType == ClassHelper.OBJECT_TYPE) { node.setReturnType(ClassHelper.VOID_TYPE); } } } } }
private void completeEnum(ClassNode enumClass) { boolean isAic = isAnonymousInnerClass(enumClass); // create MIN_VALUE and MAX_VALUE fields FieldNode minValue = null, maxValue = null, values = null; if (!isAic) { ClassNode enumRef = enumClass.getPlainNodeReference(); // create values field values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null); values.setSynthetic(true); addMethods(enumClass, values); checkForAbstractMethods(enumClass); // create MIN_VALUE and MAX_VALUE fields minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null); maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null); } addInit(enumClass, minValue, maxValue, values, isAic); }
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; }
/** * 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(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; }
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; }
public void testLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll")}; Statement loopStatement = createPrintlnStatement(new VariableExpression("i")); ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement); classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method with looping"); Object[] array = {new Integer(1234), "abc", "def"}; try { InvokerHelper.invokeMethod(bean, "iterateDemo", new Object[]{array}); } catch (InvokerInvocationException e) { System.out.println("Caught: " + e.getCause()); e.getCause().printStackTrace(); fail("Should not have thrown an exception"); } System.out.println("################ Done"); }