Skip to content

Commit

Permalink
minor code changes to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Mar 1, 2024
1 parent 6dfdbe1 commit 11fa929
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ boolean isUsingStatelessSession() {

boolean isReactive() {
return MUTINY_SESSION.equals(sessionType)
|| UNI_MUTINY_SESSION.equals(sessionType);
|| UNI_MUTINY_SESSION.equals(sessionType);
}

boolean isReactiveSession() {
Expand All @@ -198,7 +198,12 @@ String localSessionName() {
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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
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;
Expand Down Expand Up @@ -397,7 +395,7 @@ private void addDefaultConstructor() {

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 ) ) {
Expand Down Expand Up @@ -426,9 +424,7 @@ else if ( element.getKind() == ElementKind.INTERFACE
}

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 ) ) {
Expand Down Expand Up @@ -470,7 +466,7 @@ private boolean isOrmPanacheType(TypeElement type) {
Types types = processingEnvironment.getTypeUtils();
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not
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) {
Expand All @@ -484,7 +480,7 @@ private boolean isReactivePanacheType(TypeElement type) {
Types types = processingEnvironment.getTypeUtils();
// check against a raw supertype of PanacheRepositoryBase, which .asType() is not
return types.isSubtype( type.asType(), types.getDeclaredType( panacheRepositorySuperType ) )
|| types.isSubtype( type.asType(), panacheEntitySuperType.asType());
|| types.isSubtype( type.asType(), panacheEntitySuperType.asType());
}

/**
Expand All @@ -498,7 +494,7 @@ private String addDaoConstructor(@Nullable ExecutableElement method) {
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,
Expand Down Expand Up @@ -545,8 +541,8 @@ private String setupQuarkusDaoConstructor() {
false,
false,
true
)
);
)
);
return Constants.ENTITY_MANAGER;
}
else {
Expand Down Expand Up @@ -597,8 +593,18 @@ private boolean isGetterOrSetter(Element methodOfClass) {
}

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(
Expand Down Expand Up @@ -664,7 +670,9 @@ private void addLifecycleMethods(List<ExecutableElement> queryMethods) {

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 );
}
}
Expand All @@ -682,7 +690,7 @@ else if ( kind == TypeKind.DECLARED ) {
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 {
Expand Down Expand Up @@ -758,7 +766,7 @@ private static TypeMirror ununi(TypeMirror returnType) {
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);
Expand Down Expand Up @@ -831,7 +839,7 @@ else if ( returnType == null || returnType.getKind() != TypeKind.VOID ) {
"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 );
Expand Down Expand Up @@ -918,7 +926,7 @@ else if ( returnType.getKind() == TypeKind.ARRAY ) {
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 );
Expand All @@ -932,7 +940,7 @@ else if ( returnType.getKind() == TypeKind.ARRAY ) {
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 );
Expand Down Expand Up @@ -1615,16 +1623,17 @@ private boolean isValidUpdateReturnType(@Nullable TypeMirror returnType, Executa
}
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 {
// non-reactive
return returnType.getKind() == TypeKind.VOID
|| returnType.getKind() == TypeKind.BOOLEAN
|| returnType.getKind() == TypeKind.INT;
|| returnType.getKind() == TypeKind.BOOLEAN
|| returnType.getKind() == TypeKind.INT;
}
}

Expand Down Expand Up @@ -1819,7 +1828,7 @@ private boolean checkReturnedEntity(EntityDomainType<?> model, TypeMirror return
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();
Expand Down Expand Up @@ -2016,7 +2025,7 @@ private static boolean hasParameter(String hql, int i, String param) {

private boolean usingReactiveSession(String sessionType) {
return Constants.MUTINY_SESSION.equals(sessionType)
|| Constants.UNI_MUTINY_SESSION.equals(sessionType);
|| Constants.UNI_MUTINY_SESSION.equals(sessionType);
}

private boolean usingStatelessSession(String sessionType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 11fa929

Please sign in to comment.