Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Rearrange TypeUtils and ArrayTypeUtils so they can be shared with JS API #5780

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 96 additions & 3 deletions Generators/src/main/java/io/deephaven/gen/JavaFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
//
package io.deephaven.gen;

import io.deephaven.util.type.TypeUtils;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;

/**
* A Java function description for use in code generation.
*
* <p>
* JavaFunctions are equal if they have the same method names and parameter types.
*/
public class JavaFunction implements Comparable<JavaFunction> {
Expand Down Expand Up @@ -147,13 +151,102 @@ public Class<?> getReturnClass() {
}

try {
return TypeUtils.getErasedType(returnType);
return getErasedType(returnType);
} catch (UnsupportedOperationException e) {
log.warning("Unable to determine Class from returnType=" + returnType.getTypeName());
return null;
}
}

/**
* Determine the Class from the Type.
*/
private static Class<?> getErasedType(Type paramType) {
if (paramType instanceof Class) {
return (Class<?>) paramType;
} else if (paramType instanceof ParameterizedType) {
return (Class<?>) // We are asking the parameterized type for its raw type, which is always Class
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
((ParameterizedType) paramType).getRawType();
} else if (paramType instanceof WildcardType) {
final Type[] upper = ((WildcardType) paramType).getUpperBounds();
return getErasedType(upper[0]);
} else if (paramType instanceof java.lang.reflect.TypeVariable) {
final Type[] bounds = ((TypeVariable<?>) paramType).getBounds();
if (bounds.length > 1) {
Class<?>[] erasedBounds = new Class[bounds.length];
Class<?> weakest = null;
for (int i = 0; i < erasedBounds.length; i++) {
erasedBounds[i] = getErasedType(bounds[i]);
if (i == 0) {
weakest = erasedBounds[i];
} else {
weakest = getWeakest(weakest, erasedBounds[i]);
}
// If we are erased to object, stop erasing...
if (weakest == Object.class) {
break;
}
}
return weakest;
}
return getErasedType(bounds[0]);
} else {
throw new UnsupportedOperationException();
}
}

/**
* Determine the weakest parent of the two provided Classes.
*
* @param one one class to compare
* @param two the other class to compare
* @return the weakest parent Class
*/
private static Class<?> getWeakest(Class<?> one, Class<?> two) {
if (one.isAssignableFrom(two)) {
return one;
} else if (two.isAssignableFrom(one)) {
return two;
}
// No luck on quick check... Look in interfaces.
Set<Class<?>> oneInterfaces = getFlattenedInterfaces(one);
Set<Class<?>> twoInterfaces = getFlattenedInterfaces(two);
// Keep only shared interfaces
oneInterfaces.retainAll(twoInterfaces);
Class<?> strongest = Object.class;
for (Class<?> cls : oneInterfaces) {
// There is a winning type...
if (strongest.isAssignableFrom(cls)) {
strongest = cls;
} else if (!cls.isAssignableFrom(strongest)) {
return Object.class;
}
}
// Will be Object.class if there were no shared interfaces (or shared interfaces were not compatible).
return strongest;
}

private static Set<Class<?>> getFlattenedInterfaces(Class<?> cls) {
final Set<Class<?>> set = new HashSet<>();
while (cls != null && cls != Object.class) {
for (Class<?> iface : cls.getInterfaces()) {
collectInterfaces(set, iface);
}
cls = cls.getSuperclass();
}
return set;
}

private static void collectInterfaces(final Collection<Class<?>> into, final Class<?> cls) {
if (into.add(cls)) {
for (final Class<?> iface : cls.getInterfaces()) {
if (into.add(iface)) {
collectInterfaces(into, iface);
}
}
}
}

public Type[] getParameterTypes() {
return parameterTypes;
}
Expand Down
48 changes: 24 additions & 24 deletions Plot/src/main/java/io/deephaven/plot/util/ArgumentValidations.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package io.deephaven.plot.util;

import io.deephaven.base.verify.Require;
import io.deephaven.base.verify.RequirementFailure;
import io.deephaven.configuration.Configuration;
import io.deephaven.engine.table.ColumnDefinition;
import io.deephaven.plot.datasets.data.IndexableNumericData;
Expand All @@ -13,6 +12,7 @@
import io.deephaven.plot.util.tables.TableHandle;
import io.deephaven.engine.table.Table;
import io.deephaven.engine.table.TableDefinition;
import io.deephaven.util.type.NumericTypeUtils;
import io.deephaven.util.type.TypeUtils;
import org.apache.commons.lang3.ClassUtils;

