private static <T> Collection<T> toCollection(Iterable<T> iterable) { if (iterable instanceof Collection) { return (Collection<T>) iterable; } return toList(iterable); }
/** * Creates a MethodCallExpression that represents a call to a * method that takes arguments, using varargs. */ public static MethodCallExpression call(Expression expression, Method method, Expression... arguments) { return new MethodCallExpression(method, expression, toList(arguments)); }
/** * Creates a LambdaExpression by first constructing a delegate * type. * * <p>It can be used when the delegate type is not known at compile time. */ public static <T, F extends Function<? extends T>> FunctionExpression<F> lambda(Class<F> type, BlockStatement body, Iterable<? extends ParameterExpression> parameters) { return new FunctionExpression<>(type, body, toList(parameters)); }
/** * Creates a SwitchExpression that represents a switch statement * that has a default case. */ public static SwitchStatement switch_(Expression switchValue, Expression defaultBody, SwitchCase... cases) { return switch_(switchValue, defaultBody, null, toList(cases)); }
/** * Creates a SwitchExpression that represents a switch statement * that has a default case, using varargs. */ public static SwitchStatement switch_(Type type, Expression switchValue, Expression defaultBody, Method method, SwitchCase... cases) { return switch_(type, switchValue, defaultBody, method, toList(cases)); }
/** * Creates a SwitchCase for use in a SwitchExpression, with varargs. */ public static SwitchCase switchCase(Expression expression, Expression... body) { return switchCase(expression, toList(body)); }
/** * Creates a TryExpression representing a try block with any * number of catch statements and a finally block. */ public static TryStatement tryCatchFinally(Statement body, Iterable<? extends CatchBlock> handlers, Statement finally_) { return new TryStatement(body, toList(handlers), finally_); }
/** * Creates a MethodCallExpression that represents a call to a * static method that has arguments, using varargs. */ public static MethodCallExpression call(Method method, Expression... arguments) { return new MethodCallExpression(method, null, toList(arguments)); }
/** * Creates a MethodCallExpression that represents a call to a * method that takes arguments. */ public static MethodCallExpression call(Expression expression, Method method, Iterable<? extends Expression> arguments) { return new MethodCallExpression(method, expression, toList(arguments)); }
/** * Creates a MethodCallExpression that represents a call to an * instance method by calling the appropriate factory method, using varargs. */ public static MethodCallExpression call(Expression target, String methodName, Expression... arguments) { return call(target, methodName, toList(arguments)); }
/** * Creates a LoopExpression with the given body. */ public static ForStatement for_( Iterable<? extends DeclarationStatement> declarations, Expression condition, Expression post, Statement body) { return new ForStatement(toList(declarations), condition, post, body); }
/** * Creates a NewExpression that represents calling the specified * constructor with the specified arguments. */ public static NewExpression new_(Constructor constructor, Iterable<? extends Expression> expressions) { // Note that the last argument is not an empty list. That would cause // an anonymous inner-class with no members to be generated. return new NewExpression(constructor.getDeclaringClass(), toList(expressions), null); }
/** * Creates a NewArrayExpression that represents creating an * n-dimensional array and initializing it from a list of * elements, using varargs. * * @param type Element type of the array. */ public static NewArrayExpression newArrayInit(Type type, int dimension, Expression... expressions) { return new NewArrayExpression(type, dimension, null, toList(expressions)); }
/** * Creates a TryExpression representing a try block with any * number of catch statements and neither a fault nor finally * block, with varargs. */ public static TryStatement tryCatch(Statement body, CatchBlock... handlers) { return new TryStatement(body, toList(handlers), null); }
/** * Creates a TryExpression representing a try block with any * number of catch statements and a finally block, with varargs. */ public static TryStatement tryCatchFinally(Statement body, Statement finally_, CatchBlock... handlers) { return new TryStatement(body, toList(handlers), finally_); }
/** * Creates a MethodCallExpression that represents a call to a * static method by calling the * appropriate factory method, using varargs. */ public static MethodCallExpression call(Type type, String methodName, Expression... arguments) { return call(type, methodName, toList(arguments)); }
/** * Creates a MemberMemberBinding that represents the recursive * initialization of members of a field or property, using varargs. */ public static MemberMemberBinding memberBind(Member member, MemberBinding... bindings) { return memberBind(member, toList(bindings)); }
/** * Declares a constructor. */ public static ConstructorDeclaration constructorDecl(int modifier, Type declaredAgainst, Iterable<? extends ParameterExpression> parameters, BlockStatement body) { return new ConstructorDeclaration(modifier, declaredAgainst, toList(parameters), body); }
/** * Creates a TryExpression representing a try block with any * number of catch statements and neither a fault nor finally * block. */ public static TryStatement tryCatch(Statement body, Iterable<? extends CatchBlock> handlers) { return new TryStatement(body, toList(handlers), null); }
/** * Creates a LambdaExpression by first constructing a delegate * type. */ public static <F extends Function<?>> FunctionExpression<F> lambda( BlockStatement body, Iterable<? extends ParameterExpression> parameters) { final List<ParameterExpression> parameterList = toList(parameters); @SuppressWarnings("unchecked") Class<F> type = deduceType(parameterList, body.getType()); return new FunctionExpression<>(type, body, parameterList); }