Skip to content

Commit

Permalink
Move all class name constants of the hibernate-orm extension to a sin…
Browse files Browse the repository at this point in the history
…gle class

So that we can test them easily.
  • Loading branch information
yrodiere authored and gsmet committed Apr 8, 2021
1 parent 3d31edd commit c750d3b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.hibernate.orm.deployment;

import java.util.HashSet;
import java.util.Set;

import org.jboss.jandex.DotName;

public class ClassNames {

static final Set<DotName> CREATED_CONSTANTS = new HashSet<>();

private ClassNames() {
}

private static DotName createConstant(String fqcn) {
DotName result = DotName.createSimple(fqcn);
CREATED_CONSTANTS.add(result);
return result;
}

public static final DotName ENUM = createConstant("java.lang.Enum");

public static final DotName TENANT_CONNECTION_RESOLVER = createConstant(
"io.quarkus.hibernate.orm.runtime.tenant.TenantConnectionResolver");
public static final DotName TENANT_RESOLVER = createConstant("io.quarkus.hibernate.orm.runtime.tenant.TenantResolver");

public static final DotName STATIC_METAMODEL = createConstant("javax.persistence.metamodel.StaticMetamodel");

public static final DotName QUARKUS_PERSISTENCE_UNIT = createConstant("io.quarkus.hibernate.orm.PersistenceUnit");
public static final DotName QUARKUS_PERSISTENCE_UNIT_REPEATABLE_CONTAINER = createConstant(
"io.quarkus.hibernate.orm.PersistenceUnit$List");
public static final DotName JPA_PERSISTENCE_UNIT = createConstant("javax.persistence.PersistenceUnit");
public static final DotName JPA_PERSISTENCE_CONTEXT = createConstant("javax.persistence.PersistenceContext");

public static final DotName JPA_ENTITY = createConstant("javax.persistence.Entity");
public static final DotName MAPPED_SUPERCLASS = createConstant("javax.persistence.MappedSuperclass");
public static final DotName EMBEDDABLE = createConstant("javax.persistence.Embeddable");
public static final DotName EMBEDDED = createConstant("javax.persistence.Embedded");
public static final DotName ELEMENT_COLLECTION = createConstant("javax.persistence.ElementCollection");
public static final DotName PROXY = createConstant("org.hibernate.annotations.Proxy");
public static final DotName HIBERNATE_PROXY = createConstant("org.hibernate.proxy.HibernateProxy");
public static final DotName TYPE = createConstant("org.hibernate.annotations.Type");
public static final DotName TYPE_DEFINITION = createConstant("org.hibernate.annotations.TypeDef");
public static final DotName TYPE_DEFINITIONS = createConstant("org.hibernate.annotations.TypeDefs");

public static final DotName ENTITY_MANAGER_FACTORY = createConstant("javax.persistence.EntityManagerFactory");
public static final DotName SESSION_FACTORY = createConstant("org.hibernate.SessionFactory");
public static final DotName ENTITY_MANAGER = createConstant("javax.persistence.EntityManager");
public static final DotName SESSION = createConstant("org.hibernate.Session");

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,9 @@

public class HibernateOrmCdiProcessor {

public static final List<DotName> SESSION_FACTORY_EXPOSED_TYPES = Arrays.asList(
DotName.createSimple("javax.persistence.EntityManagerFactory"),
DotName.createSimple("org.hibernate.SessionFactory"));
public static final List<DotName> SESSION_EXPOSED_TYPES = Arrays.asList(
DotName.createSimple("javax.persistence.EntityManager"),
DotName.createSimple("org.hibernate.Session"));

public static final DotName PERSISTENCE_UNIT_QUALIFIER = DotName.createSimple("io.quarkus.hibernate.orm.PersistenceUnit");

public static final DotName JPA_PERSISTENCE_UNIT = DotName.createSimple("javax.persistence.PersistenceUnit");

public static final DotName JPA_PERSISTENCE_CONTEXT = DotName
.createSimple("javax.persistence.PersistenceContext");
private static final List<DotName> SESSION_FACTORY_EXPOSED_TYPES = Arrays.asList(ClassNames.ENTITY_MANAGER_FACTORY,
ClassNames.SESSION_FACTORY);
private static final List<DotName> SESSION_EXPOSED_TYPES = Arrays.asList(ClassNames.ENTITY_MANAGER, ClassNames.SESSION);

@BuildStep
AnnotationsTransformerBuildItem convertJpaResourceAnnotationsToQualifier(
Expand All @@ -69,10 +59,10 @@ public void transform(TransformationContext transformationContext) {
}

DotName jpaAnnotation;
if (field.hasAnnotation(JPA_PERSISTENCE_UNIT)) {
jpaAnnotation = JPA_PERSISTENCE_UNIT;
} else if (field.hasAnnotation(JPA_PERSISTENCE_CONTEXT)) {
jpaAnnotation = JPA_PERSISTENCE_CONTEXT;
if (field.hasAnnotation(ClassNames.JPA_PERSISTENCE_UNIT)) {
jpaAnnotation = ClassNames.JPA_PERSISTENCE_UNIT;
} else if (field.hasAnnotation(ClassNames.JPA_PERSISTENCE_CONTEXT)) {
jpaAnnotation = ClassNames.JPA_PERSISTENCE_CONTEXT;
} else {
return;
}
Expand All @@ -91,7 +81,7 @@ public void transform(TransformationContext transformationContext) {
// in this case, we consider it the default too if the name matches
transformation.add(DotNames.DEFAULT);
} else {
transformation.add(PERSISTENCE_UNIT_QUALIFIER,
transformation.add(ClassNames.QUARKUS_PERSISTENCE_UNIT,
AnnotationValue.createStringValue("value", persistenceUnitNameAnnotationValue.asString()));
}
transformation.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,7 @@ public final class HibernateOrmProcessor {

private static final Logger LOG = Logger.getLogger(HibernateOrmProcessor.class);

public static final DotName TENANT_CONNECTION_RESOLVER = DotName
.createSimple("io.quarkus.hibernate.orm.runtime.tenant.TenantConnectionResolver");
public static final DotName TENANT_RESOLVER = DotName
.createSimple("io.quarkus.hibernate.orm.runtime.tenant.TenantResolver");

public static final DotName STATIC_METAMODEL = DotName.createSimple("javax.persistence.metamodel.StaticMetamodel");
public static final DotName PERSISTENCE_UNIT = DotName.createSimple("io.quarkus.hibernate.orm.PersistenceUnit");
public static final DotName PERSISTENCE_UNIT_REPEATABLE_CONTAINER = DotName
.createSimple("io.quarkus.hibernate.orm.PersistenceUnit$List");
public static final DotName JPA_ENTITY = DotName.createSimple("javax.persistence.Entity");
public static final DotName MAPPED_SUPERCLASS = DotName.createSimple("javax.persistence.MappedSuperclass");
public static final DotName PROXY = DotName.createSimple("org.hibernate.annotations.Proxy");

private static final String INTEGRATOR_SERVICE_FILE = "META-INF/services/org.hibernate.integrator.spi.Integrator";
public static final String HIBERNATE_PROXY_INTERFACENAME = "org.hibernate.proxy.HibernateProxy";

@BuildStep
CapabilityBuildItem capability() {
Expand Down Expand Up @@ -188,7 +174,7 @@ void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrm

@BuildStep
AdditionalIndexedClassesBuildItem addPersistenceUnitAnnotationToIndex() {
return new AdditionalIndexedClassesBuildItem(PERSISTENCE_UNIT.toString());
return new AdditionalIndexedClassesBuildItem(ClassNames.QUARKUS_PERSISTENCE_UNIT.toString());
}

// We do our own enhancement during the compilation phase, so disable any
Expand Down Expand Up @@ -594,8 +580,9 @@ public void multitenancy(HibernateOrmRecorder recorder,
}

if (multitenancyEnabled) {
unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(TENANT_CONNECTION_RESOLVER)));
unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(TENANT_RESOLVER)));
unremovableBeans
.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(ClassNames.TENANT_CONNECTION_RESOLVER)));
unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(ClassNames.TENANT_RESOLVER)));
}
}

