8336927: Missing equals and hashCode in java.lang.classfile.Annotation

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-07-24 12:13:15 +00:00
parent 05d88de05e
commit 332df83e7c
2 changed files with 28 additions and 19 deletions

View file

@ -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<AnnotationElement> elements;
public AnnotationImpl(Utf8Entry className,
List<AnnotationElement> elems) {
this.className = className;
this.elements = List.copyOf(elems);
}
@Override
public Utf8Entry className() {
return className;
}
@Override
public List<AnnotationElement> elements() {
return elements;
public record AnnotationImpl(Utf8Entry className, List<AnnotationElement> elements)
implements Annotation, Util.Writable {
public AnnotationImpl {
elements = List.copyOf(elements);
}
@Override

View file

@ -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())));
}
}