Optimize OnClassCondition isPresent check

Update `OnClassCondition` to use its own `isPresent` rather than using
`ClassUtils.isPresent`. Using our own implementation saves a few cycles
since we never need to check for native types, and we don't support
nested class references specified in the non `$` notation.

See gh-7573
This commit is contained in:
Phillip Webb 2017-01-19 21:52:51 -08:00
parent 9650668291
commit 996afafac6

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -111,7 +111,7 @@ class OnClassCondition extends SpringBootCondition {
@Override
public boolean matches(String className, ConditionContext context) {
return ClassUtils.isPresent(className, context.getClassLoader());
return isPresent(className, context.getClassLoader());
}
},
@ -120,11 +120,32 @@ class OnClassCondition extends SpringBootCondition {
@Override
public boolean matches(String className, ConditionContext context) {
return !ClassUtils.isPresent(className, context.getClassLoader());
return !isPresent(className, context.getClassLoader());
}
};
private static boolean isPresent(String className, ClassLoader classLoader) {
if (classLoader == null) {
classLoader = ClassUtils.getDefaultClassLoader();
}
try {
forName(className, classLoader);
return true;
}
catch (Throwable ex) {
return false;
}
}
private static Class<?> forName(String className, ClassLoader classLoader)
throws ClassNotFoundException {
if (classLoader != null) {
return classLoader.loadClass(className);
}
return Class.forName(className);
}
public abstract boolean matches(String className, ConditionContext context);
}