Author: chatellier Date: 2009-03-11 15:21:43 +0000 (Wed, 11 Mar 2009) New Revision: 1940 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java Log: Fix tests. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java 2009-03-11 14:20:09 UTC (rev 1939) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java 2009-03-11 15:21:43 UTC (rev 1940) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,18 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * EvaluatorHelper.java - * - * Created: 3 juil. 2006 23:44:48 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.util; import static org.codelutin.i18n.I18n._; @@ -52,16 +40,20 @@ /** * Permet d'evaluer les equations ecritent en Java * + * Created: 3 juil. 2006 23:44:48 + * * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ */ public class EvaluatorHelper { - - /** - * Logger for this class - */ - private static final Log log = LogFactory.getLog(EvaluatorHelper.class); - static private String normalizeClassName(String name) { + /** Logger for this class. */ + private static Log log = LogFactory.getLog(EvaluatorHelper.class); + + protected static String normalizeClassName(String name) { StringBuffer result = new StringBuffer(name); for (int i=0; i<result.length(); i++) { char c = result.charAt(i); @@ -71,16 +63,16 @@ } return result.toString(); } - + /** - * Verifie si une equation est syntaxiquement correct + * Verifie si une equation est syntaxiquement correcte. * * @param javaInterface * @param script - * @param out + * @param out output writer (can be null for non output) * @return 0 si ok */ - static public int check(Class javaInterface, String script, PrintWriter out) { + public static int check(Class javaInterface, String script, PrintWriter out) { try { File src = File.createTempFile("check", "equation"); src.deleteOnExit(); @@ -109,8 +101,9 @@ return -10000; } } - + /** + * Evalue une equation. * * @param packageName le nom de package de la classe * @param className le nom de la classe @@ -120,26 +113,26 @@ * @param args les arguments a utiliser pour l'appel de la methode * @return la valeur retourné par la methode */ - static public Object evaluate(String packageName, String className, + public static Object evaluate(String packageName, String className, Class javaInterface, String script, Map<String, Object> args) { className = normalizeClassName(className); - + Object result = null; Class clazz = null; // recherche la methode de l'interface Method [] methods = javaInterface.getDeclaredMethods(); Method interfaceMethod = methods[0]; - + String classname = packageName + "." + className; File fileRootSrc = IsisFish.config.getCompileDirectory(); File fileCheckSum = new File(fileRootSrc, packageName + File.separator + className + ".hashCode"); File fileSrc = new File(fileRootSrc, packageName + File.separator + className + ".java"); File fileDest = new File(fileRootSrc, packageName + File.separator + className + ".class"); - + boolean checkSumEquals = false; - + // if equation's Java file exists, check the checksum if (fileSrc.exists() && fileCheckSum.exists()) { String oldCheckSum = ""; @@ -151,7 +144,7 @@ String newCheckSum = Integer.toString(script.hashCode()); checkSumEquals = newCheckSum.equals(oldCheckSum); } - + // if Java file's checkSum is not equals to script's checkSum // generate new Java file if (!checkSumEquals) { @@ -168,7 +161,7 @@ throw new IsisFishRuntimeException(_("isisfish.error.save.checkSum.compilation", fileSrc), zzz); } } - + // if Java file is newer than class file, compile java file if (FileUtil.isNewer(fileSrc, fileDest)) { try { @@ -180,7 +173,7 @@ throw new IsisFishRuntimeException(_("isisfish.error.compile.script", fileSrc), zzz); } } - + // try to load class try { ClassLoader cl = IsisFish.config.getScriptClassLoader(); @@ -188,13 +181,13 @@ } catch (Exception zzz) { throw new IsisFishRuntimeException(_("isisfish.error.load.class", classname), zzz); } - + result = invoke(clazz, interfaceMethod, args); - + return result; } - - static protected String generateContent (String packageName, String className, Method interfaceMethod, String script) { + + protected static String generateContent (String packageName, String className, Method interfaceMethod, String script) { String content = ""; if (packageName != null && !"".equals(packageName)) { content += "package " + packageName + ";"; @@ -210,7 +203,7 @@ content += "public " + interfaceMethod.getReturnType().getName() + " " + interfaceMethod.getName() + "("; - + Args args = interfaceMethod.getAnnotation(Args.class); String [] names = args.value(); @@ -225,7 +218,7 @@ stringTypes[i] = types[i].getName(); } } - + for (int i=0; i<names.length; i++) { content += stringTypes[i] + " " + names[i]; if (i+1<names.length) { @@ -239,7 +232,7 @@ return content; } - static protected Object invoke(Class clazz, Method interfaceMethod, Map<String, Object> args) { + protected static Object invoke(Class clazz, Method interfaceMethod, Map<String, Object> args) { try { Method method = clazz.getDeclaredMethod(interfaceMethod.getName(), interfaceMethod.getParameterTypes());
participants (1)
-
chatellier@users.labs.libre-entreprise.org