Improve BytecodeDescriptor error message

This commit is contained in:
liach 2025-02-25 16:17:32 -06:00
parent ed6e4b5252
commit 5745bfd08b
3 changed files with 25 additions and 4 deletions

View File

@ -95,6 +95,13 @@ public class BytecodeDescriptor {
}
/**
* Parse a single type in a descriptor. Results can be:
* <ul>
* <li>A {@code Class} for successful parsing
* <li>{@code null} for malformed descriptor format
* <li>Throwing a {@link TypeNotPresentException} for valid class name,
* but class cannot be discovered
* </ul>
* @param loader the class loader in which to look up the types (null means
* bootstrap class loader)
*/
@ -117,7 +124,8 @@ public class BytecodeDescriptor {
t = t.arrayType();
return t;
} else {
return Wrapper.forBasicType(c).primitiveType();
var w = Wrapper.forPrimitiveTypeOrNull(c);
return w == null ? null : w.primitiveType();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, 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
@ -352,6 +352,19 @@ public enum Wrapper {
throw basicTypeError(type);
}
/**
* Return the primitive wrapper for the given char. Does not return
* {@code OBJECT}. Returns {@code null} to allow flexible error messages.
* Dedicated for {@link BytecodeDescriptor}.
*/
static Wrapper forPrimitiveTypeOrNull(char type) {
Wrapper w = FROM_CHAR[(type + (type >> 1)) & 0xf];
if (w != null && w != OBJECT && w.basicTypeChar == type) {
return w;
}
return null;
}
@DontInline
private static RuntimeException basicTypeError(char type) {
for (Wrapper x : values()) {

View File

@ -67,8 +67,8 @@ class MalformedAnnotationTest {
AnnotationElement.of("value", AnnotationValue.ofClass(clb
.constantPool().utf8Entry(badDescString))))
)));
var cl = ByteCodeLoader.load("Test", bytes);
var cl = new ByteCodeLoader("Test", bytes, MalformedAnnotationTest.class.getClassLoader()).loadClass("Test");
var ex = assertThrows(GenericSignatureFormatError.class, () -> cl.getDeclaredAnnotation(ClassCarrier.class));
assertTrue(ex.getMessage().contains(badDescString));
assertTrue(ex.getMessage().contains(badDescString), () -> "Uninformative error: " + ex);
}
}