Coverage report

| %line | %branch
--- | --- | ---

org.apache.commons.jelly.util.ClassLoaderUtils

27%

62%

1

     /*

2

      * Copyright 2002,2004 The Apache Software Foundation.

3

      *

4

      * Licensed under the Apache License, Version 2.0 (the "License");

5

      * you may not use this file except in compliance with the License.

6

      * You may obtain a copy of the License at

7

      *

8

      *      http://www.apache.org/licenses/LICENSE-2.0

9

      *

10

      * Unless required by applicable law or agreed to in writing, software

11

      * distributed under the License is distributed on an "AS IS" BASIS,

12

      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

13

      * See the License for the specific language governing permissions and

14

      * limitations under the License.

15

      */

16

17

     package org.apache.commons.jelly.util;

18

19

     import org.apache.commons.logging.Log;

20

     import org.apache.commons.logging.LogFactory;

21

22

     /**

23

      * A class to centralize the class loader management code.

24

      */

25

0

     public class ClassLoaderUtils {

26

27

         /** log for debug etc output */

28

572

         private static final Log log = LogFactory.getLog(ClassLoaderUtils.class);

29

30

         /**

31

          * Return the class loader to be used for instantiating application objects

32

          * when required.  This is determined based upon the following rules:

33

          * <ul>

34

          * <li>The specified class loader, if any</li>

35

          * <li>The thread context class loader, if it exists and <code>useContextClassLoader</code> is true</li>

36

          * <li>The class loader used to load the calling class.

37

          * <li>The System class loader.

38

          * </ul>

39

          */

40

         public static ClassLoader getClassLoader(ClassLoader specifiedLoader, boolean useContextClassLoader, Class callingClass) {

41

1781

             if (spec<span class="keyword">ifiedLoader != null) {

42

0

                 return specifiedLoader;

43

             }

44

1781

             if (useContextClassLoader) {

45

0

                 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

46

0

                 if (classLoader != null) {

47

0

                     return classLoader;

48

                 }

49

             }

50

1781

             return getClassLoader(callingClass);

51

         }

52

53

         /**

54

          * Return the class loader to be used for instantiating application objects

55

          * when a context class loader is not specified.  This is determined based upon the following rules:

56

          * <ul>

57

          * <li>The specified class loader, if any</li>

58

          * <li>The class loader used to load the calling class.

59

          * <li>The System class loader.

60

          * </ul>

61

          */

62

         public static ClassLoader getClassLoader(ClassLoader specifiedLoader, Class callingClass) {

63

0

             if (spec<span class="keyword">ifiedLoader != null) {

64

0

                 return specifiedLoader;

65

             }

66

0

             return getClassLoader(callingClass);

67

         }

68

69

         /**

70

          * Get the loader for the given class.

71

          * @param clazz the class to retrieve the loader for

72

          * @return the class loader that loaded the provided class

73

          */

74

         public static ClassLoader getClassLoader(Class clazz) {

75

2639

             ClassLoader callersLoader = clazz.getClassLoader();

76

2639

             if (callersLoader == null) {

77

0

                 callersLoader = ClassLoader.getSystemClassLoader();

78

             }

79

2639

             return callersLoader;

80

         }

81

82

         /**

83

          * Loads the given class using the current Thread's context class loader first

84

          * otherwise use the class loader which loaded this class.

85

          */

86

         public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException {

87

156

             ClassLoader loader = Thread.currentThread().getContextClassLoader();

88

156

             if (loader == null) {

89

0

                 return getClassLoader(callingClass).loadClass(className);

90

             } else {

91

156

                 return loader.loadClass(className);

92

             }

93

         }

94

95

         /**

96

          * Loads the given class using:

97

          * <ol>

98

          * <li>the specified classloader,</li>

99

          * <li>the current Thread's context class loader first, if asked</li>

100

          * <li>otherwise use the class loader which loaded this class.</li>

101

          * </ol>

102

          */

103

         public static Class loadClass(String className, ClassLoader specifiedLoader, boolean useContextLoader, Class callingClass) throws ClassNotFoundException {

104

0

             Class clazz = null;

105

0

             if (spec<span class="keyword">ifiedLoader != null) {

106

                 try {

107

0

                     clazz = specifiedLoader.loadClass(className);

108

0

                 } catch (ClassNotFoundException e) {

109

0

                     log.debug("couldn't find class in specified loader", e);

110

0

                 }

111

             }

112

0

             if (clazz == null && useContextLoader) {

113

0

                 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

114

0

                 if (contextLoader != null) {

115

                     try {

116

0

                         clazz = contextLoader.loadClass(className);

117

0

                     } catch (ClassNotFoundException e) {

118

0

                         log.debug("couldn't find class in specified loader", e);

119

0

                     }

120

                 }

121

             }

122

0

             if (clazz == null) {

123

0

                 ClassLoader loader = getClassLoader(callingClass);

124

0

                 clazz = loader.loadClass(className);

125

             }

126

0

             return clazz;

127

         }

128

     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.