8310849: Pattern matching for instanceof and arrayType cleanup in j.l.invoke and j.l.reflect

Reviewed-by: mchung, darcy
This commit is contained in:
Chen Liang 2023-06-27 16:10:50 +00:00 committed by Mandy Chung
parent 7ce967a10c
commit 2bd4136bdb
34 changed files with 211 additions and 292 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -95,8 +95,8 @@ public final class AnnotatedTypeFactory {
} else if (type instanceof ParameterizedType t) {
if (t.getOwnerType() == null)
return addTo;
if (t.getRawType() instanceof Class
&& Modifier.isStatic(((Class) t.getRawType()).getModifiers()))
if (t.getRawType() instanceof Class<?> c
&& Modifier.isStatic(c.getModifiers()))
return addTo;
return nestingForType(t.getOwnerType(), addTo.pushInner());
}
@ -178,10 +178,9 @@ public final class AnnotatedTypeFactory {
@Override
public AnnotatedType getAnnotatedOwnerType() {
if (!(type instanceof Class<?>))
if (!(type instanceof Class<?> nested))
throw new IllegalStateException("Can't compute owner");
Class<?> nested = (Class<?>)type;
Class<?> owner = nested.getDeclaringClass();
if (owner == null) // top-level, local or anonymous
return null;
@ -250,16 +249,12 @@ public final class AnnotatedTypeFactory {
@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedType &&
return o instanceof AnnotatedType that &&
!(o instanceof AnnotatedArrayType) &&
!(o instanceof AnnotatedTypeVariable) &&
!(o instanceof AnnotatedParameterizedType) &&
!(o instanceof AnnotatedWildcardType)) {
AnnotatedType that = (AnnotatedType) o;
return equalsTypeAndAnnotations(that);
} else {
return false;
}
!(o instanceof AnnotatedWildcardType) &&
equalsTypeAndAnnotations(that);
}
@Override
@ -334,13 +329,10 @@ public final class AnnotatedTypeFactory {
@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedArrayType that) {
return equalsTypeAndAnnotations(that) &&
return o instanceof AnnotatedArrayType that &&
equalsTypeAndAnnotations(that) &&
Objects.equals(getAnnotatedGenericComponentType(),
that.getAnnotatedGenericComponentType());
} else {
return false;
}
}
@Override
@ -378,11 +370,8 @@ public final class AnnotatedTypeFactory {
@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedTypeVariable that) {
return equalsTypeAndAnnotations(that);
} else {
return false;
}
return o instanceof AnnotatedTypeVariable that
&& equalsTypeAndAnnotations(that);
}
}
@ -457,12 +446,9 @@ public final class AnnotatedTypeFactory {
@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedParameterizedType that) {
return equalsTypeAndAnnotations(that) &&
return o instanceof AnnotatedParameterizedType that &&
equalsTypeAndAnnotations(that) &&
Arrays.equals(getAnnotatedActualTypeArguments(), that.getAnnotatedActualTypeArguments());
} else {
return false;
}
}
@Override
@ -568,15 +554,12 @@ public final class AnnotatedTypeFactory {
@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedWildcardType that) {
return equalsTypeAndAnnotations(that) &&
return o instanceof AnnotatedWildcardType that &&
equalsTypeAndAnnotations(that) &&
// Treats ordering as significant
Arrays.equals(getAnnotatedLowerBounds(), that.getAnnotatedLowerBounds()) &&
// Treats ordering as significant
Arrays.equals(getAnnotatedUpperBounds(), that.getAnnotatedUpperBounds());
} else {
return false;
}
}
@Override

View file

