From 45a3b39c401bc3a72eed474af06deff2d143a9ac Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Tue, 22 May 2018 09:07:21 -0400 Subject: [PATCH] HHH-12612 - Fix JDK9 compatibility with TYPE_USE on collection fields. --- .../hibernate/jpamodelgen/util/TypeUtils.java | 9 ++- .../ElementCollectionTypeUseTest.java | 58 ++++++++++++++ .../OfficeBuildingValidated.java | 80 +++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTypeUseTest.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/OfficeBuildingValidated.java diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java index 7fcca11741..37ae815d62 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java @@ -121,7 +121,14 @@ public final class TypeUtils { return extractClosestRealTypeAsString( compositeUpperBound, context ); } else { - return context.getTypeUtils().erasure( type ).toString(); + final TypeMirror erasureType = context.getTypeUtils().erasure( type ); + if ( TypeKind.ARRAY.equals( erasureType.getKind() ) ) { + // keep old behavior here for arrays since #asElement returns null for them. + return erasureType.toString(); + } + else { + return context.getTypeUtils().asElement( erasureType ).toString(); + } } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTypeUseTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTypeUseTest.java new file mode 100644 index 0000000000..4504eb4407 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTypeUseTest.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpamodelgen.test.elementcollection; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestForIssue; +import org.hibernate.jpamodelgen.test.util.WithClasses; +import org.junit.Test; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertListAttributeTypeInMetaModelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMapAttributesInMetaModelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSetAttributeTypeInMetaModelFor; + +/** + * @author Chris Cranford + */ +public class ElementCollectionTypeUseTest extends CompilationTest { + @Test + @TestForIssue(jiraKey = "HHH-12612") + @WithClasses(OfficeBuildingValidated.class) + public void testAnnotatedCollectionElements() { + assertMetamodelClassGeneratedFor( OfficeBuildingValidated.class ); + + assertMapAttributesInMetaModelFor( + OfficeBuildingValidated.class, + "doorCodes", + Integer.class, + byte[].class, + "Wrong type in map attributes." + ); + + assertSetAttributeTypeInMetaModelFor( + OfficeBuildingValidated.class, + "computerSerialNumbers", + String.class, + "Wrong type in set attribute." + ); + + assertListAttributeTypeInMetaModelFor( + OfficeBuildingValidated.class, + "employeeNames", + String.class, + "Wrong type in list attributes." + ); + + assertListAttributeTypeInMetaModelFor( + OfficeBuildingValidated.class, + "rooms", + Room.class, + "Wrong type in list attributes." + ); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/OfficeBuildingValidated.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/OfficeBuildingValidated.java new file mode 100644 index 0000000000..0d912ee2a8 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/OfficeBuildingValidated.java @@ -0,0 +1,80 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpamodelgen.test.elementcollection; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; + +/** + * @author Chris Cranford + */ +@Entity +public class OfficeBuildingValidated { + + // mock a bean validation annotation using TYPE_USE + @Target({ ElementType.TYPE_USE }) + @Retention(RetentionPolicy.RUNTIME) + public @interface NotNullAllowed { + + } + + @ElementCollection + @NotNullAllowed + private Map<@NotNullAllowed Integer, @NotNullAllowed byte[]> doorCodes; + + @ElementCollection + @NotNullAllowed + private Set<@NotNullAllowed String> computerSerialNumbers; + + @ElementCollection + @NotNullAllowed + private List<@NotNullAllowed String> employeeNames; + + @ElementCollection + @NotNullAllowed + private List<@NotNullAllowed Room> rooms; + + public Map getDoorCodes() { + return doorCodes; + } + + public void setDoorCodes(Map doorCodes) { + this.doorCodes = doorCodes; + } + + public Set getComputerSerialNumbers() { + return computerSerialNumbers; + } + + public void setComputerSerialNumbers(Set computerSerialNumbers) { + this.computerSerialNumbers = computerSerialNumbers; + } + + public List getEmployeeNames() { + return employeeNames; + } + + public void setEmployeeNames(List employeeNames) { + this.employeeNames = employeeNames; + } + + public List getRooms() { + return rooms; + } + + public void setRooms(List rooms) { + this.rooms = rooms; + } +}