more misc cleanups to modelgen code

This commit is contained in:
Gavin King 2023-07-08 12:18:19 +02:00
parent 7d9fa4a536
commit a36b683870
14 changed files with 137 additions and 136 deletions

View File

@ -24,7 +24,6 @@ import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.AccessType;
import org.hibernate.jpamodelgen.util.AccessTypeInformation;
import org.hibernate.jpamodelgen.util.Constants;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -76,8 +75,8 @@ public final class Context {
String persistenceXmlOption = pe.getOptions().get( JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION );
if ( persistenceXmlOption != null ) {
if ( !persistenceXmlOption.startsWith( Constants.PATH_SEPARATOR ) ) {
persistenceXmlOption = Constants.PATH_SEPARATOR + persistenceXmlOption;
if ( !persistenceXmlOption.startsWith("/") ) {
persistenceXmlOption = "/" + persistenceXmlOption;
}
persistenceXmlLocation = persistenceXmlOption;
}
@ -89,8 +88,8 @@ public final class Context {
if ( ormXmlOption != null ) {
ormXmlFiles = new ArrayList<>();
for ( String ormFile : ormXmlOption.split( "," ) ) {
if ( !ormFile.startsWith( Constants.PATH_SEPARATOR ) ) {
ormFile = Constants.PATH_SEPARATOR + ormFile;
if ( !ormFile.startsWith("/") ) {
ormFile = "/" + ormFile;
}
ormXmlFiles.add( ormFile );
}

View File

@ -378,16 +378,16 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private static boolean isLegalRawResultType(String containerTypeName) {
return containerTypeName.equals("java.util.List")
|| containerTypeName.equals("jakarta.persistence.Query")
|| containerTypeName.equals("org.hibernate.query.Query");
return containerTypeName.equals(List.class.getName())
|| containerTypeName.equals(Constants.QUERY)
|| containerTypeName.equals(Constants.HIB_QUERY);
}
private static boolean isLegalGenericResultType(String containerTypeName) {
return containerTypeName.equals("java.util.List")
|| containerTypeName.equals("jakarta.persistence.TypedQuery")
|| containerTypeName.equals("org.hibernate.query.Query")
|| containerTypeName.equals("org.hibernate.query.SelectionQuery");
return containerTypeName.equals(List.class.getName())
|| containerTypeName.equals(Constants.TYPED_QUERY)
|| containerTypeName.equals(Constants.HIB_QUERY)
|| containerTypeName.equals(Constants.HIB_SELECTION_QUERY);
}
private void addQueryMethod(

View File

@ -9,6 +9,7 @@ package org.hibernate.jpamodelgen.annotation;
import javax.lang.model.element.Element;
import org.hibernate.jpamodelgen.model.MetaSingleAttribute;
import org.hibernate.jpamodelgen.util.Constants;
/**
* @author Max Andersen
@ -23,6 +24,6 @@ public class AnnotationMetaSingleAttribute extends AnnotationMetaAttribute imple
@Override
public final String getMetaType() {
return "jakarta.persistence.metamodel.SingularAttribute";
return Constants.SINGULAR_ATTRIBUTE;
}
}

View File

@ -9,6 +9,7 @@ package org.hibernate.jpamodelgen.annotation;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
import java.util.List;
@ -86,7 +87,7 @@ public class CriteriaFinderMethod implements MetaAttribute {
.append("(");
if ( !belongsToDao ) {
declaration
.append(annotationMetaEntity.importType("jakarta.persistence.EntityManager"))
.append(annotationMetaEntity.importType(Constants.ENTITY_MANAGER))
.append(" entityManager");
}
for ( int i = 0; i < paramNames.size(); i ++ ) {
@ -140,7 +141,7 @@ public class CriteriaFinderMethod implements MetaAttribute {
declaration
.append("\n\t\t\t.getSingleResult()");
}
else if ( containerType.equals("java.util.List") ) {
else if ( containerType.equals(Constants.LIST) ) {
declaration
.append("\n\t\t\t.getResultList()");
}

View File

@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
/**
* @author Gavin King
@ -80,7 +81,7 @@ public class DaoConstructor implements MetaAttribute {
@Override
public String getTypeDeclaration() {
return "jakarta.persistence.EntityManager";
return Constants.ENTITY_MANAGER;
}
@Override

View File

@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
/**
* @author Gavin King
@ -71,7 +72,7 @@ public class IdFinderMethod implements MetaAttribute {
.append("(");
if ( !belongsToDao ) {
declaration
.append(annotationMetaEntity.importType("jakarta.persistence.EntityManager"))
.append(annotationMetaEntity.importType(Constants.ENTITY_MANAGER))
.append(" entityManager, ");
}
declaration

View File

@ -289,14 +289,14 @@ class BasicAttributeVisitor extends SimpleTypeVisitor8<Boolean, Element> {
}
@Override
public Boolean visitPrimitive(PrimitiveType t, Element element) {
public Boolean visitPrimitive(PrimitiveType primitiveType, Element element) {
return true;
}
@Override
public Boolean visitArray(ArrayType t, Element element) {
public Boolean visitArray(ArrayType arrayType, Element element) {
final TypeElement componentElement = (TypeElement)
context.getTypeUtils().asElement( t.getComponentType() );
context.getTypeUtils().asElement( arrayType.getComponentType() );
return Constants.BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() );
}
@ -316,7 +316,9 @@ class BasicAttributeVisitor extends SimpleTypeVisitor8<Boolean, Element> {
return true;
}
final TypeMirror serializableType =
context.getElementUtils().getTypeElement("java.io.Serializable").asType();
context.getElementUtils()
.getTypeElement(java.io.Serializable.class.getName())
.asType();
if ( context.getTypeUtils().isSubtype( typeElement.asType(), serializableType) ) {
return true;
}

View File

@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
import java.util.List;
@ -75,7 +76,7 @@ public class NaturalIdFinderMethod implements MetaAttribute {
.append("(");
if ( !belongsToDao ) {
declaration
.append(annotationMetaEntity.importType("jakarta.persistence.EntityManager"))
.append(annotationMetaEntity.importType(Constants.ENTITY_MANAGER))
.append(" entityManager");
}
for ( int i = 0; i < paramNames.size(); i ++ ) {
@ -93,7 +94,7 @@ public class NaturalIdFinderMethod implements MetaAttribute {
.append("\n\treturn entityManager")
//TODO: skip if unnecessary:
.append(".unwrap(")
.append(annotationMetaEntity.importType("org.hibernate.Session"))
.append(annotationMetaEntity.importType(Constants.HIB_SESSION))
.append(".class)\n\t\t\t")
.append(".byNaturalId(")
.append(annotationMetaEntity.importType(entity))

View File

@ -10,6 +10,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.query.Order;
import org.hibernate.query.Page;
import org.hibernate.query.SelectionQuery;
@ -120,7 +121,7 @@ public class QueryMethod implements MetaAttribute {
.append("(");
if ( !belongsToDao ) {
declaration
.append(annotationMetaEntity.importType("jakarta.persistence.EntityManager"))
.append(annotationMetaEntity.importType(Constants.ENTITY_MANAGER))
.append(" entityManager");
}
@ -212,7 +213,7 @@ public class QueryMethod implements MetaAttribute {
declaration
.append("\n\t\t\t.getSingleResult()");
}
else if ( containerTypeName.equals("java.util.List") ) {
else if ( containerTypeName.equals(Constants.LIST) ) {
declaration
.append("\n\t\t\t.getResultList()");
}
@ -280,7 +281,7 @@ public class QueryMethod implements MetaAttribute {
@Override
public String getTypeDeclaration() {
return "jakarta.persistence.Query";
return Constants.QUERY;
}
@Override

View File

@ -6,14 +6,19 @@
*/
package org.hibernate.jpamodelgen.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Hardy Ferentschik
*/
public final class Constants {
// we are trying to reference jpa annotations directly
// All this is to avoid loading classes we don't need to load
public static final String ENTITY = "jakarta.persistence.Entity";
public static final String MAPPED_SUPERCLASS = "jakarta.persistence.MappedSuperclass";
public static final String EMBEDDABLE = "jakarta.persistence.Embeddable";
@ -30,7 +35,6 @@ public final class Constants {
public static final String MAP_KEY_CLASS = "jakarta.persistence.MapKeyClass";
public static final String ELEMENT_COLLECTION = "jakarta.persistence.ElementCollection";
public static final String ACCESS = "jakarta.persistence.Access";
public static final String MAP_ATTRIBUTE = "jakarta.persistence.metamodel.MapAttribute";
public static final String CONVERT = "jakarta.persistence.Convert";
public static final String HIBERNATE_TYPE = "org.hibernate.annotations.Type";
@ -58,55 +62,60 @@ public final class Constants {
public static final String CHECK_HQL = "org.hibernate.annotations.processing.CheckHQL";
public static final Map<String, String> COLLECTIONS = allCollectionTypes();
public static final String ENTITY_MANAGER = "jakarta.persistence.EntityManager";
public static final String QUERY = "jakarta.persistence.Query";
public static final String TYPED_QUERY = "jakarta.persistence.TypedQuery";
public static final String HIB_QUERY = "org.hibernate.query.Query";
public static final String HIB_SELECTION_QUERY = "org.hibernate.query.SelectionQuery";
public static final String HIB_SESSION = "org.hibernate.Session";
private static Map<String, String> allCollectionTypes() {
Map<String, String> map = new java.util.HashMap<>();
map.put( java.util.Collection.class.getName(), "jakarta.persistence.metamodel.CollectionAttribute" );
map.put( java.util.Set.class.getName(), "jakarta.persistence.metamodel.SetAttribute" );
map.put( List.class.getName(), "jakarta.persistence.metamodel.ListAttribute" );
map.put( Map.class.getName(), "jakarta.persistence.metamodel.MapAttribute" );
public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute";
public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";
public static final String SET_ATTRIBUTE = "jakarta.persistence.metamodel.SetAttribute";
public static final String LIST_ATTRIBUTE = "jakarta.persistence.metamodel.ListAttribute";
public static final String MAP_ATTRIBUTE = "jakarta.persistence.metamodel.MapAttribute";
// Hibernate also supports the SortedSet and SortedMap interfaces
map.put( java.util.SortedSet.class.getName(), "jakarta.persistence.metamodel.SetAttribute" );
map.put( java.util.SortedMap.class.getName(), "jakarta.persistence.metamodel.MapAttribute" );
return java.util.Collections.unmodifiableMap( map );
}
public static final String COLLECTION = java.util.Collection.class.getName();
public static final String LIST = java.util.List.class.getName();
public static final String MAP = java.util.Map.class.getName();
public static final String SET = java.util.Set.class.getName();
public static final List<String> BASIC_TYPES = allBasicTypes();
public static final Map<String, String> COLLECTIONS = Map.of(
COLLECTION, Constants.COLLECTION_ATTRIBUTE,
SET, Constants.SET_ATTRIBUTE,
LIST, Constants.LIST_ATTRIBUTE,
MAP, Constants.MAP_ATTRIBUTE,
// Hibernate also supports the SortedSet and SortedMap interfaces
java.util.SortedSet.class.getName(), Constants.SET_ATTRIBUTE,
java.util.SortedMap.class.getName(), Constants.MAP_ATTRIBUTE
);
private static List<String> allBasicTypes() {
java.util.ArrayList<String> strings = new java.util.ArrayList<>();
strings.add( String.class.getName() );
strings.add( Boolean.class.getName() );
strings.add( Byte.class.getName() );
strings.add( Character.class.getName() );
strings.add( Short.class.getName() );
strings.add( Integer.class.getName() );
strings.add( Long.class.getName() );
strings.add( Float.class.getName() );
strings.add( Double.class.getName() );
strings.add( java.math.BigInteger.class.getName() );
strings.add( java.math.BigDecimal.class.getName() );
strings.add( java.util.Date.class.getName() );
strings.add( java.util.Calendar.class.getName() );
strings.add( java.sql.Date.class.getName() );
strings.add( java.sql.Time.class.getName() );
strings.add( java.sql.Timestamp.class.getName() );
strings.add( java.sql.Blob.class.getName() );
return java.util.Collections.unmodifiableList( strings );
}
//TODO: this is not even an exhaustive list of built-in basic types
// so any logic that relies on incomplete this list is broken!
public static final Set<String> BASIC_TYPES = Set.of(
String.class.getName(),
Boolean.class.getName(),
Byte.class.getName(),
Character.class.getName(),
Short.class.getName(),
Integer.class.getName(),
Long.class.getName(),
Float.class.getName(),
Double.class.getName(),
BigInteger.class.getName(),
BigDecimal.class.getName(),
java.util.Date.class.getName(),
java.util.Calendar.class.getName(),
java.sql.Date.class.getName(),
java.sql.Time.class.getName(),
java.sql.Timestamp.class.getName(),
java.sql.Blob.class.getName()
);
public static final List<String> BASIC_ARRAY_TYPES = allBasicArrayTypes();
private static List<String> allBasicArrayTypes() {
java.util.ArrayList<String> strings = new java.util.ArrayList<>();
strings.add( Character.class.getName() );
strings.add( Byte.class.getName() );
return java.util.Collections.unmodifiableList( strings );
}
public static final String PATH_SEPARATOR = "/";
public static final List<String> BASIC_ARRAY_TYPES = List.of(
Character.class.getName(),
Byte.class.getName()
);
private Constants() {
}

View File

@ -88,12 +88,12 @@ public final class NullnessUtil {
* @param <T> the type of the reference
* @param ref a reference of @Nullable type, that is non-null at run time
*
* @return the argument, casted to have the type qualifier @NonNull
* @return the argument, cast to have the type qualifier @NonNull
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T castNonNull(@Nullable T ref) {
assert ref != null : "Misuse of castNonNull: called with a null argument";
return (@NonNull T) ref;
return ref;
}
/**
@ -104,14 +104,14 @@ public final class NullnessUtil {
* @param ref a reference of @Nullable type, that is non-null at run time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull
* @return the argument, cast to have the type qualifier @NonNull
*
* @see #castNonNull(Object)
*/
public static @EnsuresNonNull("#1") <T extends @Nullable Object> @NonNull T castNonNull(
@Nullable T ref, String message) {
assert ref != null : "Misuse of castNonNull: called with a null argument: " + message;
return (@NonNull T) ref;
return ref;
}
/**
@ -123,14 +123,14 @@ public final class NullnessUtil {
* @param arr an array all of whose elements, and their elements recursively, are non-null at run
* time
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [] castNonNullDeep(
T @Nullable [] arr) {
return (@NonNull T[]) castNonNullArray( arr, null );
return castNonNullArray( arr, null );
}
/**
@ -143,14 +143,14 @@ public final class NullnessUtil {
* time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [] castNonNullDeep(
T @Nullable [] arr, String message) {
return (@NonNull T[]) castNonNullArray( arr, message );
return castNonNullArray( arr, message );
}
/**
@ -162,14 +162,14 @@ public final class NullnessUtil {
* @param arr an array all of whose elements, and their elements recursively, are non-null at run
* time
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][] castNonNullDeep(
T @Nullable [] @Nullable [] arr) {
return (@NonNull T[][]) castNonNullArray( arr, null );
return castNonNullArray( arr, null );
}
/**
@ -182,14 +182,14 @@ public final class NullnessUtil {
* time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][] castNonNullDeep(
T @Nullable [] @Nullable [] arr, String message) {
return (@NonNull T[][]) castNonNullArray( arr, message );
return castNonNullArray( arr, message );
}
/**
@ -201,14 +201,14 @@ public final class NullnessUtil {
* @param arr an array all of whose elements, and their elements recursively, are non-null at run
* time
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] arr) {
return (@NonNull T[][][]) castNonNullArray( arr, null );
return castNonNullArray( arr, null );
}
/**
@ -221,14 +221,14 @@ public final class NullnessUtil {
* time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] arr, String message) {
return (@NonNull T[][][]) castNonNullArray( arr, message );
return castNonNullArray( arr, message );
}
/**
@ -240,14 +240,14 @@ public final class NullnessUtil {
* @param arr an array all of whose elements, and their elements recursively, are non-null at run
* time
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] @Nullable [] arr) {
return (@NonNull T[][][][]) castNonNullArray( arr, null );
return castNonNullArray( arr, null );
}
/**
@ -260,14 +260,14 @@ public final class NullnessUtil {
* time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] @Nullable [] arr, String message) {
return (@NonNull T[][][][]) castNonNullArray( arr, message );
return castNonNullArray( arr, message );
}
/**
@ -279,14 +279,14 @@ public final class NullnessUtil {
* @param arr an array all of whose elements, and their elements recursively, are non-null at run
* time
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] @Nullable [] @Nullable [] arr) {
return (@NonNull T[][][][][]) castNonNullArray( arr, null );
return castNonNullArray( arr, null );
}
/**
@ -299,14 +299,14 @@ public final class NullnessUtil {
* time
* @param message text to include if this method is misused
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*
* @see #castNonNull(Object)
*/
@EnsuresNonNull("#1")
public static <T extends @Nullable Object> @NonNull T @NonNull [][][][][] castNonNullDeep(
T @Nullable [] @Nullable [] @Nullable [] @Nullable [] @Nullable [] arr, String message) {
return (@NonNull T[][][][][]) castNonNullArray( arr, message );
return castNonNullArray( arr, message );
}
/**
@ -318,20 +318,20 @@ public final class NullnessUtil {
* @param message text to include if there is a non-null value, or null to use uncustomized
* message
*
* @return the argument, casted to have the type qualifier @NonNull at all levels
* @return the argument, cast to have the type qualifier @NonNull at all levels
*/
private static <T extends @Nullable Object> @NonNull T @NonNull [] castNonNullArray(
T @Nullable [] arr, @Nullable String message) {
assert arr != null
: "Misuse of castNonNullArray: called with a null array argument"
+ ( ( message == null ) ? "" : ( ": " + message ) );
+ ( message == null ? "" : ": " + message );
for ( int i = 0; i < arr.length; ++i ) {
assert arr[i] != null
: "Misuse of castNonNull: called with a null array element"
+ ( ( message == null ) ? "" : ( ": " + message ) );
+ ( message == null ? "" : ": " + message );
checkIfArray( arr[i], message );
}
return (@NonNull T[]) arr;
return arr;
}
/**

View File

@ -23,7 +23,6 @@ import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.jpamodelgen.util.NullnessUtil;
import org.hibernate.jpamodelgen.xml.jaxb.ObjectFactory;
@ -144,20 +143,20 @@ public class XmlParserHelper {
}
private String getPackage(String resourceName) {
if ( !resourceName.contains( Constants.PATH_SEPARATOR ) ) {
if ( !resourceName.contains("/") ) {
return "";
}
else {
return resourceName.substring( 0, resourceName.lastIndexOf( Constants.PATH_SEPARATOR ) );
return resourceName.substring( 0, resourceName.lastIndexOf("/") );
}
}
private String getRelativeName(String resourceName) {
if ( !resourceName.contains( Constants.PATH_SEPARATOR ) ) {
if ( !resourceName.contains("/") ) {
return resourceName;
}
else {
return resourceName.substring( resourceName.lastIndexOf( Constants.PATH_SEPARATOR ) + 1 );
return resourceName.substring( resourceName.lastIndexOf("/") + 1 );
}
}

View File

@ -7,9 +7,7 @@
package org.hibernate.jpamodelgen.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
@ -26,6 +24,7 @@ import org.hibernate.jpamodelgen.model.ImportContext;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.AccessTypeInformation;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.jpamodelgen.util.NullnessUtil;
import org.hibernate.jpamodelgen.util.StringUtil;
import org.hibernate.jpamodelgen.util.TypeUtils;
@ -47,6 +46,10 @@ import org.hibernate.jpamodelgen.xml.jaxb.OneToOne;
import org.checkerframework.checker.nullness.qual.Nullable;
import static org.hibernate.jpamodelgen.util.StringUtil.determineFullyQualifiedClassName;
import static org.hibernate.jpamodelgen.util.TypeUtils.getElementKindForAccessType;
import static org.hibernate.jpamodelgen.xml.jaxb.AccessType.*;
/**
* Collects XML-based meta information about an annotated type (entity, embeddable or mapped superclass).
*
@ -54,15 +57,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
*/
public class XmlMetaEntity implements Metamodel {
static final Map<String, String> COLLECTIONS = new HashMap<String, String>();
static {
COLLECTIONS.put( "java.util.Collection", "jakarta.persistence.metamodel.CollectionAttribute" );
COLLECTIONS.put( "java.util.Set", "jakarta.persistence.metamodel.SetAttribute" );
COLLECTIONS.put( "java.util.List", "jakarta.persistence.metamodel.ListAttribute" );
COLLECTIONS.put( "java.util.Map", "jakarta.persistence.metamodel.MapAttribute" );
}
private final String clazzName;
private final String packageName;
private final String defaultPackageName;
@ -225,11 +219,9 @@ public class XmlMetaEntity implements Metamodel {
}
DeclaredType type = determineDeclaredType( elem );
if ( type == null ) {
continue;
if ( type != null ) {
return determineTypes( propertyName, explicitTargetEntity, explicitMapKeyClass, type );
}
return determineTypes( propertyName, explicitTargetEntity, explicitMapKeyClass, type );
}
return null;
}
@ -251,7 +243,7 @@ public class XmlMetaEntity implements Metamodel {
private @Nullable String[] determineTypes(String propertyName, String explicitTargetEntity, @Nullable String explicitMapKeyClass, DeclaredType type) {
@Nullable String[] types = new String[3];
determineTargetType( type, propertyName, explicitTargetEntity, types );
if ( determineCollectionType( type, types ).equals( "jakarta.persistence.metamodel.MapAttribute" ) ) {
if ( determineCollectionType( type, types ).equals( Constants.MAP_ATTRIBUTE ) ) {
determineMapType( type, explicitMapKeyClass, types );
}
return types;
@ -267,7 +259,7 @@ public class XmlMetaEntity implements Metamodel {
}
private String determineCollectionType(DeclaredType type, @Nullable String[] types) {
return NullnessUtil.castNonNull( types[1] = COLLECTIONS.get( type.asElement().toString() ) );
return NullnessUtil.castNonNull( types[1] = Constants.COLLECTIONS.get( type.asElement().toString() ) );
}
private void determineTargetType(DeclaredType type, String propertyName, String explicitTargetEntity, @Nullable String[] types) {
@ -301,8 +293,8 @@ public class XmlMetaEntity implements Metamodel {
continue;
}
TypeMirror mirror;
String name = elem.getSimpleName().toString();
final TypeMirror mirror;
if ( ElementKind.METHOD.equals( elem.getKind() ) ) {
name = StringUtil.getPropertyName( name );
mirror = ( (ExecutableElement) elem ).getReturnType();
@ -346,7 +338,7 @@ public class XmlMetaEntity implements Metamodel {
return "java.lang.Double";
}
case DECLARED: {
return ((DeclaredType)mirror).asElement().asType().toString();
return ((DeclaredType) mirror).asElement().asType().toString();
}
case TYPEVAR: {
return mirror.toString();
@ -498,9 +490,7 @@ public class XmlMetaEntity implements Metamodel {
private String determineExplicitTargetEntity(String targetClass) {
String explicitTargetClass = targetClass;
if ( explicitTargetClass != null ) {
explicitTargetClass = StringUtil.determineFullyQualifiedClassName(
defaultPackageName, targetClass
);
explicitTargetClass = determineFullyQualifiedClassName( defaultPackageName, targetClass );
}
return explicitTargetClass;
}
@ -508,7 +498,7 @@ public class XmlMetaEntity implements Metamodel {
private @Nullable String determineExplicitMapKeyClass(MapKeyClass mapKeyClass) {
String explicitMapKey = null;
if ( mapKeyClass != null ) {
explicitMapKey = StringUtil.determineFullyQualifiedClassName( defaultPackageName, mapKeyClass.getClazz() );
explicitMapKey = determineFullyQualifiedClassName( defaultPackageName, mapKeyClass.getClazz() );
}
return explicitMapKey;
}
@ -618,14 +608,9 @@ public class XmlMetaEntity implements Metamodel {
private ElementKind getElementKind(org.hibernate.jpamodelgen.xml.jaxb.AccessType accessType) {
// if no explicit access type was specified in xml we use the entity access type
if ( accessType == null ) {
return TypeUtils.getElementKindForAccessType( accessTypeInfo.getAccessType() );
}
if ( org.hibernate.jpamodelgen.xml.jaxb.AccessType.FIELD.equals( accessType ) ) {
return ElementKind.FIELD;
}
else {
return ElementKind.METHOD;
return getElementKindForAccessType( accessTypeInfo.getAccessType() );
}
return FIELD.equals( accessType ) ? ElementKind.FIELD : ElementKind.METHOD;
}
@Override

View File

@ -7,6 +7,7 @@
package org.hibernate.jpamodelgen.xml;
import org.hibernate.jpamodelgen.model.MetaSingleAttribute;
import org.hibernate.jpamodelgen.util.Constants;
/**
* @author Hardy Ferentschik
@ -19,6 +20,6 @@ public class XmlMetaSingleAttribute extends XmlMetaAttribute implements MetaSing
@Override
public String getMetaType() {
return "jakarta.persistence.metamodel.SingularAttribute";
return Constants.SINGULAR_ATTRIBUTE;
}
}