diff --git a/src/java.compiler/share/classes/javax/lang/model/util/Types.java b/src/java.compiler/share/classes/javax/lang/model/util/Types.java index 8be31c26f77..951b56ed214 100644 --- a/src/java.compiler/share/classes/javax/lang/model/util/Types.java +++ b/src/java.compiler/share/classes/javax/lang/model/util/Types.java @@ -263,7 +263,10 @@ public interface Types { * * @param componentType the component type * @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 */ ArrayType getArrayType(TypeMirror componentType); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java index 1bc5de7f73a..71e39a6a408 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java @@ -193,10 +193,14 @@ public class JavacTypes implements javax.lang.model.util.Types { public ArrayType getArrayType(TypeMirror componentType) { switch (componentType.getKind()) { case VOID: + case NONE: + case NULL: case EXECUTABLE: case WILDCARD: // heh! case PACKAGE: case MODULE: + case UNION: + case INTERSECTION: throw new IllegalArgumentException(componentType.toString()); } return new Type.ArrayType((Type) componentType, syms.arrayClass); diff --git a/test/langtools/tools/javac/processing/model/util/types/TestInvalidInputs.java b/test/langtools/tools/javac/processing/model/util/types/TestInvalidInputs.java index 3cc1e246e41..47b11bbbba8 100644 --- a/test/langtools/tools/javac/processing/model/util/types/TestInvalidInputs.java +++ b/test/langtools/tools/javac/processing/model/util/types/TestInvalidInputs.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8340721 + * @bug 8340721 8341483 * @summary Test invalid inputs to javax.lang.model.util.Types methods * @library /tools/javac/lib * @modules java.compiler @@ -68,8 +68,20 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor { RoundEnvironment roundEnv) { if (!roundEnv.processingOver()) { initializeTypes(); + + // isSubType + // isAssignable + // contains + // directSupertypes testUnboxedType(); + // capture + // getPrimitiveType + // getNoType + testGetArrayType(); testGetWildcardType(); + // getDeclaredType + // getDeclaredType (overload) + // asMemberOf } return true; } @@ -136,15 +148,18 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor { // Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable // non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType - var invalidInputs = List.of(objectType, stringType, arrayType, - executableType, intersectionType, - noTypeVoid, noTypeNone, noTypePackage, noTypeModule, nullType, - primitiveType, /*unionType, */ wildcardType); + var invalidInputs = + List.of(primitiveType, executableType, + objectType, stringType, arrayType, + intersectionType, /*unionType, */ + noTypeVoid, noTypeNone, noTypePackage, noTypeModule, + nullType, + wildcardType); for (TypeMirror tm : invalidInputs) { try { PrimitiveType pt = types.unboxedType(tm); - throw new RuntimeException("Should not reach " + tm); + shouldNotReach(tm); } catch(IllegalArgumentException iae) { ; // Expected } @@ -152,6 +167,11 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor { return; } + private void shouldNotReach(TypeMirror tm) { + throw new RuntimeException("Should not reach " + tm + + " " + tm.getKind()); + } + /** * @throws IllegalArgumentException if bounds are not valid, * including for types that are not {@linkplain ReferenceType @@ -160,25 +180,51 @@ public class TestInvalidInputs extends JavacTestingAbstractProcessor { void testGetWildcardType() { // Reference types are ArrayType, DeclaredType, ErrorType, NullType, TypeVariable // non-reference: ExecutableType, IntersectionType, NoType, PrimitiveType, UnionType, WildcardType - var invalidInputs = List.of(executableType, intersectionType, - noTypeVoid, noTypeNone, noTypePackage, noTypeModule, nullType, - primitiveType, /*unionType, */ wildcardType); + var invalidInputs = + List.of(primitiveType, executableType, + intersectionType, /*unionType, */ + noTypeVoid, noTypeNone, noTypePackage, noTypeModule, + nullType, + wildcardType); for (TypeMirror tm : invalidInputs) { try { WildcardType wc1 = types.getWildcardType(tm, null); - throw new RuntimeException("Should not reach " + tm); + shouldNotReach(tm); } catch(IllegalArgumentException iae) { ; // Expected } try { WildcardType wc2 = types.getWildcardType(null, tm); - throw new RuntimeException("Should not reach " + tm); + shouldNotReach(tm); } catch(IllegalArgumentException iae) { ; // Expected } } 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 + } + } + } }