Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Avoid caching. Expand exception handling to all reflective exceptions. Fix since tags.

See: #2721
Original pull request: #2743.
  • Loading branch information
mp911de committed Dec 12, 2022
1 parent 016e8c2 commit f8d6c9f
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/main/java/org/springframework/data/util/QTypeContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package org.springframework.data.util;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aot.generate.GenerationContext;
Expand All @@ -29,23 +25,25 @@

/**
* @author Christoph Strobl
* @since 4.1
* @since 3.0.1
*/
public class QTypeContributor {

private final static Log logger = LogFactory.getLog(QTypeContributor.class);
private static Function<ClassLoader, Class<?>> entityPathType = cacheOf(QTypeContributor::getEntityPathType);

public static void contributeEntityPath(Class<?> type, GenerationContext context, @Nullable ClassLoader classLoader) {

Class<?> entityPathType = QTypeContributor.entityPathType.apply(classLoader);
if (entityPathType == null) {
return;
}
try {

Class<?> entityPathType = getEntityPathType(classLoader);

if (entityPathType == null) {
return;
}

String queryClassName = getQueryClassName(type);
if (ClassUtils.isPresent(queryClassName, classLoader)) {

String queryClassName = getQueryClassName(type);
if (ClassUtils.isPresent(queryClassName, classLoader)) {
try {
if (ClassUtils.isAssignable(entityPathType, ClassUtils.forName(queryClassName, classLoader))) {

logger.debug("Registering Q type %s for %s.");
Expand All @@ -55,33 +53,26 @@ public static void contributeEntityPath(Class<?> type, GenerationContext context
} else {
logger.debug("Skipping Q type %s. Not an EntityPath.");
}
} catch (ClassNotFoundException e) {
throw new IllegalStateException("%s could not be loaded".formatted(queryClassName), e);
}
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot contribute Q class for domain type %s".formatted(type.getName()), e);
}
}

@Nullable
private static Class<?> getEntityPathType(ClassLoader classLoader) {
private static Class<?> getEntityPathType(@Nullable ClassLoader classLoader) throws ClassNotFoundException {

if (!ClassUtils.isPresent("com.querydsl.core.types.EntityPath", classLoader)) {
String entityPathClassName = "com.querydsl.core.types.EntityPath";
if (!ClassUtils.isPresent(entityPathClassName, classLoader)) {
return null;
}

try {
return ClassUtils.forName("com.querydsl.core.types.EntityPath", classLoader);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

private static Function<ClassLoader, Class<?>> cacheOf(Function<ClassLoader, Class<?>> inFunction) {
Map<ClassLoader, Class<?>> cache = new WeakHashMap<>();
return in -> cache.computeIfAbsent(in, inFunction::apply);
return ClassUtils.forName(entityPathClassName, classLoader);
}

/**
* Returns the name of the query class for the given domain class.
* Returns the name of the query class for the given domain class following {@code SimpleEntityPathResolver}
* conventions.
*
* @param domainClass
* @return
Expand Down

0 comments on commit f8d6c9f

Please sign in to comment.