Expand All @@ -610,7 +597,7 @@ public void produceLoggingCategories(HibernateOrmConfig hibernateOrmConfig,
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
public void registerStaticMetamodelClassesForReflection(CombinedIndexBuildItem index,
BuildProducer<ReflectiveClassBuildItem> reflective) {
Collection<AnnotationInstance> annotationInstances = index.getIndex().getAnnotations(STATIC_METAMODEL);
Collection<AnnotationInstance> annotationInstances = index.getIndex().getAnnotations(ClassNames.STATIC_METAMODEL);
if (!annotationInstances.isEmpty()) {

String[] metamodel = annotationInstances.stream()
Expand Down Expand Up @@ -1036,8 +1023,8 @@ private static Map<String, Set<String>> getModelClassesAndPackagesPerPersistence
Set<String> relatedModelClassNames = getRelatedModelClassNames(index, jpaEntities.getAllModelClassNames(),
modelClassInfo);

if (modelClassInfo != null && (modelClassInfo.classAnnotation(PERSISTENCE_UNIT) != null
|| modelClassInfo.classAnnotation(PERSISTENCE_UNIT_REPEATABLE_CONTAINER) != null)) {
if (modelClassInfo != null && (modelClassInfo.classAnnotation(ClassNames.QUARKUS_PERSISTENCE_UNIT) != null
|| modelClassInfo.classAnnotation(ClassNames.QUARKUS_PERSISTENCE_UNIT_REPEATABLE_CONTAINER) != null)) {
modelClassesWithPersistenceUnitAnnotations.add(modelClassInfo.name().toString());
}

Expand Down Expand Up @@ -1097,8 +1084,8 @@ private static Set<String> getRelatedModelClassNames(IndexView index, Set<String
Set<String> relatedModelClassNames = new HashSet<>();

// for now we only deal with entities and mapped super classes
if (modelClassInfo.classAnnotation(JPA_ENTITY) == null &&
modelClassInfo.classAnnotation(MAPPED_SUPERCLASS) == null) {
if (modelClassInfo.classAnnotation(ClassNames.JPA_ENTITY) == null &&
modelClassInfo.classAnnotation(ClassNames.MAPPED_SUPERCLASS) == null) {
return Collections.emptySet();
}

Expand Down Expand Up @@ -1137,7 +1124,8 @@ private static boolean hasPackagesInQuarkusConfig(HibernateOrmConfig hibernateOr
}

private static Collection<AnnotationInstance> getPackageLevelPersistenceUnitAnnotations(IndexView index) {
Collection<AnnotationInstance> persistenceUnitAnnotations = index.getAnnotationsWithRepeatable(PERSISTENCE_UNIT, index);
Collection<AnnotationInstance> persistenceUnitAnnotations = index
.getAnnotationsWithRepeatable(ClassNames.QUARKUS_PERSISTENCE_UNIT, index);
Collection<AnnotationInstance> packageLevelPersistenceUnitAnnotations = new ArrayList<>();

for (AnnotationInstance persistenceUnitAnnotation : persistenceUnitAnnotations) {
Expand Down Expand Up @@ -1220,7 +1208,7 @@ private PreGeneratedProxies generatedProxies(Set<String> managedClassAndPackageN
//create a map of entity to proxy type
PreGeneratedProxies preGeneratedProxies = new PreGeneratedProxies();
Map<String, String> proxyAnnotations = new HashMap<>();
for (AnnotationInstance i : combinedIndex.getAnnotations(PROXY)) {
for (AnnotationInstance i : combinedIndex.getAnnotations(ClassNames.PROXY)) {
AnnotationValue proxyClass = i.value("proxyClass");
if (proxyClass == null) {
continue;
Expand All @@ -1235,7 +1223,7 @@ private PreGeneratedProxies generatedProxies(Set<String> managedClassAndPackageN
result = proxyCache.cache.get(managedClassOrPackageName);
} else {
Set<String> proxyInterfaceNames = new TreeSet<>();
proxyInterfaceNames.add(HIBERNATE_PROXY_INTERFACENAME); //always added
proxyInterfaceNames.add(ClassNames.HIBERNATE_PROXY.toString()); //always added
String proxy = proxyAnnotations.get(managedClassOrPackageName);
if (proxy == null) {
if (!proxyHelper.isProxiable(managedClassOrPackageName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@
public final class HibernateUserTypeProcessor {
private static final String TYPE_VALUE = "type";
private static final String TYPE_CLASS_VALUE = "typeClass";
public static final DotName TYPE = DotName.createSimple("org.hibernate.annotations.Type");
public static final DotName TYPE_DEFINITION = DotName.createSimple("org.hibernate.annotations.TypeDef");
public static final DotName TYPE_DEFINITIONS = DotName.createSimple("org.hibernate.annotations.TypeDefs");

@BuildStep
public void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem combinedIndexBuildItem) {
IndexView index = combinedIndexBuildItem.getIndex();

final Set<String> userTypes = new HashSet<>();

Collection<AnnotationInstance> typeAnnotationInstances = index.getAnnotations(TYPE);
Collection<AnnotationInstance> typeDefinitionAnnotationInstances = index.getAnnotations(TYPE_DEFINITION);
Collection<AnnotationInstance> typeDefinitionsAnnotationInstances = index.getAnnotations(TYPE_DEFINITIONS);
Collection<AnnotationInstance> typeAnnotationInstances = index.getAnnotations(ClassNames.TYPE);
Collection<AnnotationInstance> typeDefinitionAnnotationInstances = index.getAnnotations(ClassNames.TYPE_DEFINITION);
Collection<AnnotationInstance> typeDefinitionsAnnotationInstances = index.getAnnotations(ClassNames.TYPE_DEFINITIONS);

userTypes.addAll(getUserTypes(typeDefinitionAnnotationInstances));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@
*/
public final class JpaJandexScavenger {

public static final DotName JPA_ENTITY = DotName.createSimple("javax.persistence.Entity");
public static final DotName EMBEDDABLE = DotName.createSimple("javax.persistence.Embeddable");
public static final List<DotName> EMBEDDED_ANNOTATIONS = Arrays.asList(
DotName.createSimple("javax.persistence.Embedded"),
DotName.createSimple("javax.persistence.ElementCollection"));
public static final DotName MAPPED_SUPERCLASS = DotName.createSimple("javax.persistence.MappedSuperclass");

public static final DotName ENUM = DotName.createSimple("java.lang.Enum");
public static final List<DotName> EMBEDDED_ANNOTATIONS = Arrays.asList(ClassNames.EMBEDDED, ClassNames.ELEMENT_COLLECTION);

private final List<PersistenceXmlDescriptorBuildItem> explicitDescriptors;
private final BuildProducer<ReflectiveClassBuildItem> reflectiveClass;
Expand Down Expand Up @@ -72,11 +65,12 @@ public JpaEntitiesBuildItem discoverModelAndRegisterForReflection() throws IOExc
for (DotName packageAnnotation : HibernateOrmAnnotations.PACKAGE_ANNOTATIONS) {
enlistJPAModelAnnotatedPackages(indexView, domainObjectCollector, packageAnnotation);
}
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, JPA_ENTITY,
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, ClassNames.JPA_ENTITY,
unindexedClasses);
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, EMBEDDABLE,
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, ClassNames.EMBEDDABLE,
unindexedClasses);
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, MAPPED_SUPERCLASS,
enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector,
ClassNames.MAPPED_SUPERCLASS,
unindexedClasses);
enlistEmbeddedsAndElementCollections(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector,
unindexedClasses);
Expand Down Expand Up @@ -241,7 +235,7 @@ private static void addClassHierarchyToReflectiveList(IndexView index, JpaEntiti
for (FieldInfo fieldInfo : classInfo.fields()) {
DotName fieldType = fieldInfo.type().name();
ClassInfo fieldTypeClassInfo = index.getClassByName(fieldType);
if (fieldTypeClassInfo != null && ENUM.equals(fieldTypeClassInfo.superName())) {
if (fieldTypeClassInfo != null && ClassNames.ENUM.equals(fieldTypeClassInfo.superName())) {
enumTypeCollector.add(fieldType.toString());
}
}
Expand All @@ -268,7 +262,7 @@ private static void collectPackage(JpaEntitiesBuildItem domainObjectCollector, C
}

private static void collectDomainObject(JpaEntitiesBuildItem domainObjectCollector, ClassInfo modelClass) {
if (modelClass.classAnnotation(JPA_ENTITY) != null) {
if (modelClass.classAnnotation(ClassNames.JPA_ENTITY) != null) {
domainObjectCollector.addEntityClass(modelClass.name().toString());
} else {
domainObjectCollector.addModelClass(modelClass.name().toString());
Expand Down
Loading

0 comments on commit c750d3b

Please sign in to comment.