This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 4f69d9b3a498112da67d51c98963781f76f83f8b Author: Julian Hyde <[email protected]> AuthorDate: Fri Aug 25 15:35:30 2023 -0700 Refactor: In ReflectUtil, add methods isStatic and isPublic --- .../enumerable/ReflectiveCallNotNullImplementor.java | 5 +++-- .../org/apache/calcite/adapter/enumerable/RexImpTable.java | 10 +++++----- .../java/org/apache/calcite/interpreter/TableScanNode.java | 5 ++--- .../java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java | 8 ++++---- .../org/apache/calcite/rel/metadata/MetadataHandler.java | 5 +++-- .../calcite/rel/metadata/ReflectiveRelMetadataProvider.java | 13 ++++++++----- .../org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java | 5 +++-- .../src/main/java/org/apache/calcite/runtime/Resources.java | 6 ++++-- .../apache/calcite/schema/impl/AggregateFunctionImpl.java | 11 ++++++----- .../apache/calcite/schema/impl/ReflectiveFunctionBase.java | 8 ++++---- .../org/apache/calcite/schema/impl/ScalarFunctionImpl.java | 10 +++++----- .../org/apache/calcite/schema/impl/TableFunctionImpl.java | 6 +++--- .../java/org/apache/calcite/schema/impl/TableMacroImpl.java | 6 +++--- .../apache/calcite/sql2rel/ReflectiveConvertletTable.java | 7 ++++--- core/src/main/java/org/apache/calcite/util/ReflectUtil.java | 13 ++++++++++++- core/src/main/java/org/apache/calcite/util/Util.java | 8 ++++---- .../java/org/apache/calcite/test/JdbcFrontLinqBackTest.java | 4 ++-- core/src/test/java/org/apache/calcite/test/SqlTestGen.java | 8 +++++--- core/src/test/java/org/apache/calcite/util/UtilTest.java | 4 ++-- .../calcite/adapter/geode/util/JavaTypeFactoryExtImpl.java | 5 +++-- .../main/java/org/apache/calcite/piglet/PigUdfFinder.java | 5 +++-- 21 files changed, 88 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/ReflectiveCallNotNullImplementor.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/ReflectiveCallNotNullImplementor.java index 228ae48842..56dfe4cf22 100644 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/ReflectiveCallNotNullImplementor.java +++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/ReflectiveCallNotNullImplementor.java @@ -21,9 +21,10 @@ import org.apache.calcite.linq4j.tree.Expressions; import org.apache.calcite.rex.RexCall; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.List; +import static org.apache.calcite.util.ReflectUtil.isStatic; + /** * Implementation of * {@link org.apache.calcite.adapter.enumerable.NotNullImplementor} @@ -51,7 +52,7 @@ public class ReflectiveCallNotNullImplementor implements NotNullImplementor { translatedOperands = EnumUtils.convertAssignableTypes(method.getParameterTypes(), translatedOperands); final Expression callExpr; - if ((method.getModifiers() & Modifier.STATIC) != 0) { + if (isStatic(method)) { callExpr = Expressions.call(method, translatedOperands); } else { final Expression target = diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java index 1c66ffd180..df3073699f 100644 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java +++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java @@ -94,7 +94,6 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -440,6 +439,7 @@ import static org.apache.calcite.sql.fun.SqlStdOperatorTable.UNARY_MINUS; import static org.apache.calcite.sql.fun.SqlStdOperatorTable.UNARY_PLUS; import static org.apache.calcite.sql.fun.SqlStdOperatorTable.UPPER; import static org.apache.calcite.sql.fun.SqlStdOperatorTable.USER; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static java.util.Objects.requireNonNull; @@ -1882,7 +1882,7 @@ public class RexImpTable { private static NewExpression makeNew(AggregateFunctionImpl afi) { try { Constructor<?> constructor = afi.declaringClass.getConstructor(); - Objects.requireNonNull(constructor, "constructor"); + requireNonNull(constructor, "constructor"); return Expressions.new_(afi.declaringClass); } catch (NoSuchMethodException e) { // ignore, and try next constructor @@ -1890,7 +1890,7 @@ public class RexImpTable { try { Constructor<?> constructor = afi.declaringClass.getConstructor(FunctionContext.class); - Objects.requireNonNull(constructor, "constructor"); + requireNonNull(constructor, "constructor"); return Expressions.new_(afi.declaringClass, Expressions.call(BuiltInMethod.FUNCTION_CONTEXTS_OF.method, DataContext.ROOT, @@ -2700,7 +2700,7 @@ public class RexImpTable { @Override Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) { - if (Modifier.isStatic(method.getModifiers())) { + if (isStatic(method)) { return call(method, null, argValueList); } else { return call(method, argValueList.get(0), Util.skip(argValueList, 1)); @@ -4127,7 +4127,7 @@ public class RexImpTable { RexCall call, List<Expression> argValueList) { List<Expression> argValueList0 = EnumUtils.fromInternal(method.getParameterTypes(), argValueList); - if ((method.getModifiers() & Modifier.STATIC) != 0) { + if (isStatic(method)) { return Expressions.call(method, argValueList0); } else { // The UDF class must have a public zero-args constructor. diff --git a/core/src/main/java/org/apache/calcite/interpreter/TableScanNode.java b/core/src/main/java/org/apache/calcite/interpreter/TableScanNode.java index 343d10174e..51dbbdd02d 100644 --- a/core/src/main/java/org/apache/calcite/interpreter/TableScanNode.java +++ b/core/src/main/java/org/apache/calcite/interpreter/TableScanNode.java @@ -36,6 +36,7 @@ import org.apache.calcite.schema.SchemaPlus; import org.apache.calcite.schema.Schemas; import org.apache.calcite.util.ImmutableBitSet; import org.apache.calcite.util.ImmutableIntList; +import org.apache.calcite.util.ReflectUtil; import org.apache.calcite.util.Util; import org.apache.calcite.util.mapping.Mapping; import org.apache.calcite.util.mapping.Mappings; @@ -47,7 +48,6 @@ import com.google.common.collect.Lists; import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.List; @@ -142,8 +142,7 @@ public class TableScanNode implements Node { ImmutableList.Builder<Field> fieldBuilder = ImmutableList.builder(); Class type = (Class) elementType; for (Field field : type.getFields()) { - if (Modifier.isPublic(field.getModifiers()) - && !Modifier.isStatic(field.getModifiers())) { + if (ReflectUtil.isPublic(field) && !ReflectUtil.isStatic(field)) { fieldBuilder.add(field); } } diff --git a/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java b/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java index 00d256dbf1..05ed581654 100644 --- a/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java +++ b/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java @@ -51,6 +51,8 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static org.apache.calcite.util.ReflectUtil.isStatic; + import static java.util.Objects.requireNonNull; /** @@ -76,13 +78,11 @@ public class JavaTypeFactoryImpl @Override public RelDataType createStructType(Class type) { final List<RelDataTypeField> list = new ArrayList<>(); for (Field field : type.getFields()) { - if (!Modifier.isStatic(field.getModifiers())) { + if (!isStatic(field)) { // FIXME: watch out for recursion final Type fieldType = fieldType(field); list.add( - new RelDataTypeFieldImpl( - field.getName(), - list.size(), + new RelDataTypeFieldImpl(field.getName(), list.size(), createType(fieldType))); } } diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/MetadataHandler.java b/core/src/main/java/org/apache/calcite/rel/metadata/MetadataHandler.java index b89c54b3e9..a4d1d9e538 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/MetadataHandler.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/MetadataHandler.java @@ -19,10 +19,11 @@ package org.apache.calcite.rel.metadata; import com.google.common.collect.ImmutableSortedMap; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.SortedMap; +import static org.apache.calcite.util.ReflectUtil.isStatic; + /** * Marker interface for a handler of metadata. * @@ -50,7 +51,7 @@ public interface MetadataHandler<M extends Metadata> { Arrays.stream(handlerClass.getDeclaredMethods()) .filter(m -> !m.getName().equals("getDef")) .filter(m -> !m.isSynthetic()) - .filter(m -> !Modifier.isStatic(m.getModifiers())) + .filter(m -> !isStatic(m)) .forEach(m -> map.put(m.getName(), m)); return map.build(); } diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java b/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java index 408a53687e..14c040017a 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java @@ -34,7 +34,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; import java.util.ArrayList; @@ -43,11 +42,15 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static org.apache.calcite.util.ReflectUtil.isPublic; +import static org.apache.calcite.util.ReflectUtil.isStatic; + +import static java.util.Objects.requireNonNull; + /** * Implementation of the {@link RelMetadataProvider} interface that dispatches * metadata methods to methods on a given object via reflection. @@ -248,8 +251,8 @@ public class ReflectiveRelMetadataProvider @Deprecated // to be removed before 2.0 private static boolean couldImplement(Method handlerMethod, Method method) { if (!handlerMethod.getName().equals(method.getName()) - || (handlerMethod.getModifiers() & Modifier.STATIC) != 0 - || (handlerMethod.getModifiers() & Modifier.PUBLIC) == 0) { + || isStatic(handlerMethod) + || !isPublic(handlerMethod)) { return false; } final Class<?>[] parameterTypes1 = handlerMethod.getParameterTypes(); @@ -339,7 +342,7 @@ public class ReflectiveRelMetadataProvider * {@code map}. */ @SuppressWarnings({ "unchecked", "SuspiciousMethodCalls" }) Method find(final Class<? extends RelNode> relNodeClass, Method method) { - Objects.requireNonNull(relNodeClass, "relNodeClass"); + requireNonNull(relNodeClass, "relNodeClass"); for (Class r = relNodeClass;;) { Method implementingMethod = handlerMap.get(Pair.of(r, method)); if (implementingMethod != null) { diff --git a/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java b/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java index c69f542002..94ec7e0609 100644 --- a/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java +++ b/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java @@ -41,7 +41,6 @@ import com.google.common.collect.Interners; import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.nio.charset.Charset; import java.sql.Time; import java.sql.Timestamp; @@ -53,6 +52,8 @@ import java.util.Objects; import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.calcite.util.ReflectUtil.isStatic; + import static java.util.Objects.requireNonNull; /** @@ -509,7 +510,7 @@ public abstract class RelDataTypeFactoryImpl implements RelDataTypeFactory { private @Nullable List<RelDataTypeFieldImpl> fieldsOf(Class clazz) { final List<RelDataTypeFieldImpl> list = new ArrayList<>(); for (Field field : clazz.getFields()) { - if (Modifier.isStatic(field.getModifiers())) { + if (isStatic(field)) { continue; } list.add( diff --git a/core/src/main/java/org/apache/calcite/runtime/Resources.java b/core/src/main/java/org/apache/calcite/runtime/Resources.java index e6716533d9..a015541a38 100644 --- a/core/src/main/java/org/apache/calcite/runtime/Resources.java +++ b/core/src/main/java/org/apache/calcite/runtime/Resources.java @@ -31,7 +31,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; @@ -55,9 +54,12 @@ import java.util.ResourceBundle; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; +import static java.lang.reflect.Modifier.isStatic; + import static org.apache.calcite.linq4j.Nullness.castNonNull; import static java.util.Objects.requireNonNull; +import static org.apache.calcite.util.ReflectUtil.isStatic; /** * Defining wrapper classes around resources that allow the compiler to check @@ -233,7 +235,7 @@ public class Resources { public static void validate(Object o, EnumSet<Validation> validations) { int count = 0; for (Method method : o.getClass().getMethods()) { - if (!Modifier.isStatic(method.getModifiers()) + if (!isStatic(method) && Inst.class.isAssignableFrom(method.getReturnType())) { ++count; final Class<?>[] parameterTypes = method.getParameterTypes(); diff --git a/core/src/main/java/org/apache/calcite/schema/impl/AggregateFunctionImpl.java b/core/src/main/java/org/apache/calcite/schema/impl/AggregateFunctionImpl.java index b03f94daf6..e0a37fa86d 100644 --- a/core/src/main/java/org/apache/calcite/schema/impl/AggregateFunctionImpl.java +++ b/core/src/main/java/org/apache/calcite/schema/impl/AggregateFunctionImpl.java @@ -30,12 +30,13 @@ import com.google.common.collect.ImmutableList; import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.List; -import java.util.Objects; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.apache.calcite.util.Static.RESOURCE; +import static java.util.Objects.requireNonNull; + /** * Implementation of {@link AggregateFunction} via user-defined class. * The class should implement {@code A init()}, {@code A add(A, V)}, and @@ -72,11 +73,11 @@ public class AggregateFunctionImpl implements AggregateFunction, this.parameters = params; this.accumulatorType = accumulatorType; this.resultType = resultType; - this.initMethod = Objects.requireNonNull(initMethod, "initMethod"); - this.addMethod = Objects.requireNonNull(addMethod, "addMethod"); + this.initMethod = requireNonNull(initMethod, "initMethod"); + this.addMethod = requireNonNull(addMethod, "addMethod"); this.mergeMethod = mergeMethod; this.resultMethod = resultMethod; - this.isStatic = Modifier.isStatic(initMethod.getModifiers()); + this.isStatic = isStatic(initMethod); assert resultMethod != null || accumulatorType == resultType; } diff --git a/core/src/main/java/org/apache/calcite/schema/impl/ReflectiveFunctionBase.java b/core/src/main/java/org/apache/calcite/schema/impl/ReflectiveFunctionBase.java index ef28da6631..c6dd6d7fe4 100644 --- a/core/src/main/java/org/apache/calcite/schema/impl/ReflectiveFunctionBase.java +++ b/core/src/main/java/org/apache/calcite/schema/impl/ReflectiveFunctionBase.java @@ -29,10 +29,11 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; +import static org.apache.calcite.util.ReflectUtil.isPublic; + /** * Implementation of a function that is based on a method. * This class mainly solves conversion of method parameter types to {@code @@ -71,8 +72,7 @@ public abstract class ReflectiveFunctionBase implements Function { */ static boolean classHasPublicZeroArgsConstructor(Class<?> clazz) { for (Constructor<?> constructor : clazz.getConstructors()) { - if (constructor.getParameterCount() == 0 - && Modifier.isPublic(constructor.getModifiers())) { + if (constructor.getParameterCount() == 0 && isPublic(constructor)) { return true; } } @@ -91,7 +91,7 @@ public abstract class ReflectiveFunctionBase implements Function { for (Constructor<?> constructor : clazz.getConstructors()) { if (constructor.getParameterCount() == 1 && constructor.getParameterTypes()[0] == FunctionContext.class - && Modifier.isPublic(constructor.getModifiers())) { + && isPublic(constructor)) { return true; } } diff --git a/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java b/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java index 87b7011d32..1559a5c391 100644 --- a/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java +++ b/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java @@ -35,8 +35,8 @@ import com.google.common.collect.ImmutableMultimap; import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.apache.calcite.util.Static.RESOURCE; /** @@ -65,7 +65,7 @@ public class ScalarFunctionImpl extends ReflectiveFunctionBase if (method.getDeclaringClass() == Object.class) { continue; } - if (!Modifier.isStatic(method.getModifiers()) + if (!isStatic(method) && !classHasPublicZeroArgsConstructor(clazz)) { continue; } @@ -88,7 +88,7 @@ public class ScalarFunctionImpl extends ReflectiveFunctionBase if (method.getDeclaringClass() == Object.class) { continue; } - if (!Modifier.isStatic(method.getModifiers()) + if (!isStatic(method) && !classHasPublicZeroArgsConstructor(clazz)) { continue; } @@ -106,7 +106,7 @@ public class ScalarFunctionImpl extends ReflectiveFunctionBase /** * Creates {@link org.apache.calcite.schema.ScalarFunction} from given class. * - * <p>If a method of the given name is not found or it does not suit, + * <p>If a method of the given name is not found, or it does not suit, * returns {@code null}. * * @param clazz class that is used to implement the function @@ -129,7 +129,7 @@ public class ScalarFunctionImpl extends ReflectiveFunctionBase * @return created {@link ScalarFunction} or null */ public static ScalarFunction create(Method method) { - if (!Modifier.isStatic(method.getModifiers())) { + if (!isStatic(method)) { Class<?> clazz = method.getDeclaringClass(); if (!classHasPublicZeroArgsConstructor(clazz) && !classHasPublicFunctionContextConstructor(clazz)) { diff --git a/core/src/main/java/org/apache/calcite/schema/impl/TableFunctionImpl.java b/core/src/main/java/org/apache/calcite/schema/impl/TableFunctionImpl.java index 7a9a1c2da0..8d510093b0 100644 --- a/core/src/main/java/org/apache/calcite/schema/impl/TableFunctionImpl.java +++ b/core/src/main/java/org/apache/calcite/schema/impl/TableFunctionImpl.java @@ -39,11 +39,11 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.apache.calcite.util.Static.RESOURCE; import static java.util.Objects.requireNonNull; @@ -80,7 +80,7 @@ public class TableFunctionImpl extends ReflectiveFunctionBase /** Creates a {@link TableFunctionImpl} from a method. */ public static @Nullable TableFunction create(final Method method) { - if (!Modifier.isStatic(method.getModifiers())) { + if (!isStatic(method)) { Class clazz = method.getDeclaringClass(); if (!classHasPublicZeroArgsConstructor(clazz)) { throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex(); @@ -151,7 +151,7 @@ public class TableFunctionImpl extends ReflectiveFunctionBase private Table apply(List<? extends @Nullable Object> arguments) { try { Object o = null; - if (!Modifier.isStatic(method.getModifiers())) { + if (!isStatic(method)) { final Constructor<?> constructor = method.getDeclaringClass().getConstructor(); o = constructor.newInstance(); diff --git a/core/src/main/java/org/apache/calcite/schema/impl/TableMacroImpl.java b/core/src/main/java/org/apache/calcite/schema/impl/TableMacroImpl.java index 0b67897a58..527ff288c9 100644 --- a/core/src/main/java/org/apache/calcite/schema/impl/TableMacroImpl.java +++ b/core/src/main/java/org/apache/calcite/schema/impl/TableMacroImpl.java @@ -24,10 +24,10 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.apache.calcite.util.Static.RESOURCE; import static java.util.Objects.requireNonNull; @@ -57,7 +57,7 @@ public class TableMacroImpl extends ReflectiveFunctionBase /** Creates a {@code TableMacro} from a method. */ public static @Nullable TableMacro create(final Method method) { Class clazz = method.getDeclaringClass(); - if (!Modifier.isStatic(method.getModifiers())) { + if (!isStatic(method)) { if (!classHasPublicZeroArgsConstructor(clazz)) { throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex(); } @@ -78,7 +78,7 @@ public class TableMacroImpl extends ReflectiveFunctionBase @Override public TranslatableTable apply(List<? extends @Nullable Object> arguments) { try { Object o = null; - if (!Modifier.isStatic(method.getModifiers())) { + if (!isStatic(method)) { final Constructor<?> constructor = method.getDeclaringClass().getConstructor(); o = constructor.newInstance(); diff --git a/core/src/main/java/org/apache/calcite/sql2rel/ReflectiveConvertletTable.java b/core/src/main/java/org/apache/calcite/sql2rel/ReflectiveConvertletTable.java index d2ed1ba790..47be8816dd 100644 --- a/core/src/main/java/org/apache/calcite/sql2rel/ReflectiveConvertletTable.java +++ b/core/src/main/java/org/apache/calcite/sql2rel/ReflectiveConvertletTable.java @@ -30,10 +30,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; +import static org.apache.calcite.util.ReflectUtil.isPublic; + import static java.util.Objects.requireNonNull; /** @@ -67,7 +68,7 @@ public class ReflectiveConvertletTable implements SqlRexConvertletTable { private void registerNodeTypeMethod( @UnderInitialization ReflectiveConvertletTable this, final Method method) { - if (!Modifier.isPublic(method.getModifiers())) { + if (!isPublic(method)) { return; } if (!method.getName().startsWith("convert")) { @@ -110,7 +111,7 @@ public class ReflectiveConvertletTable implements SqlRexConvertletTable { private void registerOpTypeMethod( @UnderInitialization ReflectiveConvertletTable this, final Method method) { - if (!Modifier.isPublic(method.getModifiers())) { + if (!isPublic(method)) { return; } if (!method.getName().startsWith("convert")) { diff --git a/core/src/main/java/org/apache/calcite/util/ReflectUtil.java b/core/src/main/java/org/apache/calcite/util/ReflectUtil.java index 79bc2fd49f..57bc535d6e 100644 --- a/core/src/main/java/org/apache/calcite/util/ReflectUtil.java +++ b/core/src/main/java/org/apache/calcite/util/ReflectUtil.java @@ -25,6 +25,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.ByteBuffer; @@ -598,7 +599,7 @@ public abstract class ReflectUtil { * {@code foo(Object o, String s, int i, Number n, BigDecimal d} * </blockquote> * - * <p>To which which of those parameters could I pass a value that is an + * <p>To which of those parameters could I pass a value that is an * instance of {@link java.util.HashMap}? The answer: * * <ul> @@ -644,6 +645,16 @@ public abstract class ReflectUtil { } } + /** Returns whether a member (constructor, method or field) is public. */ + public static boolean isPublic(Member member) { + return Modifier.isPublic(member.getModifiers()); + } + + /** Returns whether a member (constructor, method or field) is static. */ + public static boolean isStatic(Member member) { + return Modifier.isStatic(member.getModifiers()); + } + //~ Inner Classes ---------------------------------------------------------- /** diff --git a/core/src/main/java/org/apache/calcite/util/Util.java b/core/src/main/java/org/apache/calcite/util/Util.java index 174779b8b2..eb90797400 100644 --- a/core/src/main/java/org/apache/calcite/util/Util.java +++ b/core/src/main/java/org/apache/calcite/util/Util.java @@ -68,7 +68,6 @@ import java.io.UncheckedIOException; import java.io.Writer; import java.lang.reflect.Array; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.net.MalformedURLException; import java.net.URL; @@ -115,6 +114,7 @@ import java.util.regex.Pattern; import java.util.stream.Collector; import static org.apache.calcite.linq4j.Nullness.castNonNull; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static java.util.Objects.requireNonNull; @@ -479,7 +479,7 @@ public class Util { Field[] fields = clazz.getFields(); int printed = 0; for (Field field : fields) { - if (Modifier.isStatic(field.getModifiers())) { + if (isStatic(field)) { continue; } if (printed++ > 0) { @@ -795,7 +795,7 @@ public class Util { * Returns whether s == null or if s.length() == 0. */ public static boolean isNullOrEmpty(@Nullable String s) { - return (null == s) || (s.length() == 0); + return s == null || s.isEmpty(); } /** @@ -863,7 +863,7 @@ public class Util { * rather than constructing intermediate strings. * * @see org.apache.calcite.linq4j.function.Functions#generate */ - public static <E> StringBuilder printList(StringBuilder sb, int elementCount, + public static StringBuilder printList(StringBuilder sb, int elementCount, ObjIntConsumer<StringBuilder> consumer) { if (elementCount == 0) { return sb.append("[]"); diff --git a/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java b/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java index bf7b793de2..7bd42b9af6 100644 --- a/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java +++ b/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java @@ -41,7 +41,6 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.sql.Connection; import java.sql.DriverManager; @@ -56,6 +55,7 @@ import java.util.Properties; import static org.apache.calcite.test.CalciteAssert.hr; import static org.apache.calcite.test.CalciteAssert.that; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -361,7 +361,7 @@ public class JdbcFrontLinqBackTest { final JavaTypeFactory javaTypeFactory = (JavaTypeFactory) typeFactory; final List<RelDataTypeField> list = new ArrayList<>(); for (Field field : Employee.class.getFields()) { - if (!Modifier.isStatic(field.getModifiers())) { + if (!isStatic(field)) { // FIXME: watch out for recursion final Type fieldType = field.getType(); final RelDataType relType = diff --git a/core/src/test/java/org/apache/calcite/test/SqlTestGen.java b/core/src/test/java/org/apache/calcite/test/SqlTestGen.java index 8e5c29b0df..0e7ef7dee2 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlTestGen.java +++ b/core/src/test/java/org/apache/calcite/test/SqlTestGen.java @@ -29,11 +29,13 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; import java.io.PrintWriter; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; +import static org.apache.calcite.util.ReflectUtil.isPublic; +import static org.apache.calcite.util.ReflectUtil.isStatic; + /** * Utility to generate a SQL script from validator test. */ @@ -75,8 +77,8 @@ class SqlTestGen { List<Method> list = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().startsWith("test") - && Modifier.isPublic(method.getModifiers()) - && !Modifier.isStatic(method.getModifiers()) + && isPublic(method) + && !isStatic(method) && (method.getParameterCount() == 0) && (method.getReturnType() == Void.TYPE)) { list.add(method); diff --git a/core/src/test/java/org/apache/calcite/util/UtilTest.java b/core/src/test/java/org/apache/calcite/util/UtilTest.java index cd002ccb73..7ac03ca622 100644 --- a/core/src/test/java/org/apache/calcite/util/UtilTest.java +++ b/core/src/test/java/org/apache/calcite/util/UtilTest.java @@ -62,7 +62,6 @@ import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.lang.management.MemoryType; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.sql.Timestamp; import java.text.MessageFormat; @@ -106,6 +105,7 @@ import java.util.function.Predicate; import java.util.function.UnaryOperator; import static org.apache.calcite.test.Matchers.isLinux; +import static org.apache.calcite.util.ReflectUtil.isStatic; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.equalTo; @@ -1956,7 +1956,7 @@ class UtilTest { private void checkResourceMethodNames(Object resource) { for (Method method : resource.getClass().getMethods()) { - if (!Modifier.isStatic(method.getModifiers()) + if (!isStatic(method) && !method.getName().matches("^[a-z][A-Za-z0-9_]*$")) { fail("resource method name must be camel case: " + method.getName()); } diff --git a/geode/src/main/java/org/apache/calcite/adapter/geode/util/JavaTypeFactoryExtImpl.java b/geode/src/main/java/org/apache/calcite/adapter/geode/util/JavaTypeFactoryExtImpl.java index 6112caff48..e23fbcdbda 100644 --- a/geode/src/main/java/org/apache/calcite/adapter/geode/util/JavaTypeFactoryExtImpl.java +++ b/geode/src/main/java/org/apache/calcite/adapter/geode/util/JavaTypeFactoryExtImpl.java @@ -27,12 +27,13 @@ import org.apache.calcite.rel.type.RelRecordType; import org.apache.geode.pdx.PdxInstance; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Map; +import static org.apache.calcite.util.ReflectUtil.isStatic; + /** * Implementation of {@link JavaTypeFactory}. * @@ -50,7 +51,7 @@ public class JavaTypeFactoryExtImpl final List<RelDataTypeField> list = new ArrayList<>(); for (Field field : type.getDeclaredFields()) { - if (!Modifier.isStatic(field.getModifiers())) { + if (!isStatic(field)) { // FIXME: watch out for recursion final Type fieldType = field.getType(); list.add( diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java index 633be3afac..45f07c605a 100644 --- a/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java +++ b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java @@ -19,11 +19,12 @@ package org.apache.calcite.piglet; import com.google.common.collect.ImmutableMap; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import static org.apache.calcite.util.ReflectUtil.isPublic; + /** * Utility class to find the implementation method object for a given Pig UDF * class. @@ -45,7 +46,7 @@ class PigUdfFinder { PigUdfFinder() { final Map<String, Method> map = new HashMap<>(); for (Method method : PigUdfs.class.getMethods()) { - if (Modifier.isPublic(method.getModifiers()) + if (isPublic(method) && method.getReturnType() != Method.class) { map.put(method.getName(), method); }
