PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/JavaInsight/javainsight/buildtools/JavaUtils.java

#
Java | 121 lines | 43 code | 17 blank | 61 comment | 8 complexity | a7d928fd5c30fbbc5ec580880c86fa05 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * jEdit edit mode settings:
  3. * :mode=java:tabSize=4:indentSize=4:noTabs=true:maxLineLen=0:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package javainsight.buildtools;
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.StringTokenizer;
  23. /**
  24. * Miscelleanous Java utilities.
  25. *
  26. * @author Kevin A. Burton
  27. * @version $Id: JavaUtils.java 9456 2007-04-18 20:09:54Z dmoebius $
  28. */
  29. public class JavaUtils
  30. {
  31. private static final Object mutex = new Object();
  32. private static String[] classpath = null;
  33. /**
  34. * make the constructor private.
  35. */
  36. private JavaUtils() { }
  37. /**
  38. * Get the current classpath as an array of strings.
  39. *
  40. * This implementation evaluates the contents of the following system
  41. * properties, if they exist:
  42. *
  43. * <UL>
  44. * <LI>
  45. * <CODE>java.class.path</CODE>
  46. * - should be available on all platforms.
  47. * <LI>
  48. * <CODE>sun.boot.class.path</CODE>
  49. * - should be available on JDK 1.2 or higher platforms (the IBM
  50. * JDK 1.2/1.3 defines it, too). If this property exists, it's
  51. * entries are added first in the resulting array.
  52. * </UL>
  53. *
  54. * <B>Note:</B> contrary to system classpaths, here non-existent
  55. * entries are removed from the resulting array. All entries are checked
  56. * using <CODE>java.io.File.exists()</CODE>. This is because
  57. * Jode, the decompiler library, has a bug with non-existent entries
  58. * in the classpath.
  59. *
  60. * @return an array of classpath entries
  61. */
  62. public static String[] getClasspath() {
  63. if (classpath == null) {
  64. String[] newClasspath = computeClasspath();
  65. synchronized(mutex) {
  66. classpath = newClasspath;
  67. }
  68. }
  69. return classpath;
  70. }
  71. private static String[] computeClasspath() {
  72. ArrayList<String> cp = new ArrayList<String>();
  73. String pathSep = System.getProperty("path.separator");
  74. // add entries from sun.boot.class.path:
  75. String bootpath = System.getProperty("sun.boot.class.path");
  76. if (bootpath != null) {
  77. StringTokenizer tokenizer = new StringTokenizer(bootpath, pathSep);
  78. while (tokenizer.hasMoreElements()) {
  79. String entry = tokenizer.nextElement().toString();
  80. if (new File(entry).exists())
  81. cp.add(entry);
  82. }
  83. }
  84. // add entries from java.class.path:
  85. String classpath = System.getProperty("java.class.path");
  86. StringTokenizer tokenizer = new StringTokenizer(classpath, pathSep);
  87. while (tokenizer.hasMoreElements()) {
  88. String entry = tokenizer.nextElement().toString();
  89. if (new File(entry).exists())
  90. cp.add(entry);
  91. }
  92. return cp.toArray(new String[cp.size()]);
  93. }
  94. /**
  95. * <p>Given a java class name (ie. org.apache.jetspeed.Test) return a
  96. * full filename (ie. org/apache/jetspeed/Test.java).</p>
  97. *
  98. * Substitutes all dots ('.') by the file separator char and appends
  99. * ".java" to the resulting name.
  100. */
  101. public static final String getJavaFile(String classname) {
  102. return classname.replace('.', File.separatorChar).concat(".java");
  103. }
  104. }