Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ClassworldsClassLoader.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.java.bytecode;
  21. import java.io.File;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.Arrays;
  25. import java.util.Collection;
  26. import org.codehaus.classworlds.ClassRealm;
  27. import org.codehaus.classworlds.ClassWorld;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. public final class ClassworldsClassLoader {
  31. private static final Logger LOG = LoggerFactory.getLogger(ClassworldsClassLoader.class);
  32. private ClassworldsClassLoader() {
  33. // only static methods
  34. }
  35. public static ClassLoader create(File bytecodeFileOrDirectory) {
  36. return create(Arrays.asList(bytecodeFileOrDirectory));
  37. }
  38. public static ClassLoader create(Collection<File> bytecodeFilesOrDirectories) {
  39. try {
  40. ClassWorld world = new ClassWorld();
  41. ClassRealm realm = world.newRealm("squid.project", null /* explicit declaration that parent should be bootstrap class loader */);
  42. for (File bytecode : bytecodeFilesOrDirectories) {
  43. URL url = getURL(bytecode);
  44. if (bytecode.isFile() && url.toString().endsWith(".class")) {
  45. LOG.info("Sonar Squid ClassLoader was expecting a JAR file instead of CLASS file : '" + bytecode.getAbsolutePath() + "'");
  46. } else {
  47. // JAR file or directory
  48. realm.addConstituent(url);
  49. }
  50. }
  51. if (LOG.isDebugEnabled()) {
  52. LOG.debug("----- Classpath analyzed by Squid:");
  53. for (URL url : realm.getConstituents()) {
  54. LOG.debug(url.toString());
  55. }
  56. LOG.debug("-----");
  57. }
  58. return realm.getClassLoader();
  59. } catch (Exception e) {
  60. throw new IllegalStateException("Can not create classloader", e);
  61. }
  62. }
  63. private static URL getURL(File file) throws MalformedURLException {
  64. URL url = file.toURI().toURL();
  65. if (file.isDirectory() && !url.toString().endsWith("/")) {
  66. /*
  67. * See ClassRealm javadoc : If the constituent is a directory, then the URL must end with a slash (/). Otherwise the constituent will
  68. * be treated as a JAR file.
  69. */
  70. url = new URL(url.toString() + "/");
  71. }
  72. return url;
  73. }
  74. }