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

@ -198,7 +198,12 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
void chainSession(StringBuilder declaration) {
// Reactive calls always have a return type
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.regex.Pattern;
import jakarta.persistence.EntityManager;
import static java.beans.Introspector.decapitalize;
import static java.lang.Boolean.FALSE;
import static java.util.Collections.emptyList;
@ -397,7 +395,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private void setupSession() {
jakartaDataRepository = hasAnnotation( element, JD_REPOSITORY );
ExecutableElement getter = findSessionGetter( element );
final ExecutableElement getter = findSessionGetter( element );
if ( getter != null ) {
// Never make a DAO for Panache subtypes
if ( !isPanacheType( element ) ) {
@ -426,9 +424,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private @Nullable ExecutableElement findSessionGetter(TypeElement type) {
if ( ( !hasAnnotation( type, Constants.ENTITY )
&& !hasAnnotation( type, Constants.MAPPED_SUPERCLASS )
&& !hasAnnotation( type, Constants.EMBEDDABLE ) )
if ( !hasAnnotation( type, ENTITY, MAPPED_SUPERCLASS, EMBEDDABLE )
|| isPanacheType( type ) ) {
for ( ExecutableElement method : methodsIn( type.getEnclosedElements() ) ) {
if ( isSessionGetter( method ) ) {
@ -498,7 +494,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final String name = method == null ? sessionVariableName : method.getSimpleName().toString();
final String typeName = element.getSimpleName().toString() + '_';
if( method == null || !method.isDefault() ) {
if ( method == null || !method.isDefault() ) {
putMember( name,
new RepositoryConstructor(
this,
@ -597,8 +593,18 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private static boolean hasPrefix(Name methodSimpleName, String prefix) {
return methodSimpleName.length() > prefix.length()
&& methodSimpleName.subSequence( 0, prefix.length() ).toString().equals( prefix );
final int prefixLength = prefix.length();
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(
@ -664,7 +670,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private void addQueryMethods(List<ExecutableElement> 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 );
}
}
@ -682,7 +690,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
switch ( typeArguments.size() ) {
case 0:
if ( containsAnnotation( declaredType.asElement(), Constants.ENTITY ) ) {
if ( containsAnnotation( declaredType.asElement(), ENTITY ) ) {
addQueryMethod( method, declaredType, null );
}
else {
@ -758,7 +766,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( returnType.getKind() != TypeKind.DECLARED ) {
return returnType;
}
DeclaredType declaredType = (DeclaredType) returnType;
final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement typeElement = (TypeElement) declaredType.asElement();
if ( typeElement.getQualifiedName().contentEquals( Constants.UNI ) ) {
returnType = declaredType.getTypeArguments().get(0);
@ -831,7 +839,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
"incorrect parameter type '" + parameterType + "' is not an entity type",
Diagnostic.Kind.ERROR );
}
else if ( !containsAnnotation( declaredType.asElement(), Constants.ENTITY ) ) {
else if ( !containsAnnotation( declaredType.asElement(), ENTITY ) ) {
context.message( parameter,
"incorrect parameter type '" + parameterType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR );
@ -918,7 +926,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else {
final DeclaredType declaredType = (DeclaredType) componentType;
final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) {
if ( !containsAnnotation( entity, ENTITY ) ) {
context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR );
@ -932,7 +940,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, Constants.ENTITY ) ) {
if ( !containsAnnotation( entity, ENTITY ) ) {
context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR );
@ -1615,9 +1623,10 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
if ( reactive ) {
// 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 )
|| method.getReturnType().toString().equals( Constants.UNI_BOOLEAN )
|| method.getReturnType().toString().equals( Constants.UNI_INTEGER );
final String returnTypeName = method.getReturnType().toString();
return returnTypeName.equals( Constants.UNI_VOID )
|| returnTypeName.equals( Constants.UNI_BOOLEAN )
|| returnTypeName.equals( Constants.UNI_INTEGER );
}
else {
@ -1819,7 +1828,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType;
final TypeElement typeElement = (TypeElement) declaredType.asElement();
final AnnotationMirror mirror = getAnnotationMirror(typeElement, Constants.ENTITY );
final AnnotationMirror mirror = getAnnotationMirror(typeElement, ENTITY );
if ( mirror != null ) {
final Object value = getAnnotationValue( mirror, "name" );
final String entityName = value instanceof String ? (String) value : typeElement.getSimpleName().toString();

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_VOID = UNI+"<java.lang.Void>";
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 COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";