Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8169629: Annotations with lambda expressions cause AnnotationFormatError
Reviewed-by: jfranck
  • Loading branch information
jddarcy committed Apr 1, 2021
1 parent c04a743 commit 328e951
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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 @@ -370,6 +370,8 @@ private Boolean equalsImpl(Object proxy, Object o) {
if (!type.isInstance(o))
return false;
for (Method memberMethod : getMemberMethods()) {
if (memberMethod.isSynthetic())
continue;
String member = memberMethod.getName();
Object ourValue = memberValues.get(member);
Object hisValue = null;
Expand Down Expand Up @@ -491,6 +493,17 @@ private void validateAnnotationMethods(Method[] memberMethods) {
*/
boolean valid = true;
for(Method method : memberMethods) {
int modifiers = method.getModifiers();
// Skip over methods that may be a static initializer or
// similar construct. A static initializer may be used for
// purposes such as initializing a lambda stored in an
// interface field.
if (method.isSynthetic() &&
(modifiers & (Modifier.STATIC | Modifier.PRIVATE)) != 0 &&
method.getParameterCount() == 0) {
continue;
}

/*
* "By virtue of the AnnotationTypeElementDeclaration
* production, a method declaration in an annotation type
Expand All @@ -501,7 +514,7 @@ private void validateAnnotationMethods(Method[] memberMethods) {
* production, a method declaration in an annotation type
* declaration cannot be default or static."
*/
if (method.getModifiers() != (Modifier.PUBLIC | Modifier.ABSTRACT) ||
if (modifiers != (Modifier.PUBLIC | Modifier.ABSTRACT) ||
method.isDefault() ||
method.getParameterCount() != 0 ||
method.getExceptionTypes().length != 0) {
Expand Down
14 changes: 12 additions & 2 deletions test/jdk/java/lang/annotation/EqualityTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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 All @@ -23,8 +23,12 @@

/*
* @test
* @bug 8071859
* @bug 8071859 8169629
* @summary Check annotation equality behavior against the invocation handler
* @compile --release 8 EqualityTest.java
* @run main EqualityTest
* @compile EqualityTest.java
* @run main EqualityTest
*/

import java.lang.annotation.*;
Expand All @@ -40,16 +44,22 @@ public static void main(String... args) throws Exception {
testEquality(annotation, handler, false);
testEquality(annotation, annotation, true);
testEquality(handler, handler, true);
testEquality(annotation, AnnotationHost.class.getAnnotation(TestAnnotation.class), true);
}

private static void testEquality(Object a, Object b, boolean expected) {
boolean result = a.equals(b);
if (result != b.equals(a) || result != expected)
throw new RuntimeException("Unexpected result");
}

@TestAnnotation
private static class AnnotationHost {}
}

@Retention(RetentionPolicy.RUNTIME)
@interface TestAnnotation {
// Trigger creation of synthetic method to initialize r.
public static final Runnable r = () -> {};
}

1 comment on commit 328e951

@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.