mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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:
parent
9650668291
commit
996afafac6
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user