/** * Creates a MethodCallExpression that represents a call to a * static method by calling the * appropriate factory method. */ public static MethodCallExpression call(Type type, String methodName, Iterable<? extends Expression> arguments) { Method method = Types.lookupMethod(Types.toClass(type), methodName, Types.toClassArray(arguments)); return new MethodCallExpression(method, null, toList(arguments)); }
static Class arrayClass(Type type) { // REVIEW: Is there a way to do this without creating an instance? We // just need the inverse of Class.getComponentType(). return Array.newInstance(toClass(type), 0).getClass(); }
/** * Creates a MethodCallExpression that represents a call to an * instance method by calling the appropriate factory method. */ public static MethodCallExpression call(Expression target, String methodName, Iterable<? extends Expression> arguments) { Method method; try { //noinspection unchecked method = Types.toClass(target.getType()) .getMethod(methodName, Types.toClassArray(arguments)); } catch (NoSuchMethodException e) { throw new RuntimeException("while resolving method '" + methodName + "' in class " + target.getType(), e); } return call(target, method, arguments); }
public static boolean isArray(Type type) { return toClass(type).isArray(); }
public static Class toClass(Type type) { if (type instanceof Class) { return (Class) type; } if (type instanceof ParameterizedType) { return toClass(((ParameterizedType) type).getRawType()); } if (type instanceof TypeVariable) { TypeVariable typeVariable = (TypeVariable) type; return toClass(typeVariable.getBounds()[0]); } throw new RuntimeException("unsupported type " + type); // TODO: }
public static Field lookupField(Type type, String name) { final Class clazz = toClass(type); try { return clazz.getField(name); } catch (NoSuchFieldException e) { throw new RuntimeException("while resolving field in class " + type); } }
static Class[] toClassArray(Collection<Type> types) { List<Class> classes = new ArrayList<>(); for (Type type : types) { classes.add(toClass(type)); } return classes.toArray(new Class[0]); }
MethodCallExpression(Type returnType, Method method, Expression targetExpression, List<Expression> expressions) { super(ExpressionType.Call, returnType); assert expressions != null : "expressions should not be null"; assert method != null : "method should not be null"; assert (targetExpression == null) == Modifier.isStatic( method.getModifiers()); assert Types.toClass(returnType) == method.getReturnType(); this.method = method; this.targetExpression = targetExpression; this.expressions = expressions; }
static Class[] toClassArray(Iterable<? extends Expression> arguments) { List<Class> classes = new ArrayList<>(); for (Expression argument : arguments) { classes.add(toClass(argument.getType())); } return classes.toArray(new Class[0]); }
public F getFunction() { if (function != null) { return function; } if (dynamicFunction == null) { final Invokable x = compile(); //noinspection unchecked dynamicFunction = (F) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Types.toClass(type)}, (proxy, method, args) -> x.dynamicInvoke(args)); } return dynamicFunction; }
public static PseudoField nthField(int ordinal, Type clazz) { if (clazz instanceof RecordType) { RecordType recordType = (RecordType) clazz; return recordType.getRecordFields().get(ordinal); } return field(toClass(clazz).getFields()[ordinal]); }
/** * Returns the element type of a {@link Collection}, {@link Iterable} * (including {@link org.apache.calcite.linq4j.Queryable Queryable} and * {@link org.apache.calcite.linq4j.Enumerable Enumerable}), {@link Iterator}, * {@link Enumerator}, or an array. * * <p>Returns null if the type is not one of these.</p> */ public static Type getElementType(Type type) { if (type instanceof ArrayType) { return ((ArrayType) type).getComponentType(); } if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } Class clazz = toClass(type); if (clazz.isArray()) { return clazz.getComponentType(); } if (Collection.class.isAssignableFrom(clazz) || Iterable.class.isAssignableFrom(clazz) || Iterator.class.isAssignableFrom(clazz) || Enumerator.class.isAssignableFrom(clazz)) { if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments()[0]; } return Object.class; } return null; }
final Class clazz = toClass(type); Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor constructor : constructors) {
static PseudoField getField(String fieldName, Type type) { if (type instanceof RecordType) { return getRecordField(fieldName, (RecordType) type); } else if (type instanceof Class && ((Class) type).isArray()) { return getSystemField(fieldName, (Class) type); } else { return field(getField(fieldName, toClass(type))); } }
RelOptTableImpl.create(null, typeFactory.createJavaType( Types.toClass( Types.getElementType(call.targetExpression.getType()))), ImmutableList.of(),
RelOptTableImpl.create(null, typeFactory.createJavaType( Types.toClass( Types.getElementType(call.targetExpression.getType()))), ImmutableList.of(),
private Expression toEnumerable(Expression expression) { final Type type = expression.getType(); if (Types.isArray(type)) { if (Types.toClass(type).getComponentType().isPrimitive()) { expression = Expressions.call(BuiltInMethod.AS_LIST.method, expression); } return Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, expression); } else if (Types.isAssignableFrom(Iterable.class, type) && !Types.isAssignableFrom(Enumerable.class, type)) { return Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, expression); } else if (Types.isAssignableFrom(Queryable.class, type)) { // Queryable extends Enumerable, but it's too "clever", so we call // Queryable.asEnumerable so that operations such as take(int) will be // evaluated directly. return Expressions.call(expression, BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method); } return expression; }
private Expression toEnumerable(Expression expression) { final Type type = expression.getType(); if (Types.isArray(type)) { if (Types.toClass(type).getComponentType().isPrimitive()) { expression = Expressions.call(BuiltInMethod.AS_LIST.method, expression); } return Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, expression); } else if (Types.isAssignableFrom(Iterable.class, type) && !Types.isAssignableFrom(Enumerable.class, type)) { return Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, expression); } else if (Types.isAssignableFrom(Queryable.class, type)) { // Queryable extends Enumerable, but it's too "clever", so we call // Queryable.asEnumerable so that operations such as take(int) will be // evaluated directly. return Expressions.call(expression, BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method); } return expression; }
assert expression != null; // TODO: user error in validator assert ModifiableTable.class.isAssignableFrom( Types.toClass(expression.getType())) : expression.getType(); builder.add( Expressions.declare(