You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoaderUtils.java 4.5 kB

11 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. import java.io.File;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.launch.Locator;
  22. // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
  23. /**
  24. * ClassLoader utility methods
  25. *
  26. */
  27. public class LoaderUtils {
  28. /** Utilities used for file operations */
  29. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  30. /**
  31. * Set the context classloader
  32. *
  33. * @param loader the ClassLoader to be used as the context class loader
  34. * on the current thread.
  35. */
  36. public static void setContextClassLoader(ClassLoader loader) {
  37. Thread currentThread = Thread.currentThread();
  38. currentThread.setContextClassLoader(loader);
  39. }
  40. /**
  41. * JDK1.1 compatible access to set the context class loader.
  42. *
  43. * @return the ClassLoader instance being used as the context
  44. * classloader on the current thread. Returns null on JDK 1.1
  45. */
  46. public static ClassLoader getContextClassLoader() {
  47. Thread currentThread = Thread.currentThread();
  48. return currentThread.getContextClassLoader();
  49. }
  50. /**
  51. * Indicates if the context class loader methods are available
  52. *
  53. * @return true if the get and set methods dealing with the context
  54. * classloader are available.
  55. */
  56. public static boolean isContextLoaderAvailable() {
  57. return true;
  58. }
  59. /**
  60. * Normalize a source location
  61. *
  62. * @param source the source location to be normalized.
  63. *
  64. * @return the normalized source location.
  65. */
  66. private static File normalizeSource(File source) {
  67. if (source != null) {
  68. try {
  69. source = FILE_UTILS.normalize(source.getAbsolutePath());
  70. } catch (BuildException e) {
  71. // relative path
  72. }
  73. }
  74. return source;
  75. }
  76. /**
  77. * Find the directory or jar file the class has been loaded from.
  78. *
  79. * @param c the class whose location is required.
  80. * @return the file or jar with the class or null if we cannot
  81. * determine the location.
  82. *
  83. * @since Ant 1.6
  84. */
  85. public static File getClassSource(Class<?> c) {
  86. return normalizeSource(Locator.getClassSource(c));
  87. }
  88. /**
  89. * Find the directory or a give resource has been loaded from.
  90. *
  91. * @param c the classloader to be consulted for the source
  92. * @param resource the resource whose location is required.
  93. *
  94. * @return the file with the resource source or null if
  95. * we cannot determine the location.
  96. *
  97. * @since Ant 1.6
  98. */
  99. public static File getResourceSource(ClassLoader c, String resource) {
  100. if (c == null) {
  101. c = LoaderUtils.class.getClassLoader();
  102. }
  103. return normalizeSource(Locator.getResourceSource(c, resource));
  104. }
  105. /**
  106. * Return the resource name of a class name.
  107. * @param className the name of the class to convert.
  108. * @return the corresponding resource name.
  109. * @since Ant 1.7.0.
  110. */
  111. public static String classNameToResource(String className) {
  112. return className.replace('.', '/') + ".class";
  113. }
  114. /**
  115. * Check if a classloader has a classname resource.
  116. * @param loader the classloader to look it.
  117. * @param className the name of the class to look for.
  118. * @return true if the class exists, false otherwise
  119. * @since Ant 1.7.0.
  120. */
  121. public static boolean classExists(ClassLoader loader, String className) {
  122. return loader.getResource(classNameToResource(className)) != null;
  123. }
  124. }