From 996afafac6c495bcba5c88279f7dcdc2e4fb465a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 19 Jan 2017 21:52:51 -0800 Subject: [PATCH] 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 --- .../condition/OnClassCondition.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java index 1709b23268b..62200e4007e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java @@ -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); }