Skip to content

Commit

Permalink
Use Any.is more broadly
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Mar 22, 2024
1 parent 08b66b7 commit 0f61b79
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ private void captureTypeVarsFromCandidate(Type srcType, OpCandidate candidate,
var existing = map.get(key);
var replacement = assigns.get(key);
// Ignore bounds that are weaker than current bounds.
if (Types.isAssignable(existing, replacement) && !existing.equals(
Any.class) && !(existing instanceof Any))
{
if (Types.isAssignable(existing, replacement) && !Any.is(existing)) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private Type mapAnys(Type opType, OpInfo info) {
info.opType() }, infoMap);
for (var key : reqMap.keySet()) {
var val = reqMap.get(key);
if (val.equals(Any.class) || val instanceof Any) {
if (Any.is(val)) {
reqMap.put(key, infoMap.get(key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static int checkGenericOutputsAssignability(Type[] froms, Type[] tos,
Type from = froms[i];
Type to = tos[i];

if (to instanceof Any || to.equals(Any.class)) continue;
if (Any.is(to)) continue;

if (from instanceof TypeVariable) {
TypeVarInfo typeVarInfo = typeBounds.get(from);
Expand Down
27 changes: 11 additions & 16 deletions scijava-types/src/main/java/org/scijava/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ public static Type greatestCommonSuperType(Type[] types,
final boolean wildcardSingleIface)
{

types = Arrays.stream(types).filter(t -> !(t.equals(Any.class) ||
t instanceof Any)).toArray(Type[]::new);
types = Arrays.stream(types).filter(t -> !(Any.is(t))).toArray(Type[]::new);

// return answer quick if the answer is trivial
if (types.length == 0) return null;
Expand Down Expand Up @@ -679,7 +678,7 @@ public static boolean typesSatisfyVariables(
private static boolean isApplicableToRawTypes(final Type arg,
final Type param)
{
if (arg instanceof Any || arg.equals(Any.class)) return true;
if (Any.is(arg)) return true;
final List<Class<?>> srcClasses = Types.raws(arg);
final List<Class<?>> destClasses = Types.raws(param);
for (final Class<?> destClass : destClasses) {
Expand Down Expand Up @@ -729,7 +728,7 @@ private static boolean isApplicableToParameterizedTypes(final Type arg,
if (destType instanceof TypeVariable<?>) {
final Type srcType = srcTypes[i];
final TypeVariable<?> destTypeVar = (TypeVariable<?>) destType;
if (srcType instanceof Any || srcType.equals(Any.class)) continue;
if (Any.is(srcType)) continue;
if (!isApplicableToTypeParameter(srcType, destTypeVar, typeBounds))
return false;
ignoredIndices.add(i);
Expand Down Expand Up @@ -1674,7 +1673,7 @@ private static boolean isAssignable(final Type type, final Type toType,
return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns);
}

if (toType instanceof Any) {
if (Any.is(toType)) {
return isAssignable(type, (Any) toType, typeVarAssigns);
}

Expand Down Expand Up @@ -1787,7 +1786,7 @@ private static boolean isAssignable(final Type type,
return false;
}

if (type instanceof Any) return true;
if (Any.is(type)) return true;

throw new IllegalStateException("found an unhandled type: " + type);
}
Expand Down Expand Up @@ -1941,9 +1940,7 @@ else if (!isAssignable(fromResolved == null ? fromTypeArg
// parameters of the target type.
if (fromResolved != null && !fromResolved.equals(toResolved)) {
// check for anys
if (fromResolved instanceof Any || toResolved instanceof Any ||
fromResolved.equals(Any.class) || toResolved.equals(Any.class))
continue;
if (Any.is(fromResolved) || Any.is(toResolved)) continue;
if (fromResolved instanceof ParameterizedType &&
toResolved instanceof ParameterizedType)
{
Expand All @@ -1961,9 +1958,7 @@ else if (!isAssignable(fromResolved == null ? fromTypeArg
typeVarAssigns.put((TypeVariable<?>) toTypes[i], fromTypes[i]);
continue;
}
if (!(fromTypes[i] instanceof Any || toTypes[i] instanceof Any ||
fromTypes[i].equals(Any.class) || toTypes[i].equals(Any.class)))
return false;
if (!(Any.is(fromTypes[i]) || Any.is(toTypes[i]))) return false;
}
continue;
}
Expand Down Expand Up @@ -2131,7 +2126,7 @@ private static boolean isAssignable(final Type type,
final GenericArrayType toGenericArrayType,
final Map<TypeVariable<?>, Type> typeVarAssigns)
{
if (type == null || type instanceof Any || type.equals(Any.class)) {
if (type == null || Any.is(type)) {
return true;
}

Expand Down Expand Up @@ -2211,7 +2206,7 @@ private static boolean isAssignable(final Type type,
final WildcardType toWildcardType,
final Map<TypeVariable<?>, Type> typeVarAssigns)
{
if (type == null || type instanceof Any || type.equals(Any.class)) {
if (type == null || Any.is(type)) {
return true;
}

Expand Down Expand Up @@ -2355,7 +2350,7 @@ private static boolean isAssignable(final Type type,
return true;
}

if (type instanceof Any || type.equals(Any.class)) {
if (Any.is(type)) {
typeVarAssigns.put(toTypeVariable, new Any(toTypeVariable.getBounds()));
return true;
}
Expand Down Expand Up @@ -3496,7 +3491,7 @@ private static String toString(final Type type, final Set<Type> done) {
if (type instanceof Class) {
return classToString((Class<?>) type, done);
}
if (type instanceof Any) {
if (Any.is(type)) {
return type.toString();
}
if (type instanceof ParameterizedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private static void inferTypeVariables(Class<?> type, Type inferFrom,
Type current = typeVarAssigns.putIfAbsent((TypeVariable<?>) inferFrom,
type);
if (current != null) {
if (current instanceof Any) {
if (Any.is(current)) {
typeVarAssigns.put((TypeVariable<?>) inferFrom, type);
}
else if (!Objects.equal(type, current)) {
Expand Down Expand Up @@ -491,7 +491,7 @@ private static void inferTypeVariables(ParameterizedType type, Type inferFrom,
if (inferFrom instanceof WildcardType) {
inferFrom = getInferrableBound((WildcardType) inferFrom);
}
if (inferFrom instanceof Any || inferFrom.equals(Any.class)) {
if (Any.is(inferFrom)) {
mapTypeVarsToAny(type, typeMappings);
return;
}
Expand Down

0 comments on commit 0f61b79

Please sign in to comment.