PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms 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
  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
 20package javainsight.buildtools;
 21
 22
 23import java.io.File;
 24import java.util.ArrayList;
 25import java.util.StringTokenizer;
 26
 27
 28/**
 29 * Miscelleanous Java utilities.
 30 *
 31 * @author Kevin A. Burton
 32 * @version $Id: JavaUtils.java 9456 2007-04-18 20:09:54Z dmoebius $
 33 */
 34public class JavaUtils
 35{
 36    private static final Object mutex = new Object();
 37
 38    private static String[] classpath = null;
 39
 40    /**
 41     * make the constructor private.
 42     */
 43    private JavaUtils() { }
 44
 45
 46     /**
 47      * Get the current classpath as an array of strings.
 48      *
 49      * This implementation evaluates the contents of the following system
 50      * properties, if they exist:
 51      *
 52      * <UL>
 53      *   <LI>
 54      *     <CODE>java.class.path</CODE>
 55      *     - should be available on all platforms.
 56      *   <LI>
 57      *     <CODE>sun.boot.class.path</CODE>
 58      *     - should be available on JDK 1.2 or higher platforms (the IBM
 59      *     JDK 1.2/1.3 defines it, too). If this property exists, it's
 60      *     entries are added first in the resulting array.
 61      * </UL>
 62      *
 63      * <B>Note:</B> contrary to system classpaths, here non-existent
 64      * entries are removed from the resulting array. All entries are checked
 65      * using <CODE>java.io.File.exists()</CODE>. This is because
 66      * Jode, the decompiler library, has a bug with non-existent entries
 67      * in the classpath.
 68      *
 69      * @return an array of classpath entries
 70      */
 71    public static String[] getClasspath() {
 72        if (classpath == null) {
 73            String[] newClasspath = computeClasspath();
 74            synchronized(mutex) {
 75                classpath = newClasspath;
 76            }
 77        }
 78        return classpath;
 79    }
 80    
 81    
 82    private static String[] computeClasspath() {
 83        ArrayList<String> cp = new ArrayList<String>();
 84        String pathSep = System.getProperty("path.separator");
 85
 86        // add entries from sun.boot.class.path:
 87        String bootpath = System.getProperty("sun.boot.class.path");
 88        if (bootpath != null) {
 89            StringTokenizer tokenizer = new StringTokenizer(bootpath, pathSep);
 90            while (tokenizer.hasMoreElements()) {
 91                String entry = tokenizer.nextElement().toString();
 92                if (new File(entry).exists())
 93                    cp.add(entry);
 94            }
 95        }
 96
 97        // add entries from java.class.path:
 98        String classpath = System.getProperty("java.class.path");
 99        StringTokenizer tokenizer = new StringTokenizer(classpath, pathSep);
100        while (tokenizer.hasMoreElements()) {
101            String entry = tokenizer.nextElement().toString();
102            if (new File(entry).exists())
103                cp.add(entry);
104        }
105
106        return cp.toArray(new String[cp.size()]);
107    }
108
109
110    /**
111     * <p>Given a java class name (ie. org.apache.jetspeed.Test) return a
112     * full filename (ie. org/apache/jetspeed/Test.java).</p>
113     *
114     * Substitutes all dots ('.') by the file separator char and appends
115     * ".java" to the resulting name.
116     */
117    public static final String getJavaFile(String classname) {
118        return classname.replace('.', File.separatorChar).concat(".java");
119    }
120
121}