Expand Down Expand Up @@ -362,24 +362,24 @@ public static boolean isTime(final Class c, final PlotInfo plotInfo) {
}

/**
* Whether the class is {@link TypeUtils#isNumeric(Class)} or {@link #isTime(Class, PlotInfo)}
* Whether the class is {@link NumericTypeUtils#isNumeric(Class)} or {@link #isTime(Class, PlotInfo)}
*
* @param c class
* @return true if {@code c} is a numeric or time class, false otherwise
*/
public static boolean isNumericOrTime(final Class c) {
return TypeUtils.isNumeric(c) || isTime(c, null);
return NumericTypeUtils.isNumeric(c) || isTime(c, null);
}

/**
* Whether the class is {@link TypeUtils#isNumeric(Class)} or {@link #isTime(Class, PlotInfo)}
* Whether the class is {@link NumericTypeUtils#isNumeric(Class)} or {@link #isTime(Class, PlotInfo)}
*
* @param c class
* @param plotInfo source of the exception
* @return true if {@code c} is a numeric or time class, false otherwise
*/
public static boolean isNumericOrTime(final Class c, final PlotInfo plotInfo) {
return TypeUtils.isNumeric(c) || isTime(c, plotInfo);
return NumericTypeUtils.isNumeric(c) || isTime(c, plotInfo);
}

/**
Expand Down Expand Up @@ -419,7 +419,7 @@ public static boolean isTime(final SelectableDataSet sds, final String column, f
}

/**
* Whether the column's data type {@link TypeUtils#isPrimitiveNumeric(Class)}.
* Whether the column's data type {@link NumericTypeUtils#isPrimitiveNumeric(Class)}.
*
* @param t table
* @param column column
Expand All @@ -428,11 +428,11 @@ public static boolean isTime(final SelectableDataSet sds, final String column, f
*/
public static boolean isPrimitiveNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
return TypeUtils.isPrimitiveNumeric(getColumnType(t, column, plotInfo));
return NumericTypeUtils.isPrimitiveNumeric(getColumnType(t, column, plotInfo));
}

/**
* Whether the column's data type {@link TypeUtils#isBoxedNumeric(Class)}.
* Whether the column's data type {@link NumericTypeUtils#isBoxedNumeric(Class)}.
*
* @param t table
* @param column column
Expand All @@ -441,11 +441,11 @@ public static boolean isPrimitiveNumeric(final Table t, final String column, fin
*/
public static boolean isBoxedNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
return TypeUtils.isBoxedNumeric(getColumnType(t, column, plotInfo));
return NumericTypeUtils.isBoxedNumeric(getColumnType(t, column, plotInfo));
}

/**
* Whether the column's data type {@link TypeUtils#isNumeric(Class)}.
* Whether the column's data type {@link NumericTypeUtils#isNumeric(Class)}.
*
* @param t table
* @param column column
Expand All @@ -454,11 +454,11 @@ public static boolean isBoxedNumeric(final Table t, final String column, final P
*/
public static boolean isNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
return TypeUtils.isNumeric(getColumnType(t, column, plotInfo));
return NumericTypeUtils.isNumeric(getColumnType(t, column, plotInfo));
}

/**
* Whether the column's data type {@link TypeUtils#isNumeric(Class)}.
* Whether the column's data type {@link NumericTypeUtils#isNumeric(Class)}.
*
* @param t table
* @param column column
Expand All @@ -467,11 +467,11 @@ public static boolean isNumeric(final Table t, final String column, final PlotIn
*/
public static boolean isNumeric(final TableDefinition t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
return TypeUtils.isNumeric(getColumnType(t, column, plotInfo));
return NumericTypeUtils.isNumeric(getColumnType(t, column, plotInfo));
}

