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

View File

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

View File

@ -11,17 +11,22 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
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.ExecutableType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import jakarta.persistence.EntityManager;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.ImportContextImpl;
import org.hibernate.jpamodelgen.ProcessLaterException;
@ -168,7 +173,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
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 ) {
this.entityToMerge = other;
}
@ -220,7 +226,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
protected final void init() {
getContext().logMessage( Diagnostic.Kind.OTHER, "Initializing type " + getQualifiedName() + "." );
getContext().logMessage( Diagnostic.Kind.OTHER, "Initializing type '" + getQualifiedName() + "'" );
determineAccessTypeForHierarchy( element, context );
entityAccessTypeInfo = castNonNull( context.getAccessTypeInfo( getQualifiedName() ) );
@ -271,12 +277,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private static boolean isSessionGetter(ExecutableElement getterOrSetter) {
final TypeMirror type = getterOrSetter.getReturnType();
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 ) {
final Name name = ((TypeElement) element).getQualifiedName();
return name.contentEquals(Session.class.getName())
|| name.contentEquals(EntityManager.class.getName())
|| name.contentEquals(StatelessSession.class.getName());
return name.contentEquals(Constants.HIB_SESSION)
|| name.contentEquals(Constants.HIB_STATELESS_SESSION)
|| name.contentEquals(Constants.ENTITY_MANAGER);
}
}
return false;
@ -495,7 +502,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<String> paramNames = parameterNames( method );
final List<String> paramTypes = parameterTypes( method );
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 ) ) {
putMember( methodKey,
new NaturalIdFinderMethod(
@ -535,7 +542,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final String methodKey = methodName + "!";
switch ( fieldType ) {
case ID:
if ( !Constants.HIB_STATELESS_SESSION.equals(sessionType)
if ( !usingStatelessSession()
|| enabledFetchProfiles( method ).isEmpty() ) { // no byId() API for SS, only get()
putMember( methodKey,
new IdFinderMethod(
@ -552,7 +559,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
break;
}
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,
new NaturalIdFinderMethod(
this,
@ -721,4 +728,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
&& !isPageParam(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 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.constructorName = constructorName;
this.methodName = methodName;

View File

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

View File

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