Author: chatellier Date: 2009-03-05 15:34:44 +0000 (Thu, 05 Mar 2009) New Revision: 1913 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/CompileHelper.java Log: Clean code, remove unused private code Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/CompileHelper.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/CompileHelper.java 2009-03-05 10:31:56 UTC (rev 1912) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/CompileHelper.java 2009-03-05 15:34:44 UTC (rev 1913) @@ -17,18 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * ClassUtil.java - * - * Created: 12 janv. 2006 15:29:53 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.util; import static org.codelutin.i18n.I18n._; @@ -38,7 +26,6 @@ import java.io.PrintWriter; import java.lang.reflect.Method; import java.net.URL; -import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -58,27 +45,24 @@ import fr.ifremer.isisfish.IsisFish; import fr.ifremer.isisfish.datastore.JavaSourceStorage; - /** + * Compile helper used to compile Java code. + * + * JDK must be installed to use compilation. + * (JRE won't work). + * + * Created: 12 janv. 2006 15:29:53 + * * @author poussin + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ - public class CompileHelper { - /** - * Logger for this class - */ + /** Logger for this class. */ private static final Log log = LogFactory.getLog(CompileHelper.class); - - /** path to the tools.jar library */ - private static File toolsjar; - /** compilation classloader that contains compiler (toolsjar) */ - private static ClassLoader loader; - /** method to use to compile java source */ - private static Method compileMethod; - /** method to use to compile java source and that redirect output to specific stream */ - private static Method compileMethodWithPrintStream; /** * Recherche tous les fichiers qui un source plus recent que la version compilé. @@ -87,9 +71,9 @@ * @param destDir * @return File list */ - static public List<File> searchSrcToCompile(File srcDir, File destDir) { + public static List<File> searchSrcToCompile(File srcDir, File destDir) { List<File> result = new ArrayList<File>(); - for(File src : srcDir.listFiles()) { + for (File src : srcDir.listFiles()) { File dest = new File(FileUtil.basename(src, ".java"), ".class"); if (src.getName().endsWith(".java") && FileUtil.isNewer(src, dest)) { result.add(src); @@ -97,13 +81,13 @@ } return result; } - + /** * Load la class demandé * @param fqn le nom complet de la classe a charger * @return la classe souhaité ou null si la class n'est pas trouvée */ - static public Class loadClass(String fqn) { + public static Class loadClass(String fqn) { Class result = null; try { ClassLoader cl = IsisFish.config.getScriptClassLoader(); @@ -113,21 +97,21 @@ } return result; } - - static public Object newInstance(String fqn) { + + public static Object newInstance(String fqn) { Object result = null; try { Class clazz = loadClass(fqn); if (clazz != null) { result = clazz.newInstance(); - } - } catch(Exception eee) { + } + } catch (Exception eee) { log.warn(_("isisfish.error.instanciate", fqn), eee); } return result; } - + /** * Compile le fichier source en .class si le source est plus recent que * le .class @@ -140,15 +124,18 @@ * utilisées. * @return 0 si la compilation a reussi une autre valeur sinon */ - static public int compile(JavaSourceStorage source, File destDir, boolean force, PrintWriter out) { + public static int compile(JavaSourceStorage source, File destDir, + boolean force, PrintWriter out) { File src = source.getFile(); - File dst = new File(destDir, source.getFQN().replace('.', File.separatorChar) + ".class"); + File dst = new File(destDir, source.getFQN().replace('.', + File.separatorChar) + + ".class"); if (force || FileUtil.isNewer(src, dst)) { return CompileHelper.compile(source.getRoot(), src, destDir, out); } return 0; } - + /** * Methode permettant de compiler un script, une regle ou un export * @param rootSrc le répertoire ou se trouve les sources @@ -163,17 +150,18 @@ * <li> -10000 si une autre exception * <li> sinon les valeurs retourné par le compilateur java */ - static public int compile(File rootSrc, File src, File dest, PrintWriter out) { + public static int compile(File rootSrc, File src, File dest, PrintWriter out) { int result = compile(rootSrc, Collections.singletonList(src), dest, out); return result; } - - static public int compile(File rootSrc, Collection<File> src, File dest, PrintWriter out) { + + public static int compile(File rootSrc, Collection<File> src, File dest, + PrintWriter out) { int result = -10000; - try { + try { List<File> classpath = new ArrayList<File>(); classpath.add(rootSrc.getAbsoluteFile()); - + result = compile(classpath, src, dest, out); } catch (Exception eee) { if (log.isWarnEnabled()) { @@ -182,23 +170,25 @@ } return result; } - - + /** * Compile un fichier java * @param src les fichiers java source * @param dest le repertoire destination */ - static protected int compile(List<File> classpath, Collection<File> src, File dest, PrintWriter out) { + protected static int compile(List<File> classpath, Collection<File> src, + File dest, PrintWriter out) { dest.mkdirs(); - + int result = -1000; try { //JavaCompiler compiler = JavacTool.create(); // Use system compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(src); + StandardJavaFileManager fileManager = compiler + .getStandardFileManager(null, null, null); + Iterable<? extends JavaFileObject> compilationUnits = fileManager + .getJavaFileObjectsFromFiles(src); // Options de compilations String classpathAsString = getClassPathAsString(classpath); @@ -208,12 +198,13 @@ args.add(classpathAsString); args.add("-d"); args.add(dest.getAbsolutePath()); - + // Compilation - boolean b = compiler.getTask(out, fileManager, null, args, null, compilationUnits).call(); + boolean b = compiler.getTask(out, fileManager, null, args, null, + compilationUnits).call(); // on retourne 0 si tout s'est bien déroulé et -1 sinon - result = b?0:-1; - + result = b ? 0 : -1; + fileManager.close(); } catch (Exception eee) { if (log.isWarnEnabled()) { @@ -222,34 +213,38 @@ } return result; } - + /** * @return */ - private static String getClassPathAsString(List<File> classpath) throws Exception { - String result = StringUtils.join(classpath.iterator(), File.pathSeparator) + - File.pathSeparator + System.getProperty("java.class.path"); + private static String getClassPathAsString(List<File> classpath) + throws Exception { + String result = StringUtils.join(classpath.iterator(), + File.pathSeparator) + + File.pathSeparator + System.getProperty("java.class.path"); // Ajout des jars - for (Enumeration<?> e = CompileHelper.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); e.hasMoreElements();) { + for (Enumeration<?> e = CompileHelper.class.getClassLoader() + .getResources("META-INF/MANIFEST.MF"); e.hasMoreElements();) { URL url = (URL) e.nextElement(); if ((url != null) && url.getFile().startsWith("file:/")) { - String jarName = url.getPath().substring(5, url.getPath().indexOf("!")); - if(!result.contains(jarName)) { + String jarName = url.getPath().substring(5, + url.getPath().indexOf("!")); + if (!result.contains(jarName)) { result += File.pathSeparator + jarName; } } } - + // FIXME just for test to run in maven // In this case, current project in not packaged ad jar // and no META-INF/MANIFEST.MF could be found... - File targetClassesFile = new File("target","classes"); + File targetClassesFile = new File("target", "classes"); if (targetClassesFile.isDirectory()) { result += File.pathSeparator + targetClassesFile.getAbsolutePath(); } - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("CLASSPATH : " + result); } @@ -257,70 +252,45 @@ } /** - * @return - */ - private static File getToolsJar() { - if (toolsjar == null) { - File javaHome = new File(System.getProperty("java.home")).getParentFile(); - log.debug("javaHome " + javaHome); - List<File> tools = FileUtil.find(javaHome, ".*tools.jar", true); - - if (tools.size() > 0) { - toolsjar = tools.get(0); - log.debug("tools.jar -> " + toolsjar); - } - } - return toolsjar; - } - - - private static class CompilerClassLoader extends URLClassLoader { - - public CompilerClassLoader(URL [] url, ClassLoader parent) { - super(url, parent); - } - - } - - /** * Extract documentation from interface (Equation) * @param category * @param name * @param javaInterface * @return doc */ - static public String extractDoc(String category, String name, Class javaInterface) { + public static String extractDoc(String category, String name, + Class javaInterface) { String content = ""; - content = "<p>Equation : " + createHREF(javaInterface.getName(), category) + " - " + name + "</p>"; + content = "<p>Equation : " + + createHREF(javaInterface.getName(), category) + " - " + name + + "</p>"; content += "<h3>Parameter: name and type</h3>"; - - if (javaInterface != null) { - Method [] methods = javaInterface.getDeclaredMethods(); - Method interfaceMethod = methods[0]; - - Args args = interfaceMethod.getAnnotation(Args.class); - String [] names = args.value(); - - String [] stringTypes = null; - ArgTypes argTypes = interfaceMethod.getAnnotation(ArgTypes.class); - if (argTypes != null) { - stringTypes = argTypes.value(); - } else { - stringTypes = new String[names.length]; - Class [] types = interfaceMethod.getParameterTypes(); - for (int i=0; i<types.length; i++) { - stringTypes[i] = types[i].getName(); - } + + Method[] methods = javaInterface.getDeclaredMethods(); + Method interfaceMethod = methods[0]; + + Args args = interfaceMethod.getAnnotation(Args.class); + String[] names = args.value(); + + String[] stringTypes = null; + ArgTypes argTypes = interfaceMethod.getAnnotation(ArgTypes.class); + if (argTypes != null) { + stringTypes = argTypes.value(); + } else { + stringTypes = new String[names.length]; + Class[] types = interfaceMethod.getParameterTypes(); + for (int i = 0; i < types.length; i++) { + stringTypes[i] = types[i].getName(); } - - for (int i=0; i<names.length; i++) { - content += "<li>" + names[i] + " : " + createHREF(stringTypes[i]); -// if (i+1<names.length) { -// content += "\n"; -// } - } - } + + for (int i = 0; i < names.length; i++) { + content += "<li>" + names[i] + " : " + createHREF(stringTypes[i]); + // if (i+1<names.length) { + // content += "\n"; + // } + } + try { FileUtil.writeString(new File("/tmp/testDoc.html"), content); } catch (IOException eee) { @@ -330,16 +300,15 @@ } return content; } - - static private String createHREF(String type, String ... texts) { - String ref = IsisFish.config.getJavadocURL() + type.replaceAll("\\.", "/") + ".html"; + + private static String createHREF(String type, String... texts) { + String ref = IsisFish.config.getJavadocURL() + + type.replaceAll("\\.", "/") + ".html"; String text = type; if (texts.length > 0) { text = texts[0]; } - String result = "<a href='"+ref+"'>" + text + "</a>"; + String result = "<a href='" + ref + "'>" + text + "</a>"; return result; } } - -
participants (1)
-
chatellier@users.labs.libre-entreprise.org