8338700: AttributeMapper type parameter should be bounded by Attribute

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-08-23 15:16:44 +00:00
parent 916f1aa04f
commit a461369f16
5 changed files with 6 additions and 6 deletions

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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -40,7 +40,7 @@ import jdk.internal.javac.PreviewFeature;
* @since 22 * @since 22
*/ */
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API) @PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public interface AttributeMapper<A> { public interface AttributeMapper<A extends Attribute<A>> {
/** /**
* Attribute stability indicator * Attribute stability indicator

View File

@ -147,7 +147,7 @@
* ClassReader, int)} method for mapping from the classfile format * ClassReader, int)} method for mapping from the classfile format
* to an attribute instance, and the * to an attribute instance, and the
* {@link java.lang.classfile.AttributeMapper#writeAttribute(java.lang.classfile.BufWriter, * {@link java.lang.classfile.AttributeMapper#writeAttribute(java.lang.classfile.BufWriter,
* java.lang.Object)} method for mapping back to the classfile format. It also * java.lang.classfile.Attribute)} method for mapping back to the classfile format. It also
* contains metadata including the attribute name, the set of classfile entities * contains metadata including the attribute name, the set of classfile entities
* where the attribute is applicable, and whether multiple attributes of the * where the attribute is applicable, and whether multiple attributes of the
* same kind are allowed on a single entity. * same kind are allowed on a single entity.

View File

@ -54,7 +54,7 @@ public class AttributeHolder {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
<A> A get(AttributeMapper<A> am) { <A extends Attribute<A>> A get(AttributeMapper<A> am) {
for (Attribute<?> a : attributes) for (Attribute<?> a : attributes)
if (a.attributeMapper() == am) if (a.attributeMapper() == am)
return (A)a; return (A)a;

View File

@ -148,7 +148,7 @@ public abstract sealed class BoundAttribute<T extends Attribute<T>>
mapper = customAttributes.apply(name); mapper = customAttributes.apply(name);
} }
if (mapper != null) { if (mapper != null) {
filled.add((Attribute<?>) Objects.requireNonNull(mapper.readAttribute(enclosing, reader, p))); filled.add(Objects.requireNonNull(mapper.readAttribute(enclosing, reader, p)));
} else { } else {
AttributeMapper<UnknownAttribute> fakeMapper = new AttributeMapper<>() { AttributeMapper<UnknownAttribute> fakeMapper = new AttributeMapper<>() {
@Override @Override

View File

@ -224,7 +224,7 @@ public class Util {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <T> void writeAttribute(BufWriterImpl writer, Attribute<?> attr) { private static <T extends Attribute<T>> void writeAttribute(BufWriterImpl writer, Attribute<?> attr) {
if (attr instanceof CustomAttribute<?> ca) { if (attr instanceof CustomAttribute<?> ca) {
var mapper = (AttributeMapper<T>) ca.attributeMapper(); var mapper = (AttributeMapper<T>) ca.attributeMapper();
mapper.writeAttribute(writer, (T) ca); mapper.writeAttribute(writer, (T) ca);