| 1 | package fr.sii.ogham.core.util.classpath; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | import org.slf4j.LoggerFactory; | |
| 5 | ||
| 6 | ||
| 7 | /** | |
| 8 | * Helper for classpath management. | |
| 9 | * | |
| 10 | * @author Aurélien Baudet | |
| 11 | * | |
| 12 | */ | |
| 13 | public class SimpleClasspathHelper implements ClasspathHelper { | |
| 14 | private static final Logger LOG = LoggerFactory.getLogger(SimpleClasspathHelper.class); | |
| 15 | | |
| 16 | private ClassLoader classLoader; | |
| 17 | | |
| 18 | public SimpleClasspathHelper() { | |
| 19 | super(); | |
| 20 | classLoader = null; | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * Test if the class name is defined in the classpath. | |
| 25 | * | |
| 26 | * @param className | |
| 27 | * the class name | |
| 28 | * @return true if the class exists in the classpath, false otherwise | |
| 29 | */ | |
| 30 | public boolean exists(String className) { | |
| 31 |
1
1. exists : negated conditional → NO_COVERAGE |
if(this.classLoader!=null) { |
| 32 |
1
1. exists : negated conditional → NO_COVERAGE |
if(exists(className, this.classLoader)) { |
| 33 | LOG.debug("class {} found using class specific class loader", className); | |
| 34 |
1
1. exists : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return true; |
| 35 | } | |
| 36 |
1
1. exists : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return false; |
| 37 | } | |
| 38 |
1
1. exists : negated conditional → NO_COVERAGE |
if(existsWithDefaultClassLoader(className)) { |
| 39 | LOG.debug("class {} found using default class loader", className); | |
| 40 |
1
1. exists : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return true; |
| 41 | } | |
| 42 |
1
1. exists : negated conditional → NO_COVERAGE |
if(exists(className, Thread.currentThread().getContextClassLoader())) { |
| 43 | LOG.debug("class {} found using class loader of current thread", className); | |
| 44 |
1
1. exists : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return true; |
| 45 | } | |
| 46 |
1
1. exists : negated conditional → NO_COVERAGE |
if(exists(className, getClass().getClassLoader())) { |
| 47 | LOG.debug("class {} found using class loader of current class", className); | |
| 48 |
1
1. exists : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return true; |
| 49 | } | |
| 50 |
1
1. exists : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return false; |
| 51 | } | |
| 52 | ||
| 53 | private static boolean existsWithDefaultClassLoader(String className) { | |
| 54 | try { | |
| 55 | Class.forName(className); | |
| 56 |
1
1. existsWithDefaultClassLoader : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::existsWithDefaultClassLoader → NO_COVERAGE |
return true; |
| 57 | } catch (ClassNotFoundException e) { | |
| 58 | LOG.debug("Class {} not found", className); | |
| 59 | LOG.trace("Cause:", e); | |
| 60 |
1
1. existsWithDefaultClassLoader : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::existsWithDefaultClassLoader → NO_COVERAGE |
return false; |
| 61 | } catch (NoClassDefFoundError e) { | |
| 62 | LOG.debug("Class {} can't be loaded", className); | |
| 63 | LOG.trace("Cause:", e); | |
| 64 |
2
1. existsWithDefaultClassLoader : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::existsWithDefaultClassLoader → NO_COVERAGE 2. existsWithDefaultClassLoader : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::existsWithDefaultClassLoader → NO_COVERAGE |
return isForSameClass(className, e); |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | private static boolean exists(String className, ClassLoader classLoader) { | |
| 69 | try { | |
| 70 | Class.forName(className, false, classLoader); | |
| 71 |
1
1. exists : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return true; |
| 72 | } catch (ClassNotFoundException e) { | |
| 73 | LOG.debug("Class {} not found", className); | |
| 74 | LOG.trace("Cause:", e); | |
| 75 |
1
1. exists : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return false; |
| 76 | } catch (NoClassDefFoundError e) { | |
| 77 | LOG.debug("Class {} can't be loaded", className); | |
| 78 | LOG.trace("Cause:", e); | |
| 79 |
2
1. exists : negated conditional → NO_COVERAGE 2. exists : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::exists → NO_COVERAGE |
return !isForSameClass(className, e); |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | private static boolean isForSameClass(String className, NoClassDefFoundError e) { | |
| 84 |
2
1. isForSameClass : replaced boolean return with false for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::isForSameClass → NO_COVERAGE 2. isForSameClass : replaced boolean return with true for fr/sii/ogham/core/util/classpath/SimpleClasspathHelper::isForSameClass → NO_COVERAGE |
return className.equals(e.getMessage().replace("/", ".")); |
| 85 | } | |
| 86 | ||
| 87 | public void setClassLoader(ClassLoader classLoader) { | |
| 88 | this.classLoader = classLoader; | |
| 89 | } | |
| 90 | ||
| 91 | public void resetClassLoader() { | |
| 92 | this.classLoader = null; | |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 31 |
1.1 |
|
| 32 |
1.1 |
|
| 34 |
1.1 |
|
| 36 |
1.1 |
|
| 38 |
1.1 |
|
| 40 |
1.1 |
|
| 42 |
1.1 |
|
| 44 |
1.1 |
|
| 46 |
1.1 |
|
| 48 |
1.1 |
|
| 50 |
1.1 |
|
| 56 |
1.1 |
|
| 60 |
1.1 |
|
| 64 |
1.1 2.2 |
|
| 71 |
1.1 |
|
| 75 |
1.1 |
|
| 79 |
1.1 2.2 |
|
| 84 |
1.1 2.2 |