8341483: Clarify special case handling of Types.getArrayType

Reviewed-by: liach, prappo, dlsmith
This commit is contained in:
Joe Darcy 2024-10-04 20:31:28 +00:00
parent e70cbcfd0c
commit a3e23572d5
3 changed files with 65 additions and 12 deletions

View File

@ -263,7 +263,10 @@ public interface Types {
* *
* @param componentType the component type * @param componentType the component type
* @throws IllegalArgumentException if the component type is not valid for * @throws IllegalArgumentException if the component type is not valid for
* an array, including executable, package, module, and wildcard types * an array. All valid types are {@linkplain ReferenceType
* reference types} or {@linkplain PrimitiveType primitive types}.
* Invalid types include {@linkplain NullType null}, executable, package,
* module, and wildcard types.
* @jls 10.1 Array Types * @jls 10.1 Array Types
*/ */
ArrayType getArrayType(TypeMirror componentType); ArrayType getArrayType(TypeMirror componentType);

View File

@ -193,10 +193,14 @@ public class JavacTypes implements javax.lang.model.util.Types {
public ArrayType getArrayType(TypeMirror componentType) { public ArrayType getArrayType(TypeMirror componentType) {
switch (componentType.getKind()) { switch (componentType.getKind()) {
case VOID: case VOID:
case NONE:
case NULL:
case EXECUTABLE: case EXECUTABLE:
case WILDCARD: // heh! case WILDCARD: // heh!
case PACKAGE: case PACKAGE:
case MODULE: case MODULE:
case UNION:
case INTERSECTION:
throw new IllegalArgumentException(componentType.toString()); throw new IllegalArgumentException(componentType.toString());
} }
return new Type.ArrayType((Type) componentType, syms.arrayClass); return new Type.ArrayType((Type) componentType, syms.arrayClass);

View File

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8340721 * @bug 8340721 8341483
* @summary Test invalid inputs to javax.lang.model.util.Types methods * @summary Test invalid inputs to javax.lang.model.util.Types methods
* @library /tools/javac/lib * @library /tools/javac/lib
* @modules java.compiler * @modules java.compiler
@ -68,8 +68,20 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor {
RoundEnvironment roundEnv) { RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) { if (!roundEnv.processingOver()) {
initializeTypes(); initializeTypes();
// isSubType
// isAssignable
// contains
// directSupertypes
testUnboxedType(); testUnboxedType();
// capture
// getPrimitiveType
// getNoType
testGetArrayType();
testGetWildcardType(); testGetWildcardType();
// getDeclaredType
// getDeclaredType (overload)
// asMemberOf
} }
return true; return true;
} }
@ -136,15 +148,18 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor {
// Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable // Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable
// non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType // non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType
var invalidInputs = List.of(objectType, stringType, arrayType, var invalidInputs =
executableType, intersectionType, List.of(primitiveType, executableType,
noTypeVoid, noTypeNone, noTypePackage, noTypeModule, nullType, objectType, stringType, arrayType,
primitiveType, /*unionType, */ wildcardType); intersectionType, /*unionType, */
noTypeVoid, noTypeNone, noTypePackage, noTypeModule,
nullType,
wildcardType);
for (TypeMirror tm : invalidInputs) { for (TypeMirror tm : invalidInputs) {
try { try {
PrimitiveType pt = types.unboxedType(tm); PrimitiveType pt = types.unboxedType(tm);
throw new RuntimeException("Should not reach " + tm); shouldNotReach(tm);
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
; // Expected ; // Expected
} }
@ -152,6 +167,11 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor {
return; return;
} }
private void shouldNotReach(TypeMirror tm) {
throw new RuntimeException("Should not reach " + tm +
" " + tm.getKind());
}
/** /**
* @throws IllegalArgumentException if bounds are not valid, * @throws IllegalArgumentException if bounds are not valid,
* including for types that are not {@linkplain ReferenceType * including for types that are not {@linkplain ReferenceType
@ -160,25 +180,51 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor {
void testGetWildcardType() { void testGetWildcardType() {
// Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable // Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable
// non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType // non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType
var invalidInputs = List.of(executableType, intersectionType, var invalidInputs =
noTypeVoid, noTypeNone, noTypePackage, noTypeModule, nullType, List.of(primitiveType, executableType,
primitiveType, /*unionType, */ wildcardType); intersectionType, /*unionType, */
noTypeVoid, noTypeNone, noTypePackage, noTypeModule,
nullType,
wildcardType);
for (TypeMirror tm : invalidInputs) { for (TypeMirror tm : invalidInputs) {
try { try {
WildcardType wc1 = types.getWildcardType(tm, null); WildcardType wc1 = types.getWildcardType(tm, null);
throw new RuntimeException("Should not reach " + tm); shouldNotReach(tm);
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
; // Expected ; // Expected
} }
try { try {
WildcardType wc2 = types.getWildcardType(null, tm); WildcardType wc2 = types.getWildcardType(null, tm);
throw new RuntimeException("Should not reach " + tm); shouldNotReach(tm);
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
; // Expected ; // Expected
} }
} }
return; return;
} }
/**
* @throws IllegalArgumentException if the component type is not valid for
* an array. All valid types are {@linkplain ReferenceType
* reference types} or {@linkplain PrimitiveType primitive types}.
* Invalid types include null, executable, package, module, and wildcard types.
*/
void testGetArrayType() {
var invalidInputs =
List.of(executableType,
noTypeVoid, noTypeNone, noTypePackage, noTypeModule,
nullType,
/*unionType, */ wildcardType);
for (TypeMirror tm : invalidInputs) {
try {
ArrayType arrayType = types.getArrayType(tm);
shouldNotReach(tm);
} catch(IllegalArgumentException iae) {
; // Expected
}
}
}
} }