/**
* Whether the column's data type {@link TypeUtils#isNumeric(Class)}.
* Whether the column's data type {@link NumericTypeUtils#isNumeric(Class)}.
*
* @param sds selectable dataset
* @param column column
Expand All @@ -480,7 +480,7 @@ public static boolean isNumeric(final TableDefinition t, final String column, fi
*/
public static boolean isNumeric(final SelectableDataSet sds, final String column, final PlotInfo plotInfo) {
assertNotNull(sds, "t", plotInfo);
return TypeUtils.isNumeric(getColumnType(sds, column, plotInfo));
return NumericTypeUtils.isNumeric(getColumnType(sds, column, plotInfo));
}

/**
Expand Down Expand Up @@ -583,7 +583,7 @@ public static void assertIsTime(final TableDefinition t, final String column, fi

/**
* Requires the column's data type to be a numeric primitive as defined in
* {@link TypeUtils#isPrimitiveNumeric(Class)}
* {@link NumericTypeUtils#isPrimitiveNumeric(Class)}
*
* @throws RuntimeException if the column's data type isn't a numeric primitive
* @param t table
Expand All @@ -598,7 +598,7 @@ public static void assertIsPrimitiveNumeric(final Table t, final String column,

/**
* Requires the column's data type to be a numeric primitive as defined in
* {@link TypeUtils#isPrimitiveNumeric(Class)}
* {@link NumericTypeUtils#isPrimitiveNumeric(Class)}
*
* @throws RuntimeException if the column's data type isn't a numeric primitive
* @param t table
Expand All @@ -616,7 +616,7 @@ public static void assertIsPrimitiveNumeric(final Table t, final String column,

/**
* Requires the column's data type to be an instance of {@link Number} as defined in
* {@link TypeUtils#isBoxedNumeric(Class)}
* {@link NumericTypeUtils#isBoxedNumeric(Class)}
*
* @throws RuntimeException if the column's data type isn't an instance of {@link Number}
* @param t table
Expand All @@ -631,7 +631,7 @@ public static void assertIsBoxedNumeric(final Table t, final String column, fina

/**
* Requires the column's data type to be an instance of {@link Number} as defined in
* {@link TypeUtils#isBoxedNumeric(Class)}
* {@link NumericTypeUtils#isBoxedNumeric(Class)}
*
* @throws RuntimeException if the column's data type isn't an instance of {@link Number}
* @param t table
Expand All @@ -649,7 +649,7 @@ public static void assertIsBoxedNumeric(final Table t, final String column, fina


/**
* Requires the column's data type to be a numeric instance as defined in {@link TypeUtils#isNumeric(Class)}
* Requires the column's data type to be a numeric instance as defined in {@link NumericTypeUtils#isNumeric(Class)}
*
* @throws PlotRuntimeException if the column's data type isn't a numeric instance
* @param t table
Expand All @@ -663,7 +663,7 @@ public static void assertIsNumeric(final Table t, final String column, final Plo


/**
* Requires the column's data type to be a numeric instance as defined in {@link TypeUtils#isNumeric(Class)}
* Requires the column's data type to be a numeric instance as defined in {@link NumericTypeUtils#isNumeric(Class)}
*
* @throws PlotRuntimeException if the column's data type isn't a numeric instance
* @param t table
Expand All @@ -677,7 +677,7 @@ public static void assertIsNumeric(final TableDefinition t, final String column,


/**
* Requires the column's data type to be a numeric instance as defined in {@link TypeUtils#isNumeric(Class)}
* Requires the column's data type to be a numeric instance as defined in {@link NumericTypeUtils#isNumeric(Class)}
*
* @throws PlotRuntimeException if the column's data type isn't a numeric instance
* @param t table
Expand All @@ -695,7 +695,7 @@ public static void assertIsNumeric(final Table t, final String column, final Str


/**
* Requires the column's data type to be a numeric instance as defined in {@link TypeUtils#isNumeric(Class)}
* Requires the column's data type to be a numeric instance as defined in {@link NumericTypeUtils#isNumeric(Class)}
*
* @throws PlotRuntimeException if the column's data type isn't a numeric instance
* @param t table
Expand All @@ -713,7 +713,7 @@ public static void assertIsNumeric(final TableDefinition t, final String column,


/**
* Requires the column's data type to be a numeric instance as defined in {@link TypeUtils#isNumeric(Class)}
* Requires the column's data type to be a numeric instance as defined in {@link NumericTypeUtils#isNumeric(Class)}
*
* @throws PlotRuntimeException if the column's data type isn't a numeric instance
* @param sds selectable dataset
Expand Down
Loading
Loading