From 7abe8f5f2b8ad8cf53768fddf19abaf70cdc89b1 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 24 Feb 2024 11:43:02 +0100 Subject: [PATCH] split out an inner class --- .../ContainsAttributeTypeVisitor.java | 57 +++++++++++++++++++ .../JPAMetaModelEntityProcessor.java | 42 -------------- .../hibernate/jpamodelgen/util/TypeUtils.java | 5 +- 3 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ContainsAttributeTypeVisitor.java diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ContainsAttributeTypeVisitor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ContainsAttributeTypeVisitor.java new file mode 100644 index 0000000000..390d5417bb --- /dev/null +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ContainsAttributeTypeVisitor.java @@ -0,0 +1,57 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpamodelgen; + +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.ExecutableType; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.SimpleTypeVisitor8; + +import static org.hibernate.jpamodelgen.util.Constants.COLLECTIONS; +import static org.hibernate.jpamodelgen.util.StringUtil.isProperty; +import static org.hibernate.jpamodelgen.util.TypeUtils.getCollectionElementType; +import static org.hibernate.jpamodelgen.util.TypeUtils.toTypeString; + +class ContainsAttributeTypeVisitor extends SimpleTypeVisitor8 { + + private final Context context; + private final TypeElement type; + + ContainsAttributeTypeVisitor(TypeElement elem, Context context) { + this.context = context; + this.type = elem; + } + + @Override + public Boolean visitDeclared(DeclaredType declaredType, Element element) { + TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement(declaredType); + + final String returnTypeName = returnedElement.getQualifiedName().toString(); + final String collection = COLLECTIONS.get(returnTypeName); + if (collection != null) { + final TypeMirror collectionElementType = + getCollectionElementType( declaredType, returnTypeName, null, context ); + final Element collectionElement = context.getTypeUtils().asElement(collectionElementType); + if ( ElementKind.TYPE_PARAMETER == collectionElement.getKind() ) { + return false; + } + returnedElement = (TypeElement) collectionElement; + } + + return type.getQualifiedName().contentEquals( returnedElement.getQualifiedName() ); + } + + @Override + public Boolean visitExecutable(ExecutableType executable, Element element) { + return element.getKind() == ElementKind.METHOD + && isProperty( element.getSimpleName().toString(), toTypeString( executable.getReturnType() ) ) + && executable.getReturnType().accept(this, element); + } +} diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index 4339950ac8..0cf0052954 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -19,14 +19,10 @@ import javax.annotation.processing.SupportedOptions; import javax.lang.model.SourceVersion; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; -import javax.lang.model.element.ElementKind; import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; -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.lang.model.util.SimpleTypeVisitor8; import javax.tools.Diagnostic; import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity; @@ -40,7 +36,6 @@ import static java.lang.Boolean.parseBoolean; import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.methodsIn; import static org.hibernate.jpamodelgen.util.Constants.*; -import static org.hibernate.jpamodelgen.util.StringUtil.isProperty; import static org.hibernate.jpamodelgen.util.TypeUtils.*; import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.*; @@ -456,41 +451,4 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { } } - - static class ContainsAttributeTypeVisitor extends SimpleTypeVisitor8 { - - private final Context context; - private final TypeElement type; - - ContainsAttributeTypeVisitor(TypeElement elem, Context context) { - this.context = context; - this.type = elem; - } - - @Override - public Boolean visitDeclared(DeclaredType declaredType, Element element) { - TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); - - final String fqNameOfReturnType = returnedElement.getQualifiedName().toString(); - final String collection = COLLECTIONS.get( fqNameOfReturnType ); - if ( collection != null ) { - final TypeMirror collectionElementType = - getCollectionElementType( declaredType, fqNameOfReturnType, null, context ); - final Element collectionElement = context.getTypeUtils().asElement( collectionElementType ); - if ( ElementKind.TYPE_PARAMETER.equals( collectionElement.getKind() ) ) { - return false; - } - returnedElement = (TypeElement) collectionElement; - } - - return type.getQualifiedName().contentEquals( returnedElement.getQualifiedName() ); - } - - @Override - public Boolean visitExecutable(ExecutableType t, Element element) { - return element.getKind().equals(ElementKind.METHOD) - && isProperty( element.getSimpleName().toString(), toTypeString( t.getReturnType() ) ) - && t.getReturnType().accept( this, element ); - } - } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java index cfbda3bf4f..374997dfbe 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java @@ -37,6 +37,7 @@ import org.hibernate.jpamodelgen.MetaModelGenerationException; import org.checkerframework.checker.nullness.qual.Nullable; import static org.hibernate.jpamodelgen.util.Constants.JAVA_OBJECT; +import static org.hibernate.jpamodelgen.util.Constants.MAP; import static org.hibernate.jpamodelgen.util.NullnessUtil.castNonNull; import static org.hibernate.jpamodelgen.util.StringUtil.isProperty; @@ -327,7 +328,7 @@ public final class TypeUtils { } public static TypeMirror getCollectionElementType( - DeclaredType type, String returnTupeName, @Nullable String explicitTargetEntityName, Context context) { + DeclaredType type, String returnTypeName, @Nullable String explicitTargetEntityName, Context context) { if ( explicitTargetEntityName != null ) { return context.getElementUtils().getTypeElement( explicitTargetEntityName ).asType(); } @@ -336,7 +337,7 @@ public final class TypeUtils { if ( typeArguments.isEmpty() ) { throw new MetaModelGenerationException( "Unable to determine collection type" ); } - else if ( Map.class.getCanonicalName().equals( returnTupeName ) ) { + else if ( MAP.equals( returnTypeName ) ) { return typeArguments.get( 1 ); } else {