minor code changes to generator

This commit is contained in:
Gavin King 2024-03-01 18:51:31 +01:00
parent 6dfdbe197f
commit 11fa929f20
3 changed files with 43 additions and 29 deletions

View File

@ -184,7 +184,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
boolean isReactive() { boolean isReactive() {
return MUTINY_SESSION.equals(sessionType) return MUTINY_SESSION.equals(sessionType)
|| UNI_MUTINY_SESSION.equals(sessionType); || UNI_MUTINY_SESSION.equals(sessionType);
} }
boolean isReactiveSession() { boolean isReactiveSession() {
@ -198,7 +198,12 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
void chainSession(StringBuilder declaration) { void chainSession(StringBuilder declaration) {
// Reactive calls always have a return type // Reactive calls always have a return type
if ( isReactiveSession() ) { if ( isReactiveSession() ) {
declaration.append("\treturn ").append(sessionName).append(".chain(").append(localSessionName()).append(" -> {\n\t"); declaration
.append("\treturn ")
.append(sessionName)
.append(".chain(")
.append(localSessionName())
.append(" -> {\n\t");
} }
} }

View File

@ -58,8 +58,6 @@ import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import jakarta.persistence.EntityManager;
import static java.beans.Introspector.decapitalize; import static java.beans.Introspector.decapitalize;
import static java.lang.Boolean.FALSE; import static java.lang.Boolean.FALSE;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
@ -397,7 +395,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private void setupSession() { private void setupSession() {
jakartaDataRepository = hasAnnotation( element, JD_REPOSITORY ); jakartaDataRepository = hasAnnotation( element, JD_REPOSITORY );
ExecutableElement getter = findSessionGetter( element ); final ExecutableElement getter = findSessionGetter( element );
if ( getter != null ) { if ( getter != null ) {
// Never make a DAO for Panache subtypes // Never make a DAO for Panache subtypes
if ( !isPanacheType( element ) ) { if ( !isPanacheType( element ) ) {
@ -426,9 +424,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private @Nullable ExecutableElement findSessionGetter(TypeElement type) { private @Nullable ExecutableElement findSessionGetter(TypeElement type) {
if ( ( !hasAnnotation( type, Constants.ENTITY ) if ( !hasAnnotation( type, ENTITY, MAPPED_SUPERCLASS, EMBEDDABLE )
&& !hasAnnotation( type, Constants.MAPPED_SUPERCLASS )
&& !hasAnnotation( type, Constants.EMBEDDABLE ) )
|| isPanacheType( type ) ) { || isPanacheType( type ) ) {
for ( ExecutableElement method : methodsIn( type.getEnclosedElements() ) ) { for ( ExecutableElement method : methodsIn( type.getEnclosedElements() ) ) {
if ( isSessionGetter( method ) ) { if ( isSessionGetter( method ) ) {
@ -470,7 +466,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Types types = processingEnvironment.getTypeUtils(); Types types = processingEnvironment.getTypeUtils();
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not // check against a raw supertype of PanacheRepositoryBase, which .asType() is not
return processingEnvironment.getTypeUtils().isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) return processingEnvironment.getTypeUtils().isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) )
|| processingEnvironment.getTypeUtils().isSubtype( type.asType(), panacheEntitySuperType.asType() ); || processingEnvironment.getTypeUtils().isSubtype( type.asType(), panacheEntitySuperType.asType() );
} }
private boolean isReactivePanacheType(TypeElement type) { private boolean isReactivePanacheType(TypeElement type) {
@ -484,7 +480,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Types types = processingEnvironment.getTypeUtils(); Types types = processingEnvironment.getTypeUtils();
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not // check against a raw supertype of PanacheRepositoryBase, which .asType() is not
return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) ) return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) )
|| types.isSubtype( type.asType(), panacheEntitySuperType.asType()); || types.isSubtype( type.asType(), panacheEntitySuperType.asType());
} }
/** /**
@ -498,7 +494,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final String name = method == null ? sessionVariableName : method.getSimpleName().toString(); final String name = method == null ? sessionVariableName : method.getSimpleName().toString();
final String typeName = element.getSimpleName().toString() + '_'; final String typeName = element.getSimpleName().toString() + '_';
if( method == null || !method.isDefault() ) { if ( method == null || !method.isDefault() ) {
putMember( name, putMember( name,
new RepositoryConstructor( new RepositoryConstructor(
this, this,
@ -545,8 +541,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
false, false,
false, false,
true true
) )
); );
return Constants.ENTITY_MANAGER; return Constants.ENTITY_MANAGER;
} }
else { else {
@ -597,8 +593,18 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private static boolean hasPrefix(Name methodSimpleName, String prefix) { private static boolean hasPrefix(Name methodSimpleName, String prefix) {
return methodSimpleName.length() > prefix.length() final int prefixLength = prefix.length();
&& methodSimpleName.subSequence( 0, prefix.length() ).toString().equals( prefix ); if ( methodSimpleName.length() > prefixLength ) {
for ( int i = 0; i < prefixLength; i++ ) {
if ( methodSimpleName.charAt(i) != prefix.charAt(i) ) {
return false;
}
}
return true;
}
else {
return false;
}
} }
private static boolean isGetter( private static boolean isGetter(
@ -664,7 +670,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private void addQueryMethods(List<ExecutableElement> queryMethods) { private void addQueryMethods(List<ExecutableElement> queryMethods) {
for ( ExecutableElement method : queryMethods) { for ( ExecutableElement method : queryMethods) {
if ( method.getModifiers().contains(Modifier.ABSTRACT) || method.getModifiers().contains(Modifier.NATIVE) ) { final Set<Modifier> modifiers = method.getModifiers();
if ( modifiers.contains(Modifier.ABSTRACT)
|| modifiers.contains(Modifier.NATIVE) ) {
addQueryMethod( method ); addQueryMethod( method );
} }
} }
@ -682,7 +690,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
switch ( typeArguments.size() ) { switch ( typeArguments.size() ) {
case 0: case 0:
if ( containsAnnotation( declaredType.asElement(), Constants.ENTITY ) ) { if ( containsAnnotation( declaredType.asElement(), ENTITY ) ) {
addQueryMethod( method, declaredType, null ); addQueryMethod( method, declaredType, null );
} }
else { else {
@ -758,7 +766,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( returnType.getKind() != TypeKind.DECLARED ) { if ( returnType.getKind() != TypeKind.DECLARED ) {
return returnType; return returnType;
} }
DeclaredType declaredType = (DeclaredType) returnType; final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement typeElement = (TypeElement) declaredType.asElement(); final TypeElement typeElement = (TypeElement) declaredType.asElement();
if ( typeElement.getQualifiedName().contentEquals( Constants.UNI ) ) { if ( typeElement.getQualifiedName().contentEquals( Constants.UNI ) ) {
returnType = declaredType.getTypeArguments().get(0); returnType = declaredType.getTypeArguments().get(0);
@ -831,7 +839,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
"incorrect parameter type '" + parameterType + "' is not an entity type", "incorrect parameter type '" + parameterType + "' is not an entity type",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else if ( !containsAnnotation( declaredType.asElement(), Constants.ENTITY ) ) { else if ( !containsAnnotation( declaredType.asElement(), ENTITY ) ) {
context.message( parameter, context.message( parameter,
"incorrect parameter type '" + parameterType + "' is not annotated '@Entity'", "incorrect parameter type '" + parameterType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
@ -918,7 +926,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else { else {
final DeclaredType declaredType = (DeclaredType) componentType; final DeclaredType declaredType = (DeclaredType) componentType;
final TypeElement entity = (TypeElement) declaredType.asElement(); final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) { if ( !containsAnnotation( entity, ENTITY ) ) {
context.message( method, context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'", "incorrect return type '" + returnType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
@ -932,7 +940,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else if ( returnType.getKind() == TypeKind.DECLARED ) { else if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType; final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement entity = (TypeElement) declaredType.asElement(); final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) { if ( !containsAnnotation( entity, ENTITY ) ) {
context.message( method, context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'", "incorrect return type '" + returnType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
@ -1615,16 +1623,17 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
if ( reactive ) { if ( reactive ) {
// for reactive calls, don't use the returnType param, which has been ununi-ed, we want to check the full one // for reactive calls, don't use the returnType param, which has been ununi-ed, we want to check the full one
return method.getReturnType().toString().equals( Constants.UNI_VOID ) final String returnTypeName = method.getReturnType().toString();
|| method.getReturnType().toString().equals( Constants.UNI_BOOLEAN ) return returnTypeName.equals( Constants.UNI_VOID )
|| method.getReturnType().toString().equals( Constants.UNI_INTEGER ); || returnTypeName.equals( Constants.UNI_BOOLEAN )
|| returnTypeName.equals( Constants.UNI_INTEGER );
} }
else { else {
// non-reactive // non-reactive
return returnType.getKind() == TypeKind.VOID return returnType.getKind() == TypeKind.VOID
|| returnType.getKind() == TypeKind.BOOLEAN || returnType.getKind() == TypeKind.BOOLEAN
|| returnType.getKind() == TypeKind.INT; || returnType.getKind() == TypeKind.INT;
} }
} }
@ -1819,7 +1828,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( returnType.getKind() == TypeKind.DECLARED ) { if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType; final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement typeElement = (TypeElement) declaredType.asElement(); final TypeElement typeElement = (TypeElement) declaredType.asElement();
final AnnotationMirror mirror = getAnnotationMirror(typeElement, Constants.ENTITY ); final AnnotationMirror mirror = getAnnotationMirror(typeElement, ENTITY );
if ( mirror != null ) { if ( mirror != null ) {
final Object value = getAnnotationValue( mirror, "name" ); final Object value = getAnnotationValue( mirror, "name" );
final String entityName = value instanceof String ? (String) value : typeElement.getSimpleName().toString(); final String entityName = value instanceof String ? (String) value : typeElement.getSimpleName().toString();
@ -2016,7 +2025,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private boolean usingReactiveSession(String sessionType) { private boolean usingReactiveSession(String sessionType) {
return Constants.MUTINY_SESSION.equals(sessionType) return Constants.MUTINY_SESSION.equals(sessionType)
|| Constants.UNI_MUTINY_SESSION.equals(sessionType); || Constants.UNI_MUTINY_SESSION.equals(sessionType);
} }
private boolean usingStatelessSession(String sessionType) { private boolean usingStatelessSession(String sessionType) {

View File

@ -103,7 +103,7 @@ public final class Constants {
public static final String UNI_INTEGER = UNI+"<java.lang.Integer>"; public static final String UNI_INTEGER = UNI+"<java.lang.Integer>";
public static final String UNI_VOID = UNI+"<java.lang.Void>"; public static final String UNI_VOID = UNI+"<java.lang.Void>";
public static final String UNI_BOOLEAN = UNI+"<java.lang.Boolean>"; public static final String UNI_BOOLEAN = UNI+"<java.lang.Boolean>";
public static final String BOXED_VOID = Void.class.getName(); public static final String BOXED_VOID = "java.lang.Void";
public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute"; public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute";
public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute"; public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";