/plugins/JCompiler/branches/burton/src/java/net/sourceforge/jedit/jcompiler/JCompiler.java

# · Java · 453 lines · 279 code · 135 blank · 39 comment · 60 complexity · 3d2d47b150e134b82bb5672b489c734f MD5 · raw file

  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. package net.sourceforge.jedit.jcompiler;
  17. //jEdit interface
  18. import org.gjt.sp.jedit.*;
  19. import org.gjt.sp.util.*;
  20. //GUI support
  21. import javax.swing.*;
  22. import java.awt.event.ActionEvent;
  23. //build support
  24. import net.sourceforge.jedit.buildtools.*;
  25. import net.sourceforge.jedit.buildtools.msg.*;
  26. //standard java stuff
  27. import java.util.Vector;
  28. import java.io.*;
  29. import java.lang.*;
  30. import net.sourceforge.jedit.pluginholder.*;
  31. public class JCompiler extends EditAction implements BuildProgressListener {
  32. NoExitSecurityManager sm;
  33. boolean pkgCompile;
  34. boolean rebuild = false;
  35. public JCompiler(NoExitSecurityManager sm, String pluginName, boolean pkgCompile) {
  36. super(pluginName);
  37. this.pkgCompile = pkgCompile;
  38. this.sm = sm;
  39. }
  40. public JCompiler(NoExitSecurityManager sm, String pluginName, boolean pkgCompile, boolean rebuild)
  41. {
  42. super(pluginName);
  43. this.pkgCompile = pkgCompile;
  44. this.sm = sm;
  45. this.rebuild = rebuild;
  46. }
  47. public void actionPerformed(ActionEvent evt)
  48. {
  49. View view = getView(evt);
  50. Buffer buf = view.getBuffer();
  51. boolean autoSaveBuf;
  52. boolean autoSaveAll;
  53. if (pkgCompile)
  54. {
  55. autoSaveBuf = jEdit.getProperty("jcompiler.javapkgcompile.autosave").equals("T");
  56. autoSaveAll = jEdit.getProperty("jcompiler.javapkgcompile.autosaveall").equals("T");
  57. }
  58. else
  59. {
  60. autoSaveBuf = jEdit.getProperty("jcompiler.javacompile.autosave").equals("T");
  61. autoSaveAll = jEdit.getProperty("jcompiler.javacompile.autosaveall").equals("T");
  62. }
  63. if (autoSaveAll)
  64. {
  65. Buffer[] arr = jEdit.getBuffers();
  66. for (int i = 0; i < arr.length; i++)
  67. {
  68. if (arr[i].isDirty())
  69. {
  70. arr[i].save(view, null);
  71. }
  72. }
  73. }
  74. if (autoSaveAll == false && autoSaveBuf == true && buf.isDirty() == true)
  75. {
  76. buf.save(view, null);
  77. }
  78. if (autoSaveAll == false && autoSaveBuf == false && buf.isDirty() == true)
  79. {
  80. int result = JOptionPane.showConfirmDialog(view,
  81. "Save changes to " + buf.getName() + "?",
  82. "File Not Saved",
  83. JOptionPane.YES_NO_CANCEL_OPTION,
  84. JOptionPane.WARNING_MESSAGE);
  85. if (result == JOptionPane.CANCEL_OPTION)
  86. {
  87. return;
  88. }
  89. if (result == JOptionPane.YES_OPTION)
  90. {
  91. buf.save(view, null);
  92. }
  93. }
  94. String path = buf.getPath();
  95. if (path == null || path.equals("") || !path.endsWith(".java"))
  96. {
  97. return;
  98. }
  99. CompilerThread cc = getCompilerThread(view, path);
  100. cc.addBuildProgressListener(this);
  101. cc.start();
  102. }
  103. protected CompilerThread getCompilerThread(View view, String path) {
  104. return new CompilerThread(view, path, sm, pkgCompile, rebuild);
  105. }
  106. //BuildProgressListener
  107. public void reportNewBuild() {}
  108. public void reportBuildDone( boolean success ) {}
  109. public void reportError(String file, int line, String errorMessage) {}
  110. public void reportMessage(String message){}
  111. public void reportStatus(int progress, String message) {
  112. Log.log( Log.DEBUG, "JCompiler", " reportProgress()" );
  113. SwingUtilities.invokeLater(new BuildUpdater( progress, message ) );
  114. }
  115. }
  116. class CompilerThread extends Thread {
  117. String filename;
  118. String compileResult;
  119. NoExitSecurityManager sm;
  120. View view;
  121. boolean pkgCompile;
  122. boolean rebuild = false;
  123. //BEGIN FINAL BUILDER OBJECT CODE
  124. private Vector listeners = new Vector();
  125. public void addBuildProgressListener(BuildProgressListener listener) {
  126. listeners.addElement(listener);
  127. }
  128. public void removeBuildProgressListener(BuildProgressListener listener) {
  129. listeners.removeElement(listener);
  130. }
  131. public void sendStatus(int progress, String message) {
  132. Log.log( Log.DEBUG, "JCompiler", " sendStatus()" );
  133. //enumerate through all the BuildProgressListeners and report messages
  134. for ( int i = 0; i < listeners.size(); ++i ) {
  135. Log.log( Log.DEBUG, "JCompiler", " sending status of " + progress );
  136. ( (BuildProgressListener)listeners.elementAt(i) ).reportStatus(progress, message);
  137. }
  138. }
  139. //END FINAL BUILDER OBJECT CODE
  140. public CompilerThread( View view,
  141. String path,
  142. NoExitSecurityManager sm,
  143. boolean pkgCompile,
  144. boolean rebuild) {
  145. this(view, path, sm, pkgCompile);
  146. this.rebuild = rebuild;
  147. }
  148. public CompilerThread(View view, String path, NoExitSecurityManager sm, boolean pkgCompile) {
  149. super();
  150. this.setPriority( Thread.MIN_PRIORITY );
  151. this.view = view;
  152. this.sm = sm;
  153. this.pkgCompile = pkgCompile;
  154. filename = path;
  155. JCompilerPlugin.progress.setValue(0);
  156. }
  157. public String getArgumentsAsString( String[] arguments ) {
  158. StringBuffer buffer = new StringBuffer("");
  159. for (int i = 0; i < arguments.length; ++i ) {
  160. buffer.append( arguments[i] + " " );
  161. }
  162. return buffer.toString();
  163. }
  164. public void run() {
  165. //create a GUI for the user...
  166. String[] args = null;
  167. String[] files = null;
  168. String[] arguments = null;
  169. String titleStr = filename;
  170. PrintStream origOut = System.out;
  171. PrintStream origErr = System.err;
  172. ByteArrayOutputStream bytes = null;
  173. /*
  174. boolean smartCompile = (pkgCompile &&
  175. jEdit.getProperty("jcompiler.javapkgcompile.smartcompile").equals("T")) ? true : false;
  176. */
  177. try {
  178. File ff = new File(filename);
  179. String parent = ff.getParent();
  180. if (parent != null && pkgCompile == true) {
  181. String dir = JavaUtils.getBaseDirectory( ff.getAbsolutePath() );
  182. String exts[] = { "java" };
  183. FileChangeMonitor monitor = JCompilerPlugin.controller.getMonitor( dir, exts );
  184. String list[];
  185. if (this.rebuild) {
  186. list = monitor.getAllFiles();
  187. } else {
  188. list = monitor.getChangedFiles();
  189. }
  190. JCompilerPlugin.controller.getMonitor( dir, exts ).check();
  191. // = buildtools.JavaUtils.getFilesFromExtension(dir, exts );
  192. //the title to give the compiled output.
  193. titleStr = list.length + " file(s) in directory: " + dir;
  194. if (list.length == 0) {
  195. JCompilerPlugin.showStartCompileMsg(view, titleStr, true);
  196. return;
  197. }
  198. args = new String[2];
  199. files = list;
  200. }
  201. if (args == null) {
  202. titleStr = filename;
  203. JCompilerPlugin.showStartCompileMsg(view, titleStr, true);
  204. args = new String[2];
  205. args[1] = filename;
  206. String [] newfiles = { filename };
  207. files = newfiles;
  208. }
  209. // CLASSPATH Setting!!
  210. args[0] = "-classpath";
  211. if (jEdit.getProperty("jcompiler.usejavacp").equals("T"))
  212. {
  213. args[1] = System.getProperty("java.class.path");
  214. }
  215. else {
  216. args[1] = jEdit.getProperty("jcompiler.classpath");
  217. }
  218. if (jEdit.getProperty("jcompiler.addpkg2cp").equals("T"))
  219. {
  220. try
  221. {
  222. String pkgName = JavaUtils.getPackageName(filename);
  223. // If no package stmt found then pkgName would be null
  224. if (parent != null && pkgName == null) {
  225. args[1] = args[1] + System.getProperty("path.separator") + parent;
  226. }
  227. else if (parent != null && pkgName != null)
  228. {
  229. String pkgPath = pkgName.replace('.', System.getProperty("file.separator").charAt(0));
  230. if (parent.endsWith(pkgPath)) {
  231. parent = parent.substring(0, parent.length() - pkgPath.length() - 1);
  232. args[1] = args[1] + System.getProperty("path.separator") + parent;
  233. }
  234. }
  235. } catch (Exception exp) {
  236. exp.printStackTrace();
  237. }
  238. }
  239. JCompilerPlugin.showStartCompileMsg(view, titleStr, false);
  240. Vector vectorArgs = new Vector();
  241. vectorArgs.addElement( args[0] );
  242. vectorArgs.addElement( args[1] );
  243. if ( jEdit.getProperty("jcompiler.showdeprecated").equals("T") ) {
  244. vectorArgs.addElement("-deprecation");
  245. }
  246. if ( jEdit.getProperty( "jcompiler.specifyoutputdirectory").equals("T") ) {
  247. vectorArgs.addElement("-d");
  248. vectorArgs.addElement(jEdit.getProperty( "jcompiler.outputdirectory"));
  249. }
  250. //now add the files...
  251. for (int i = 0; i < files.length; ++i) {
  252. vectorArgs.addElement(files[i]);
  253. }
  254. arguments = new String[vectorArgs.size()];
  255. vectorArgs.copyInto(arguments);
  256. this.sendStatus( 50, "Starting compiler" );
  257. System.out.println( "JCompiler: compiling with arguments: " + getArgumentsAsString( arguments ) );
  258. bytes = new ByteArrayOutputStream();
  259. PrintStream ps = new PrintStream( bytes );
  260. System.setOut(ps);
  261. System.setErr(ps);
  262. sm.setAllowExit(false);
  263. sun.tools.javac.Main.main( arguments );
  264. //WARNING: don't put code here as sun.tools.javac.Main.main might throw
  265. //an exception
  266. //JCompilerPlugin.progress.setValue(100);
  267. //JCompilerPlugin.progress.setString("Compile done");
  268. } catch (SecurityException donothing) {
  269. //don't do anything here because sun.tools.javac.Main.main will
  270. //always try and exit.
  271. } catch (RuntimeException e) {
  272. if ( ! (e instanceof SecurityException) ) {
  273. System.err.println(
  274. "ERROR: Sun's javac just threw a runtime exceptions. " +
  275. "Please report this to the current JCompiler maintainer." );
  276. e.printStackTrace();
  277. }
  278. } catch (Exception e) {
  279. System.setOut(origOut);
  280. System.setErr(origErr);
  281. e.printStackTrace();
  282. } finally {
  283. System.setOut(origOut);
  284. System.setErr(origErr);
  285. this.sendStatus( 100, "Done" );
  286. }
  287. sm.setAllowExit(true);
  288. JCompilerPlugin.setCompilerOutput( view, titleStr , new String(bytes.toByteArray()) );
  289. }
  290. }
  291. class JavaFilenameFilter implements java.io.FilenameFilter
  292. {
  293. public boolean accept(File dir, String name)
  294. {
  295. if (name.endsWith(".java") == false)
  296. {
  297. return false;
  298. }
  299. String clazzName = name.substring(0, name.length()-5) + ".class";
  300. File clazzFile = new File(dir, clazzName);
  301. if (clazzFile.exists() == false)
  302. {
  303. return true;
  304. }
  305. File srcFile = new File(dir, name);
  306. if (srcFile.exists() == false)
  307. {
  308. return false;
  309. }
  310. long srcTime = srcFile.lastModified();
  311. long clsTime = clazzFile.lastModified();
  312. if (srcTime >= clsTime)
  313. {
  314. return true;
  315. }
  316. return false;
  317. }
  318. }