8317356: Fix missing null checks in the ClassFile API

Co-authored-by: Nizar Benalla <nbenalla@openjdk.org>
Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-10-14 17:21:10 +00:00
parent 60713463c7
commit a2c775222e
36 changed files with 249 additions and 116 deletions

View file

@ -42,6 +42,8 @@ import java.util.List;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
import static java.util.Objects.requireNonNull;
/**
* Models an {@code element_value} structure, or a value of an element-value
* pair of an annotation, as defined in JVMS {@jvms 4.7.16.1}.
@ -488,6 +490,8 @@ public sealed interface AnnotationValue {
*/
static OfEnum ofEnum(Utf8Entry className,
Utf8Entry constantName) {
requireNonNull(className);
requireNonNull(constantName);
return new AnnotationImpl.OfEnumImpl(className, constantName);
}
@ -506,6 +510,7 @@ public sealed interface AnnotationValue {
* @param className the descriptor string of the class
*/
static OfClass ofClass(Utf8Entry className) {
requireNonNull(className);
return new AnnotationImpl.OfClassImpl(className);
}
@ -522,6 +527,7 @@ public sealed interface AnnotationValue {
* @param value the string
*/
static OfString ofString(Utf8Entry value) {
requireNonNull(value);
return new AnnotationImpl.OfStringImpl(value);
}
@ -538,6 +544,7 @@ public sealed interface AnnotationValue {
* @param value the double value
*/
static OfDouble ofDouble(DoubleEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfDoubleImpl(value);
}
@ -554,6 +561,7 @@ public sealed interface AnnotationValue {
* @param value the float value
*/
static OfFloat ofFloat(FloatEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfFloatImpl(value);
}
@ -570,6 +578,7 @@ public sealed interface AnnotationValue {
* @param value the long value
*/
static OfLong ofLong(LongEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfLongImpl(value);
}
@ -586,6 +595,7 @@ public sealed interface AnnotationValue {
* @param value the int value
*/
static OfInt ofInt(IntegerEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfIntImpl(value);
}
@ -602,6 +612,7 @@ public sealed interface AnnotationValue {
* @param value the short value
*/
static OfShort ofShort(IntegerEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfShortImpl(value);
}
@ -618,6 +629,7 @@ public sealed interface AnnotationValue {
* @param value the char value
*/
static OfChar ofChar(IntegerEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfCharImpl(value);
}
@ -634,6 +646,7 @@ public sealed interface AnnotationValue {
* @param value the byte value
*/
static OfByte ofByte(IntegerEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfByteImpl(value);
}
@ -650,6 +663,7 @@ public sealed interface AnnotationValue {
* @param value the boolean value
*/
static OfBoolean ofBoolean(IntegerEntry value) {
requireNonNull(value);
return new AnnotationImpl.OfBooleanImpl(value);
}
@ -667,6 +681,7 @@ public sealed interface AnnotationValue {
* @param value the annotation
*/
static OfAnnotation ofAnnotation(Annotation value) {
requireNonNull(value);
return new AnnotationImpl.OfAnnotationImpl(value);
}
@ -784,6 +799,6 @@ public sealed interface AnnotationValue {
} else if (value instanceof Enum<?> e) {
return ofEnum(ClassDesc.ofDescriptor(e.getDeclaringClass().descriptorString()), e.name());
}
throw new IllegalArgumentException("Illegal annotation constant value type " + (value == null ? null : value.getClass()));
throw new IllegalArgumentException("Illegal annotation constant value type " + requireNonNull(value).getClass());
}
}