Skip to content

Commit 328e951

Browse files
committed
8169629: Annotations with lambda expressions cause AnnotationFormatError
Reviewed-by: jfranck
1 parent c04a743 commit 328e951

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -370,6 +370,8 @@ private Boolean equalsImpl(Object proxy, Object o) {
370370
if (!type.isInstance(o))
371371
return false;
372372
for (Method memberMethod : getMemberMethods()) {
373+
if (memberMethod.isSynthetic())
374+
continue;
373375
String member = memberMethod.getName();
374376
Object ourValue = memberValues.get(member);
375377
Object hisValue = null;
@@ -491,6 +493,17 @@ private void validateAnnotationMethods(Method[] memberMethods) {
491493
*/
492494
boolean valid = true;
493495
for(Method method : memberMethods) {
496+
int modifiers = method.getModifiers();
497+
// Skip over methods that may be a static initializer or
498+
// similar construct. A static initializer may be used for
499+
// purposes such as initializing a lambda stored in an
500+
// interface field.
501+
if (method.isSynthetic() &&
502+
(modifiers & (Modifier.STATIC | Modifier.PRIVATE)) != 0 &&
503+
method.getParameterCount() == 0) {
504+
continue;
505+
}
506+
494507
/*
495508
* "By virtue of the AnnotationTypeElementDeclaration
496509
* production, a method declaration in an annotation type
@@ -501,7 +514,7 @@ private void validateAnnotationMethods(Method[] memberMethods) {
501514
* production, a method declaration in an annotation type
502515
* declaration cannot be default or static."
503516
*/
504-
if (method.getModifiers() != (Modifier.PUBLIC | Modifier.ABSTRACT) ||
517+
if (modifiers != (Modifier.PUBLIC | Modifier.ABSTRACT) ||
505518
method.isDefault() ||
506519
method.getParameterCount() != 0 ||
507520
method.getExceptionTypes().length != 0) {

test/jdk/java/lang/annotation/EqualityTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,12 @@
2323

2424
/*
2525
* @test
26-
* @bug 8071859
26+
* @bug 8071859 8169629
2727
* @summary Check annotation equality behavior against the invocation handler
28+
* @compile --release 8 EqualityTest.java
29+
* @run main EqualityTest
30+
* @compile EqualityTest.java
31+
* @run main EqualityTest
2832
*/
2933

3034
import java.lang.annotation.*;
@@ -40,16 +44,22 @@ public static void main(String... args) throws Exception {
4044
testEquality(annotation, handler, false);
4145
testEquality(annotation, annotation, true);
4246
testEquality(handler, handler, true);
47+
testEquality(annotation, AnnotationHost.class.getAnnotation(TestAnnotation.class), true);
4348
}
4449

4550
private static void testEquality(Object a, Object b, boolean expected) {
4651
boolean result = a.equals(b);
4752
if (result != b.equals(a) || result != expected)
4853
throw new RuntimeException("Unexpected result");
4954
}
55+
56+
@TestAnnotation
57+
private static class AnnotationHost {}
5058
}
5159

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

0 commit comments

Comments
 (0)