8335927: Revisit AnnotationConstantValueEntry and AnnotationValue.OfConstant

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-08-13 15:14:06 +00:00
parent 9e282e5c96
commit 6af1d6ff21
12 changed files with 356 additions and 179 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.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,13 +24,23 @@
*/
package java.lang.classfile.constantpool;
import java.lang.classfile.AnnotationValue;
import java.lang.constant.ConstantDesc;
import jdk.internal.javac.PreviewFeature;
/**
* A constant pool entry that may be used as an annotation constant,
* which includes the four kinds of primitive constants, and UTF8 constants.
* A constant pool entry that may be used by annotation constant values,
* which includes the four kinds of primitive constants and UTF8 constants.
* These entries are also the only entries that do not refer to other
* constant pool entries.
*
* @apiNote
* An annotation constant value entry alone is not sufficient to determine
* the annotation constant; for example, an {@link IntegerEntry} of {@code 1}
* can mean {@code true} in {@link AnnotationValue.OfBoolean} or {@code 1}
* in {@link AnnotationValue.OfInt}.
*
* @see AnnotationValue.OfConstant
* @sealedGraph
* @since 22
*/
@ -40,7 +50,8 @@ public sealed interface AnnotationConstantValueEntry extends PoolEntry
/**
* {@return the constant value} The constant value will be an {@link Integer},
* {@link Long}, {@link Float}, {@link Double}, or {@link String}.
* {@link Long}, {@link Float}, {@link Double} for the primitive constants,
* or {@link String} for UTF8 constants.
*/
ConstantDesc constantValue();
}

View file

@ -510,25 +510,6 @@ public sealed interface ConstantPoolBuilder
throw new IllegalArgumentException("Illegal type: " + (c == null ? null : c.getClass()));
}
/**
* {@return An {@link AnnotationConstantValueEntry} describing the provided
* constant} The constant should be an Integer, String, Long, Float,
* Double, ClassDesc (for a Class constant), or MethodTypeDesc (for a MethodType
* constant.)
*
* @param c the constant
*/
default AnnotationConstantValueEntry annotationConstantValueEntry(ConstantDesc c) {
if (c instanceof Integer i) return intEntry(i);
if (c instanceof String s) return utf8Entry(s);
if (c instanceof Long l) return longEntry(l);
if (c instanceof Float f) return floatEntry(f);
if (c instanceof Double d) return doubleEntry(d);
if (c instanceof ClassDesc cd) return utf8Entry(cd);
if (c instanceof MethodTypeDesc mtd) return utf8Entry(mtd);
throw new IllegalArgumentException("Illegal type: " + (c == null ? null : c.getClass()));
}
/**
* {@return a {@link BootstrapMethodEntry} describing the provided
* bootstrap method and static arguments}