HHH-16633 minor cleanups

This commit is contained in:
Gavin King 2023-07-09 22:17:23 +02:00
parent db4d529f60
commit cd75b0baf1
6 changed files with 57 additions and 54 deletions

View File

@ -32,8 +32,6 @@ import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity;
import org.hibernate.jpamodelgen.annotation.AnnotationMetaPackage; import org.hibernate.jpamodelgen.annotation.AnnotationMetaPackage;
import org.hibernate.jpamodelgen.model.Metamodel; import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants; import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.jpamodelgen.util.StringUtil;
import org.hibernate.jpamodelgen.util.TypeUtils;
import org.hibernate.jpamodelgen.xml.JpaDescriptorParser; import org.hibernate.jpamodelgen.xml.JpaDescriptorParser;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -44,8 +42,8 @@ import static javax.lang.model.util.ElementFilter.methodsIn;
import static org.hibernate.jpamodelgen.util.Constants.FIND; import static org.hibernate.jpamodelgen.util.Constants.FIND;
import static org.hibernate.jpamodelgen.util.Constants.HQL; import static org.hibernate.jpamodelgen.util.Constants.HQL;
import static org.hibernate.jpamodelgen.util.Constants.SQL; import static org.hibernate.jpamodelgen.util.Constants.SQL;
import static org.hibernate.jpamodelgen.util.TypeUtils.containsAnnotation; import static org.hibernate.jpamodelgen.util.StringUtil.isProperty;
import static org.hibernate.jpamodelgen.util.TypeUtils.isAnnotationMirrorOfType; import static org.hibernate.jpamodelgen.util.TypeUtils.*;
/** /**
* Main annotation processor. * Main annotation processor.
@ -105,8 +103,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
boolean fullyAnnotationConfigured = handleSettings( processingEnvironment ); boolean fullyAnnotationConfigured = handleSettings( processingEnvironment );
if ( !fullyAnnotationConfigured ) { if ( !fullyAnnotationConfigured ) {
JpaDescriptorParser parser = new JpaDescriptorParser( context ); new JpaDescriptorParser( context ).parseXml();
parser.parseXml();
if ( context.isFullyXmlConfigured() ) { if ( context.isFullyXmlConfigured() ) {
createMetaModelClasses(); createMetaModelClasses();
} }
@ -132,7 +129,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null ); context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null );
} }
context.setAddGenerationDate(parseBoolean( options.get( ADD_GENERATION_DATE ) ) ); context.setAddGenerationDate( parseBoolean( options.get( ADD_GENERATION_DATE ) ) );
context.setAddSuppressWarningsAnnotation( parseBoolean( options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ) ) ); context.setAddSuppressWarningsAnnotation( parseBoolean( options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ) ) );
@ -351,11 +348,9 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
+ "' since xml configuration is metadata complete."); + "' since xml configuration is metadata complete.");
} }
else { else {
boolean requiresLazyMemberInitialization = false; boolean requiresLazyMemberInitialization
if ( containsAnnotation( element, Constants.EMBEDDABLE ) || = containsAnnotation( element, Constants.EMBEDDABLE )
containsAnnotation( element, Constants.MAPPED_SUPERCLASS ) ) { || containsAnnotation( element, Constants.MAPPED_SUPERCLASS );
requiresLazyMemberInitialization = true;
}
final AnnotationMetaEntity metaEntity = final AnnotationMetaEntity metaEntity =
AnnotationMetaEntity.create( (TypeElement) element, context, requiresLazyMemberInitialization ); AnnotationMetaEntity.create( (TypeElement) element, context, requiresLazyMemberInitialization );
if ( alreadyExistingMetaEntity != null ) { if ( alreadyExistingMetaEntity != null ) {
@ -369,12 +364,12 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
private void handleRootElementAuxiliaryAnnotationMirrors(final Element element) { private void handleRootElementAuxiliaryAnnotationMirrors(final Element element) {
if ( element instanceof TypeElement ) { if ( element instanceof TypeElement ) {
AnnotationMetaEntity metaEntity = final AnnotationMetaEntity metaEntity =
AnnotationMetaEntity.create( (TypeElement) element, context, false ); AnnotationMetaEntity.create( (TypeElement) element, context, false );
context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity );
} }
else if ( element instanceof PackageElement ) { else if ( element instanceof PackageElement ) {
AnnotationMetaPackage metaEntity = final AnnotationMetaPackage metaEntity =
AnnotationMetaPackage.create( (PackageElement) element, context ); AnnotationMetaPackage.create( (PackageElement) element, context );
context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity );
} }
@ -382,15 +377,14 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
} }
private @Nullable Metamodel tryGettingExistingEntityFromContext(AnnotationMirror mirror, String qualifiedName) { private @Nullable Metamodel tryGettingExistingEntityFromContext(AnnotationMirror mirror, String qualifiedName) {
Metamodel alreadyExistingMetaEntity = null;
if ( isAnnotationMirrorOfType( mirror, Constants.ENTITY ) if ( isAnnotationMirrorOfType( mirror, Constants.ENTITY )
|| isAnnotationMirrorOfType( mirror, Constants.MAPPED_SUPERCLASS ) ) { || isAnnotationMirrorOfType( mirror, Constants.MAPPED_SUPERCLASS ) ) {
alreadyExistingMetaEntity = context.getMetaEntity( qualifiedName ); return context.getMetaEntity( qualifiedName );
} }
else if ( isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) { else if ( isAnnotationMirrorOfType( mirror, Constants.EMBEDDABLE ) ) {
alreadyExistingMetaEntity = context.getMetaEmbeddable( qualifiedName ); return context.getMetaEmbeddable( qualifiedName );
} }
return alreadyExistingMetaEntity; return null;
} }
private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntity metaEntity) { private void addMetaEntityToContext(AnnotationMirror mirror, AnnotationMetaEntity metaEntity) {
@ -420,36 +414,26 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
public Boolean visitDeclared(DeclaredType declaredType, Element element) { public Boolean visitDeclared(DeclaredType declaredType, Element element) {
TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
String fqNameOfReturnType = returnedElement.getQualifiedName().toString(); final String fqNameOfReturnType = returnedElement.getQualifiedName().toString();
String collection = Constants.COLLECTIONS.get( fqNameOfReturnType ); final String collection = Constants.COLLECTIONS.get( fqNameOfReturnType );
if ( collection != null ) { if ( collection != null ) {
TypeMirror collectionElementType = TypeUtils.getCollectionElementType( final TypeMirror collectionElementType =
declaredType, fqNameOfReturnType, null, context getCollectionElementType( declaredType, fqNameOfReturnType, null, context );
);
final Element collectionElement = context.getTypeUtils().asElement( collectionElementType ); final Element collectionElement = context.getTypeUtils().asElement( collectionElementType );
if ( ElementKind.TYPE_PARAMETER.equals( collectionElement.getKind() ) ) { if ( ElementKind.TYPE_PARAMETER.equals( collectionElement.getKind() ) ) {
return Boolean.FALSE; return false;
} }
returnedElement = (TypeElement) collectionElement; returnedElement = (TypeElement) collectionElement;
} }
return type.getQualifiedName().toString().equals( returnedElement.getQualifiedName().toString() ); return type.getQualifiedName().contentEquals( returnedElement.getQualifiedName() );
} }
@Override @Override
public Boolean visitExecutable(ExecutableType t, Element element) { public Boolean visitExecutable(ExecutableType t, Element element) {
if ( !element.getKind().equals( ElementKind.METHOD ) ) { return element.getKind().equals(ElementKind.METHOD)
return false; && isProperty( element.getSimpleName().toString(), toTypeString( t.getReturnType() ) )
} && t.getReturnType().accept( this, element );
String string = element.getSimpleName().toString();
if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) {
return false;
}
return t.getReturnType().accept( this, element );
} }
} }
} }

View File

@ -37,7 +37,9 @@ public abstract class AbstractFinderMethod implements MetaAttribute {
String entity, String entity,
boolean belongsToDao, boolean belongsToDao,
String sessionType, String sessionType,
List<String> fetchProfiles, List<String> paramNames, List<String> paramTypes) { List<String> fetchProfiles,
List<String> paramNames,
List<String> paramTypes) {
this.annotationMetaEntity = annotationMetaEntity; this.annotationMetaEntity = annotationMetaEntity;
this.methodName = methodName; this.methodName = methodName;
this.entity = entity; this.entity = entity;

View File

@ -11,17 +11,22 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.lang.model.element.*; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType; import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import jakarta.persistence.EntityManager;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.jpamodelgen.Context; import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.ImportContextImpl; import org.hibernate.jpamodelgen.ImportContextImpl;
import org.hibernate.jpamodelgen.ProcessLaterException; import org.hibernate.jpamodelgen.ProcessLaterException;
@ -168,7 +173,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
public void mergeInMembers(Metamodel other) { public void mergeInMembers(Metamodel other) {
// store the entity in order do the merge lazily in case of a non-initialized embeddedable or mapped superclass // store the entity in order do the merge lazily in case of
// an uninitialized embeddedable or mapped superclass
if ( !initialized ) { if ( !initialized ) {
this.entityToMerge = other; this.entityToMerge = other;
} }
@ -220,7 +226,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
protected final void init() { protected final void init() {
getContext().logMessage( Diagnostic.Kind.OTHER, "Initializing type " + getQualifiedName() + "." ); getContext().logMessage( Diagnostic.Kind.OTHER, "Initializing type '" + getQualifiedName() + "'" );
determineAccessTypeForHierarchy( element, context ); determineAccessTypeForHierarchy( element, context );
entityAccessTypeInfo = castNonNull( context.getAccessTypeInfo( getQualifiedName() ) ); entityAccessTypeInfo = castNonNull( context.getAccessTypeInfo( getQualifiedName() ) );
@ -271,12 +277,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private static boolean isSessionGetter(ExecutableElement getterOrSetter) { private static boolean isSessionGetter(ExecutableElement getterOrSetter) {
final TypeMirror type = getterOrSetter.getReturnType(); final TypeMirror type = getterOrSetter.getReturnType();
if ( type.getKind() == TypeKind.DECLARED ) { if ( type.getKind() == TypeKind.DECLARED ) {
final Element element = ((DeclaredType) type).asElement(); final DeclaredType declaredType = (DeclaredType) type;
final Element element = declaredType.asElement();
if ( element.getKind() == ElementKind.INTERFACE ) { if ( element.getKind() == ElementKind.INTERFACE ) {
final Name name = ((TypeElement) element).getQualifiedName(); final Name name = ((TypeElement) element).getQualifiedName();
return name.contentEquals(Session.class.getName()) return name.contentEquals(Constants.HIB_SESSION)
|| name.contentEquals(EntityManager.class.getName()) || name.contentEquals(Constants.HIB_STATELESS_SESSION)
|| name.contentEquals(StatelessSession.class.getName()); || name.contentEquals(Constants.ENTITY_MANAGER);
} }
} }
return false; return false;
@ -495,7 +502,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<String> paramNames = parameterNames( method ); final List<String> paramNames = parameterNames( method );
final List<String> paramTypes = parameterTypes( method ); final List<String> paramTypes = parameterTypes( method );
final String methodKey = methodName + paramTypes; final String methodKey = methodName + paramTypes;
if ( !Constants.HIB_STATELESS_SESSION.equals(sessionType) // no byNaturalId() lookup API for SS if ( !usingStatelessSession() // no byNaturalId() lookup API for SS
&& matchesNaturalKey( method, entity ) ) { && matchesNaturalKey( method, entity ) ) {
putMember( methodKey, putMember( methodKey,
new NaturalIdFinderMethod( new NaturalIdFinderMethod(
@ -535,7 +542,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final String methodKey = methodName + "!"; final String methodKey = methodName + "!";
switch ( fieldType ) { switch ( fieldType ) {
case ID: case ID:
if ( !Constants.HIB_STATELESS_SESSION.equals(sessionType) if ( !usingStatelessSession()
|| enabledFetchProfiles( method ).isEmpty() ) { // no byId() API for SS, only get() || enabledFetchProfiles( method ).isEmpty() ) { // no byId() API for SS, only get()
putMember( methodKey, putMember( methodKey,
new IdFinderMethod( new IdFinderMethod(
@ -552,7 +559,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
break; break;
} }
case NATURAL_ID: case NATURAL_ID:
if ( !Constants.HIB_STATELESS_SESSION.equals(sessionType) ) { // no byNaturalId() lookup API for SS if ( !usingStatelessSession()) { // no byNaturalId() lookup API for SS
putMember( methodKey, putMember( methodKey,
new NaturalIdFinderMethod( new NaturalIdFinderMethod(
this, this,
@ -721,4 +728,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
&& !isPageParam(type) && !isPageParam(type)
&& !isOrderParam(type); && !isOrderParam(type);
} }
private boolean usingStatelessSession() {
return Constants.HIB_STATELESS_SESSION.equals(sessionType);
}
} }

View File

@ -20,7 +20,12 @@ public class DaoConstructor implements MetaAttribute {
private final String returnTypeName; private final String returnTypeName;
private final boolean inject; private final boolean inject;
public DaoConstructor(Metamodel annotationMetaEntity, String constructorName, String methodName, String returnTypeName, boolean inject) { public DaoConstructor(
Metamodel annotationMetaEntity,
String constructorName,
String methodName,
String returnTypeName,
boolean inject) {
this.annotationMetaEntity = annotationMetaEntity; this.annotationMetaEntity = annotationMetaEntity;
this.constructorName = constructorName; this.constructorName = constructorName;
this.methodName = methodName; this.methodName = methodName;

View File

@ -17,6 +17,7 @@ import java.util.List;
public class IdFinderMethod extends AbstractFinderMethod { public class IdFinderMethod extends AbstractFinderMethod {
private final String paramName; private final String paramName;
private final boolean usingStatelessSession;
public IdFinderMethod( public IdFinderMethod(
Metamodel annotationMetaEntity, Metamodel annotationMetaEntity,
@ -28,6 +29,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, fetchProfiles, super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, fetchProfiles,
List.of(paramName), List.of(paramType) ); List.of(paramName), List.of(paramType) );
this.paramName = paramName; this.paramName = paramName;
usingStatelessSession = Constants.HIB_STATELESS_SESSION.equals(sessionType);
} }
@Override @Override
@ -35,7 +37,6 @@ public class IdFinderMethod extends AbstractFinderMethod {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
comment( declaration ); comment( declaration );
preamble( declaration ); preamble( declaration );
final boolean usingStatelessSession = Constants.HIB_STATELESS_SESSION.equals(sessionType);
if ( usingStatelessSession || usingEntityManager && fetchProfiles.isEmpty() ) { if ( usingStatelessSession || usingEntityManager && fetchProfiles.isEmpty() ) {
declaration declaration
.append(usingStatelessSession ? ".get(" : ".find(") .append(usingStatelessSession ? ".get(" : ".find(")

View File

@ -138,7 +138,7 @@ public class QueryMethod implements MetaAttribute {
if ( paramType.endsWith("...") ) { if ( paramType.endsWith("...") ) {
declaration declaration
.append("\n\t\t\t.setOrder(") .append("\n\t\t\t.setOrder(")
.append(annotationMetaEntity.importType(List.class.getName())) .append(annotationMetaEntity.importType(Constants.LIST))
.append(".of(") .append(".of(")
.append(paramName) .append(paramName)
.append("))"); .append("))");