diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/AnnotationImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/AnnotationImpl.java index 8290fcd286e..03353e272a6 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/AnnotationImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/AnnotationImpl.java @@ -32,24 +32,10 @@ import java.util.List; import static java.lang.classfile.ClassFile.*; -public final class AnnotationImpl implements Annotation, Util.Writable { - private final Utf8Entry className; - private final List elements; - - public AnnotationImpl(Utf8Entry className, - List elems) { - this.className = className; - this.elements = List.copyOf(elems); - } - - @Override - public Utf8Entry className() { - return className; - } - - @Override - public List elements() { - return elements; +public record AnnotationImpl(Utf8Entry className, List elements) + implements Annotation, Util.Writable { + public AnnotationImpl { + elements = List.copyOf(elements); } @Override diff --git a/test/jdk/jdk/classfile/AnnotationTest.java b/test/jdk/jdk/classfile/AnnotationTest.java index 0226affe7c2..4ed3b2141ad 100644 --- a/test/jdk/jdk/classfile/AnnotationTest.java +++ b/test/jdk/jdk/classfile/AnnotationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, 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 @@ -207,4 +207,27 @@ class AnnotationTest { assertAnno(mannos.get(0), "LAnno;", true); assertAnno(fannos.get(0), "LAnno;", true); } + + @Test + void testEquality() { + assertEquals(Annotation.of(CD_Object), Annotation.of(ClassDesc.of("java.lang.Object"))); + assertNotEquals(Annotation.of(CD_Object), Annotation.of(CD_String)); + assertEquals(Annotation.of(CD_Object, AnnotationElement.of("fly", AnnotationValue.ofInt(5))), + Annotation.of(CD_Object, AnnotationElement.ofInt("fly", 5))); + assertEquals(AnnotationElement.ofFloat("one", 1.2F), + AnnotationElement.ofFloat("one", 1.2F)); + assertEquals(AnnotationElement.ofFloat("one", 1.2F), + AnnotationElement.of("one", AnnotationValue.ofFloat(1.2F))); + assertNotEquals(AnnotationElement.ofFloat("one", 1.2F), + AnnotationElement.ofFloat("two", 1.2F)); + assertNotEquals(AnnotationElement.ofFloat("one", 1.2F), + AnnotationElement.ofFloat("one", 2.1F)); + assertNotEquals(AnnotationElement.ofFloat("one", 1.2F), + AnnotationElement.ofDouble("one", 1.2F)); + assertEquals(AnnotationValue.ofInt(23), AnnotationValue.ofInt(23)); + assertNotEquals(AnnotationValue.ofInt(23), AnnotationValue.ofInt(42)); + assertNotEquals(AnnotationValue.ofInt(23), AnnotationValue.ofLong(23)); + assertEquals(AnnotationValue.ofAnnotation(Annotation.of(CD_Object)), + AnnotationValue.ofAnnotation(Annotation.of(Object.class.describeConstable().orElseThrow()))); + } }