Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / util / CompileServer.java
1 /*
2  *    CompileServer.java
3  *    Copyright (C) 2001 Javier Lopez (jslopez@alum.mit.edu)
4  *
5  *    $Revision: 1.7 $
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 package jde.util;
23
24 import java.util.StringTokenizer;
25 import java.lang.reflect.Method;
26 import java.lang.reflect.InvocationTargetException;
27
28 /**
29  * CompileServer.java provides an interface between 
30  * the command line(or from Emacs) and the javac compiler.
31  * @see com.sun.tools.javac.main
32  * Calling the compile server instead of doing javac will 
33  * avoid the start up time that occur with every incovation
34  * of javac.exe
35  *
36  * Created: Sun Aug 12 21:56:50 2001
37  *
38  * @author <a href="mailto:jslopez@alum.mit.edu"></a>
39  * @version 1.0
40  * @since jde-2.2.8beta5
41  */
42 public class CompileServer {
43     private static Class compiler;
44     static {
45         try {
46             compiler = Class.forName("com.sun.tools.javac.Main");
47         } catch (ClassNotFoundException e) {
48             e.printStackTrace();
49         }
50     }
51     
52     private static Method compile;
53     static {
54         try {
55             if (compiler != null) {
56                 Class[] params = new Class[] {String[].class};
57                 compile = compiler.getMethod("compile", params);
58             }
59         } catch (NoSuchMethodException e) {
60             e.printStackTrace();
61         } catch (SecurityException e) {
62             e.printStackTrace();
63         }
64     }
65     
66     /**
67      *
68      * @param args a <code>String[]</code> with
69      * the arguments to passed to compiler.
70      * @see com.sun.tools.javac.Main#compiler(String[] args)
71      */
72     public static void compile(String[] args) {
73         try {
74             if (compile != null) {
75                 Object[] arguments = new Object[] {args};
76                 System.out.println(compile.invoke(compiler.newInstance(), arguments));
77             }
78         } catch (InstantiationException e) {
79             e.printStackTrace();
80         } catch (InvocationTargetException e) {
81             e.printStackTrace();
82         } catch (IllegalAccessException e) {
83             e.printStackTrace();
84         } catch (IllegalArgumentException e) {
85             e.printStackTrace();
86         }
87     }
88
89     /**
90      *
91      * @param commands a <code>String[]</code> with
92      * the arguments to passed to compiler.
93      * @see com.sun.tools.javac.Main#compiler(String[] args)
94      */
95     public static void compile(String commands) {
96         //Parsing commands
97         StringTokenizer st = new StringTokenizer(commands);
98         String[] args = new String[st.countTokens()];
99         
100         for (int i = 0; st.hasMoreTokens(); i++) { //Fetching the array 
101             args[i] = st.nextToken();
102         }
103         compile(args); 
104     }
105
106     public static void main (String[] args) { 
107         CompileServer.compile("CompileServer.java");
108     }
109 }// CompileServer
110
111 /*
112  * $Log: CompileServer.java,v $
113  * Revision 1.7  2001/11/21 15:45:34  jslopez
114  * Fixes causing a compilation error when using
115  * jdk1.3
116  *
117  * Revision 1.6  2001/11/21 07:07:00  paulk
118  * Fixed typo
119  *
120  * Revision 1.5  2001/11/18 03:08:17  jslopez
121  * Modifies the code to use reflection to
122  * get a handle on the Main compiler. This
123  * will avoid IncompatibleClassChangeError.
124  *
125  * Revision 1.4  2001/11/12 13:06:45  jslopez
126  * Removing control characters from the end of the lines.
127  *
128  * Revision 1.3  2001/10/17 04:15:30  paulk
129  * Added change log at the end of the file.
130  *
131  *
132  */
133
134 // End of CompileServer.java