split out an inner class

This commit is contained in:
Gavin King 2024-02-24 11:43:02 +01:00
parent 9aa2f631f1
commit 7abe8f5f2b
3 changed files with 60 additions and 44 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Boolean, Element> {
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);
}
}

View File

@ -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<Boolean, Element> {
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 );
}
}
}

View File

@ -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 {