Skip to content

Commit

Permalink
8288227: Refactor annotation implementation to use pattern matching f…
Browse files Browse the repository at this point in the history
…or instanceof

Reviewed-by: alanb
  • Loading branch information
jddarcy committed Jun 10, 2022
1 parent 2cc40af commit aaa8971
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, 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
Expand Down Expand Up @@ -69,20 +69,20 @@ public static AnnotatedType buildAnnotatedType(Type type,
actualTypeAnnos,
allOnSameTarget,
decl);
} else if (type instanceof TypeVariable) {
return new AnnotatedTypeVariableImpl((TypeVariable)type,
} else if (type instanceof TypeVariable<?> typeVariable) {
return new AnnotatedTypeVariableImpl(typeVariable,
currentLoc,
actualTypeAnnos,
allOnSameTarget,
decl);
} else if (type instanceof ParameterizedType) {
return new AnnotatedParameterizedTypeImpl((ParameterizedType)type,
} else if (type instanceof ParameterizedType paramType) {
return new AnnotatedParameterizedTypeImpl(paramType,
currentLoc,
actualTypeAnnos,
allOnSameTarget,
decl);
} else if (type instanceof WildcardType) {
return new AnnotatedWildcardTypeImpl((WildcardType) type,
} else if (type instanceof WildcardType wildType) {
return new AnnotatedWildcardTypeImpl(wildType,
currentLoc,
actualTypeAnnos,
allOnSameTarget,
Expand All @@ -94,15 +94,13 @@ public static AnnotatedType buildAnnotatedType(Type type,
public static LocationInfo nestingForType(Type type, LocationInfo addTo) {
if (isArray(type))
return addTo;
if (type instanceof Class) {
Class<?> clz = (Class)type;
if (type instanceof Class<?> clz) {
if (clz.getEnclosingClass() == null)
return addTo;
if (Modifier.isStatic(clz.getModifiers()))
return addTo;
return nestingForType(clz.getEnclosingClass(), addTo.pushInner());
} else if (type instanceof ParameterizedType) {
ParameterizedType t = (ParameterizedType)type;
} else if (type instanceof ParameterizedType t) {
if (t.getOwnerType() == null)
return addTo;
if (t.getRawType() instanceof Class
Expand All @@ -114,8 +112,7 @@ public static LocationInfo nestingForType(Type type, LocationInfo addTo) {
}

private static boolean isArray(Type t) {
if (t instanceof Class) {
Class<?> c = (Class)t;
if (t instanceof Class<?> c) {
if (c.isArray())
return true;
} else if (t instanceof GenericArrayType) {
Expand Down Expand Up @@ -317,8 +314,7 @@ public AnnotatedType getAnnotatedOwnerType() {

private Type getComponentType() {
Type t = getType();
if (t instanceof Class) {
Class<?> c = (Class)t;
if (t instanceof Class<?> c) {
return c.getComponentType();
}
return ((GenericArrayType)t).getGenericComponentType();
Expand All @@ -343,8 +339,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();

AnnotatedType componentType = this;
while (componentType instanceof AnnotatedArrayType) {
AnnotatedArrayType annotatedArrayType = (AnnotatedArrayType) componentType;
while (componentType instanceof AnnotatedArrayType annotatedArrayType) {
sb.append(annotationsToString(annotatedArrayType.getAnnotations(), true) + "[]");
componentType = annotatedArrayType.getAnnotatedGenericComponentType();
}
Expand All @@ -355,8 +350,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedArrayType) {
AnnotatedArrayType that = (AnnotatedArrayType) o;
if (o instanceof AnnotatedArrayType that) {
return equalsTypeAndAnnotations(that) &&
Objects.equals(getAnnotatedGenericComponentType(),
that.getAnnotatedGenericComponentType());
Expand Down Expand Up @@ -401,8 +395,7 @@ private TypeVariable<?> getTypeVariable() {

@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedTypeVariable) {
AnnotatedTypeVariable that = (AnnotatedTypeVariable) o;
if (o instanceof AnnotatedTypeVariable that) {
return equalsTypeAndAnnotations(that);
} else {
return false;
Expand Down Expand Up @@ -483,8 +476,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedParameterizedType) {
AnnotatedParameterizedType that = (AnnotatedParameterizedType) o;
if (o instanceof AnnotatedParameterizedType that) {
return equalsTypeAndAnnotations(that) &&
Arrays.equals(getAnnotatedActualTypeArguments(), that.getAnnotatedActualTypeArguments());
} else {
Expand Down Expand Up @@ -598,8 +590,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (o instanceof AnnotatedWildcardType) {
AnnotatedWildcardType that = (AnnotatedWildcardType) o;
if (o instanceof AnnotatedWildcardType that) {
return equalsTypeAndAnnotations(that) &&
// Treats ordering as significant
Arrays.equals(getAnnotatedLowerBounds(), that.getAnnotatedLowerBounds()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public Object invoke(Object proxy, Method method, Object[] args) {
if (result == null)
throw new IncompleteAnnotationException(type, member);

if (result instanceof ExceptionProxy)
throw ((ExceptionProxy) result).generateException();
if (result instanceof ExceptionProxy exceptProxy)
throw exceptProxy.generateException();

if (result.getClass().isArray() && Array.getLength(result) != 0)
result = cloneArray(result);
Expand Down Expand Up @@ -410,8 +410,8 @@ private Boolean equalsImpl(Object proxy, Object o) {
private AnnotationInvocationHandler asOneOfUs(Object o) {
if (Proxy.isProxyClass(o.getClass())) {
InvocationHandler handler = Proxy.getInvocationHandler(o);
if (handler instanceof AnnotationInvocationHandler)
return (AnnotationInvocationHandler) handler;
if (handler instanceof AnnotationInvocationHandler annotationHandler)
return annotationHandler;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, 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
Expand Down Expand Up @@ -280,9 +280,8 @@ private static Annotation parseAnnotation2(ByteBuffer buf,
skipMemberValue(buf);
} else {
Object value = parseMemberValue(memberType, buf, constPool, container);
if (value instanceof AnnotationTypeMismatchExceptionProxy)
((AnnotationTypeMismatchExceptionProxy) value).
setMember(type.members().get(memberName));
if (value instanceof AnnotationTypeMismatchExceptionProxy exceptProxy)
exceptProxy.setMember(type.members().get(memberName));
memberValues.put(memberName, value);
}
}
Expand Down Expand Up @@ -443,9 +442,8 @@ private static Class<?> parseSig(String sig, Class<?> container) {
return toClass(result);
}
static Class<?> toClass(Type o) {
if (o instanceof GenericArrayType)
return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
0)
if (o instanceof GenericArrayType gat)
return Array.newInstance(toClass(gat.getGenericComponentType()), 0)
.getClass();
return (Class)o;
}
Expand Down Expand Up @@ -744,8 +742,9 @@ private static Object parseArrayElements(Object[] result,
int tag = buf.get();
if (tag == expectedTag) {
Object value = parseElement.get();
if (value instanceof ExceptionProxy) {
if (exceptionProxy == null) exceptionProxy = (ExceptionProxy) value;
if (value instanceof ExceptionProxy proxyValue) {
if (exceptionProxy == null)
exceptionProxy = proxyValue;
} else {
result[i] = value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, 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
Expand Down Expand Up @@ -127,8 +127,7 @@ public static AnnotatedType[] buildAnnotatedTypes(byte[] rawAnnotations,
// has no annotations and the annotations to parameter mapping
// should be offset by 1.
boolean offset = false;
if (decl instanceof Constructor) {
Constructor<?> ctor = (Constructor<?>) decl;
if (decl instanceof Constructor<?> ctor) {
Class<?> declaringClass = ctor.getDeclaringClass();
if (!declaringClass.isEnum() &&
(declaringClass.isMemberClass() &&
Expand Down Expand Up @@ -226,11 +225,11 @@ public static <D extends GenericDeclaration> Annotation[] parseTypeVariableAnnot
int typeVarIndex) {
AnnotatedElement decl;
TypeAnnotationTarget predicate;
if (genericDecl instanceof Class) {
decl = (Class<?>)genericDecl;
if (genericDecl instanceof Class<?> classDecl) {
decl = classDecl;
predicate = TypeAnnotationTarget.CLASS_TYPE_PARAMETER;
} else if (genericDecl instanceof Executable) {
decl = (Executable)genericDecl;
} else if (genericDecl instanceof Executable execDecl) {
decl = execDecl;
predicate = TypeAnnotationTarget.METHOD_TYPE_PARAMETER;
} else {
throw new AssertionError("Unknown GenericDeclaration " + genericDecl + "\nthis should not happen.");
Expand Down Expand Up @@ -279,13 +278,11 @@ private static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBoun
// if applicable.
if (bounds.length > 0) {
Type b0 = bounds[0];
if (b0 instanceof Class<?>) {
Class<?> c = (Class<?>) b0;
if (b0 instanceof Class<?> c) {
if (c.isInterface()) {
startIndex = 1;
}
} else if (b0 instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) b0;
} else if (b0 instanceof ParameterizedType p) {
Class<?> c = (Class<?>) p.getRawType();
if (c.isInterface()) {
startIndex = 1;
Expand Down Expand Up @@ -316,9 +313,9 @@ private static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBoun
private static <D extends GenericDeclaration> List<TypeAnnotation> fetchBounds(D decl) {
AnnotatedElement boundsDecl;
TypeAnnotationTarget target;
if (decl instanceof Class) {
if (decl instanceof Class<?> classDecl) {
target = TypeAnnotationTarget.CLASS_TYPE_PARAMETER_BOUND;
boundsDecl = (Class)decl;
boundsDecl = classDecl;
} else {
target = TypeAnnotationTarget.METHOD_TYPE_PARAMETER_BOUND;
boundsDecl = (Executable)decl;
Expand All @@ -337,12 +334,12 @@ static TypeAnnotation[] parseAllTypeAnnotations(AnnotatedElement decl) {
Class<?> container;
byte[] rawBytes;
JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
if (decl instanceof Class) {
container = (Class<?>)decl;
if (decl instanceof Class<?> classDecl) {
container = classDecl;
rawBytes = javaLangAccess.getRawClassTypeAnnotations(container);
} else if (decl instanceof Executable) {
container = ((Executable)decl).getDeclaringClass();
rawBytes = javaLangAccess.getRawExecutableTypeAnnotations((Executable)decl);
} else if (decl instanceof Executable execDecl) {
container = execDecl.getDeclaringClass();
rawBytes = javaLangAccess.getRawExecutableTypeAnnotations(execDecl);
} else {
// Should not reach here. Assert?
return EMPTY_TYPE_ANNOTATION_ARRAY;
Expand Down

1 comment on commit aaa8971

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.