@ -434,8 +434,8 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
// Check for array of string, class, enum const, annotation,
// or ExceptionProxy
if (v1 instanceof Object[] && v2 instanceof Object[])
return Arrays.equals((Object[]) v1, (Object[]) v2);
if (v1 instanceof Object[] a1 && v2 instanceof Object[] a2)
return Arrays.equals(a1, a2);
// Check for ill formed annotation(s)
if (v2.getClass() != type)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -445,9 +445,8 @@ public class AnnotationParser {
}
static Class<?> toClass(Type o) {
if (o instanceof GenericArrayType gat)
return Array.newInstance(toClass(gat.getGenericComponentType()), 0)
.getClass();
return (Class)o;
return toClass(gat.getGenericComponentType()).arrayType();
return (Class<?>) o;
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,6 @@
package sun.reflect.generics.factory;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
@ -59,12 +58,12 @@ public class CoreReflectionFactory implements GenericsFactory {
private ClassLoader getDeclsLoader() {
if (decl instanceof Class) {return ((Class) decl).getClassLoader();}
if (decl instanceof Method) {
return ((Method) decl).getDeclaringClass().getClassLoader();
if (decl instanceof Class<?> c) {return c.getClassLoader();}
if (decl instanceof Method m) {
return m.getDeclaringClass().getClassLoader();
}
assert decl instanceof Constructor : "Constructor expected";
return ((Constructor) decl).getDeclaringClass().getClassLoader();
return ((Constructor<?>) decl).getDeclaringClass().getClassLoader();
}
@ -119,8 +118,8 @@ public class CoreReflectionFactory implements GenericsFactory {
}
public Type makeArrayType(Type componentType){
if (componentType instanceof Class<?>)
return Array.newInstance((Class<?>) componentType, 0).getClass();
if (componentType instanceof Class<?> ct)
return ct.arrayType();
else
return GenericArrayTypeImpl.make(componentType);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -70,12 +70,8 @@ public class GenericArrayTypeImpl
@Override
public boolean equals(Object o) {
if (o instanceof GenericArrayType) {
GenericArrayType that = (GenericArrayType) o;
return Objects.equals(genericComponentType, that.getGenericComponentType());
} else
return false;
return o instanceof GenericArrayType that
&& Objects.equals(genericComponentType, that.getGenericComponentType());
}
@Override

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,10 +25,7 @@
package sun.reflect.generics.reflectiveObjects;
import sun.reflect.generics.tree.FieldTypeSignature;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
@ -165,42 +162,13 @@ public class ParameterizedTypeImpl implements ParameterizedType {
*/
@Override
public boolean equals(Object o) {
if (o instanceof ParameterizedType) {
// Check that information is equivalent
ParameterizedType that = (ParameterizedType) o;
if (this == that)
return true;
Type thatOwner = that.getOwnerType();
Type thatRawType = that.getRawType();
if (false) { // Debugging
boolean ownerEquality = (ownerType == null ?
thatOwner == null :
ownerType.equals(thatOwner));
boolean rawEquality = (rawType == null ?
thatRawType == null :
rawType.equals(thatRawType));
boolean typeArgEquality = Arrays.equals(actualTypeArguments, // avoid clone
that.getActualTypeArguments());
for (Type t : actualTypeArguments) {
System.out.printf("\t\t%s%s%n", t, t.getClass());
}
System.out.printf("\towner %s\traw %s\ttypeArg %s%n",
ownerEquality, rawEquality, typeArgEquality);
return ownerEquality && rawEquality && typeArgEquality;
}
return
Objects.equals(ownerType, thatOwner) &&
Objects.equals(rawType, thatRawType) &&
if (this == o)
return true;
return o instanceof ParameterizedType that &&
Objects.equals(ownerType, that.getOwnerType()) &&
Objects.equals(rawType, that.getRawType()) &&
Arrays.equals(actualTypeArguments, // avoid clone
that.getActualTypeArguments());
} else
return false;
}
@Override
@ -219,10 +187,10 @@ public class ParameterizedTypeImpl implements ParameterizedType {
sb.append("$");
if (ownerType instanceof ParameterizedTypeImpl) {
if (ownerType instanceof ParameterizedTypeImpl pt) {
// Find simple name of nested type by removing the
// shared prefix with owner.
sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
sb.append(rawType.getName().replace(pt.rawType.getName() + "$",
""));
} else
sb.append(rawType.getSimpleName());

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,6 @@ package sun.reflect.generics.reflectiveObjects;
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Member;
@ -42,7 +41,6 @@ import sun.reflect.annotation.TypeAnnotationParser;
import sun.reflect.annotation.AnnotationType;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.tree.FieldTypeSignature;
import sun.reflect.generics.visitor.Reifier;
import sun.reflect.misc.ReflectUtil;
/**
@ -94,7 +92,7 @@ public class TypeVariableImpl<D extends GenericDeclaration>
throw new AssertionError("Unexpected kind of GenericDeclaration" +
decl.getClass().toString());
}
return new TypeVariableImpl<T>(decl, name, bs, f);
return new TypeVariableImpl<>(decl, name, bs, f);
}
@ -121,8 +119,8 @@ public class TypeVariableImpl<D extends GenericDeclaration>
*/
public Type[] getBounds() {
Object[] value = bounds;
if (value instanceof FieldTypeSignature[]) {
value = reifyBounds((FieldTypeSignature[])value);
if (value instanceof FieldTypeSignature[] sigs) {
value = reifyBounds(sigs);
bounds = value;
}
return (Type[])value.clone();
@ -137,8 +135,8 @@ public class TypeVariableImpl<D extends GenericDeclaration>
* @since 1.5
*/
public D getGenericDeclaration() {
if (genericDeclaration instanceof Class)
ReflectUtil.checkPackageAccess((Class)genericDeclaration);
if (genericDeclaration instanceof Class<?> c)
ReflectUtil.checkPackageAccess(c);
else if ((genericDeclaration instanceof Method) ||
(genericDeclaration instanceof Constructor))
ReflectUtil.conservativeCheckMemberAccess((Member)genericDeclaration);
@ -159,18 +157,10 @@ public class TypeVariableImpl<D extends GenericDeclaration>
@Override
public boolean equals(Object o) {
if (o instanceof TypeVariable &&
o.getClass() == TypeVariableImpl.class) {
TypeVariable<?> that = (TypeVariable<?>) o;
GenericDeclaration thatDecl = that.getGenericDeclaration();
String thatName = that.getName();
return Objects.equals(genericDeclaration, thatDecl) &&
Objects.equals(name, thatName);
} else
return false;
return o instanceof TypeVariable<?> that &&
o.getClass() == TypeVariableImpl.class &&
Objects.equals(genericDeclaration, that.getGenericDeclaration()) &&
Objects.equals(name, that.getName());
}
@Override

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,7 +30,6 @@ import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.tree.FieldTypeSignature;
import sun.reflect.generics.visitor.Reifier;
import java.util.Arrays;
import java.util.StringJoiner;
@ -101,8 +100,8 @@ public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
*/
public Type[] getUpperBounds() {
Object[] value = upperBounds;
if (value instanceof FieldTypeSignature[]) {
value = reifyBounds((FieldTypeSignature[])value);
if (value instanceof FieldTypeSignature[] sigs) {
value = reifyBounds(sigs);
upperBounds = value;
}
return (Type[])value.clone();
@ -132,8 +131,8 @@ public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
*/
public Type[] getLowerBounds() {
Object[] value = lowerBounds;
if (value instanceof FieldTypeSignature[]) {
value = reifyBounds((FieldTypeSignature[])value);
if (value instanceof FieldTypeSignature[] sigs) {
value = reifyBounds(sigs);
lowerBounds = value;
}
return (Type[])value.clone();
@ -168,15 +167,9 @@ public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
@Override
public boolean equals(Object o) {
if (o instanceof WildcardType) {
WildcardType that = (WildcardType) o;
return
Arrays.equals(this.getLowerBounds(),
that.getLowerBounds()) &&
Arrays.equals(this.getUpperBounds(),
that.getUpperBounds());
} else
return false;
return o instanceof WildcardType that
&& Arrays.equals(this.getLowerBounds(), that.getLowerBounds())
&& Arrays.equals(this.getUpperBounds(), that.getUpperBounds());
}
@Override

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -98,14 +98,14 @@ public final class MethodUtil extends SecureClassLoader {
} catch (InvocationTargetException ie) {
Throwable t = ie.getCause();
if (t instanceof InvocationTargetException) {
throw (InvocationTargetException)t;
} else if (t instanceof IllegalAccessException) {
throw (IllegalAccessException)t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException)t;
} else if (t instanceof Error) {
throw (Error)t;
if (t instanceof InvocationTargetException ite) {
throw ite;
} else if (t instanceof IllegalAccessException iae) {
throw iae;
} else if (t instanceof RuntimeException re) {
throw re;
} else if (t instanceof Error error) {
throw error;
} else {
throw new Error("Unexpected invocation error", t);
}