SystemClassLoader.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 2005, 2006 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.gcj.runtime;
  7. import java.io.*;
  8. import java.lang.reflect.Field;
  9. import java.util.StringTokenizer;
  10. import java.util.HashMap;
  11. import java.net.URL;
  12. import java.net.URLClassLoader;
  13. public final class SystemClassLoader extends URLClassLoader
  14. {
  15. SystemClassLoader(ClassLoader parent)
  16. {
  17. super(new URL[0], parent);
  18. }
  19. // This holds all the "native" classes linked into the executable
  20. // and registered with this loader.
  21. private HashMap nativeClasses = new HashMap();
  22. // This is called to register a native class which was linked into
  23. // the application but which is registered with the system class
  24. // loader after the VM is initialized.
  25. void addClass(Class klass)
  26. {
  27. String packageName = null;
  28. String className = klass.getName();
  29. int lastDot = className.lastIndexOf('.');
  30. if (lastDot != -1)
  31. packageName = className.substring(0, lastDot);
  32. if (packageName != null && getPackage(packageName) == null)
  33. {
  34. // Should have some way to store this information in a
  35. // precompiled manifest.
  36. definePackage(packageName, null, null, null, null, null, null, null);
  37. }
  38. // Use reflection to access the package-private "loadedClasses" field.
  39. nativeClasses.put(className, klass);
  40. }
  41. protected native Class findClass(String name);
  42. // We add the URLs to the system class loader late. The reason for
  43. // this is that during bootstrap we don't want to parse URLs or
  44. // create URL connections, since that will result in circularities
  45. // causing a crash.
  46. void init()
  47. {
  48. String sep = File.pathSeparator;
  49. StringTokenizer st
  50. = new StringTokenizer (System.getProperty ("java.class.path", "."),
  51. sep, true);
  52. // Pretend we start with a ':', so if we see a ':' first we add
  53. // '.'.
  54. boolean last_was_sep = true;
  55. while (st.hasMoreElements ())
  56. {
  57. String e = st.nextToken ();
  58. try
  59. {
  60. if (sep.equals(e))
  61. {
  62. if (last_was_sep)
  63. {
  64. // We saw two separators in a row, so add ".".
  65. addURL(new URL("file", "", -1, "./"));
  66. last_was_sep = false;
  67. }
  68. else
  69. last_was_sep = true;
  70. continue;
  71. }
  72. last_was_sep = false;
  73. File path = new File(e);
  74. // Ignore invalid paths.
  75. if (!path.exists())
  76. continue;
  77. if (!e.endsWith (File.separator) && path.isDirectory ())
  78. addURL(new URL("file", "", -1, e + File.separator));
  79. else
  80. addURL(new URL("file", "", -1, e));
  81. }
  82. catch (java.net.MalformedURLException x)
  83. {
  84. // This should never happen.
  85. throw new RuntimeException(x);
  86. }
  87. }
  88. // If we saw a trailing ":", add "." to the path.
  89. if (last_was_sep)
  90. {
  91. try
  92. {
  93. addURL(new URL("file", "", -1, "./"));
  94. }
  95. catch (java.net.MalformedURLException x)
  96. {
  97. // This should never happen.
  98. throw new RuntimeException(x);
  99. }
  100. }
  101. }
  102. }