From 26013b3602e053d7005a554a2793c30930e1ac08 Mon Sep 17 00:00:00 2001
From: Ludovic Apvrille <ludovic.apvrille@telecom-paristech.fr>
Date: Tue, 19 Jun 2018 18:01:43 +0200
Subject: [PATCH] Adding directory method to C code generation plugin

---
 .idea/misc.xml                                |   2 +-
 src/main/java/myutil/Plugin.java              | 308 +++++++++---------
 .../ui/window/JDialogAvatarModelChecker.java  |   1 +
 .../ui/window/JDialogCCodeGeneration.java     |   4 +
 4 files changed, 163 insertions(+), 152 deletions(-)

diff --git a/.idea/misc.xml b/.idea/misc.xml
index b755d8ca46..51e5191ed4 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -38,7 +38,7 @@
       <property name="caretWidth" class="java.lang.Integer" />
     </properties>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="false" project-jdk-name="1.7" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build" />
   </component>
 </project>
\ No newline at end of file
diff --git a/src/main/java/myutil/Plugin.java b/src/main/java/myutil/Plugin.java
index dac53373ed..5e8a27626d 100644
--- a/src/main/java/myutil/Plugin.java
+++ b/src/main/java/myutil/Plugin.java
@@ -1,26 +1,26 @@
 /* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
- * 
+ *
  * ludovic.apvrille AT enst.fr
- * 
+ *
  * This software is a computer program whose purpose is to allow the
  * edition of TURTLE analysis, design and deployment diagrams, to
  * allow the generation of RT-LOTOS or Java code from this diagram,
  * and at last to allow the analysis of formal validation traces
  * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
  * from INRIA Rhone-Alpes.
- * 
+ *
  * This software is governed by the CeCILL  license under French law and
  * abiding by the rules of distribution of free software.  You can  use,
  * modify and/ or redistribute the software under the terms of the CeCILL
  * license as circulated by CEA, CNRS and INRIA at the following URL
  * "http://www.cecill.info".
- * 
+ *
  * As a counterpart to the access to the source code and  rights to copy,
  * modify and redistribute granted by the license, users are provided only
  * with a limited warranty  and the software's author,  the holder of the
  * economic rights,  and the successive licensors  have only  limited
  * liability.
- * 
+ *
  * In this respect, the user's attention is drawn to the risks associated
  * with loading,  using,  modifying and/or developing or reproducing the
  * software by the user in light of its specific status of free software,
@@ -31,29 +31,27 @@
  * requirements in conditions enabling the security of their systems and/or
  * data to be ensured and,  more generally, to use and operate it in the
  * same conditions as regards security.
- * 
+ *
  * The fact that you are presently reading this means that you have had
  * knowledge of the CeCILL license and that you accept its terms.
  */
 
 
-
-
 package myutil;
 
+import javax.swing.*;
 import java.io.File;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.HashMap;
 
-import javax.swing.*;
-
 /**
-   * Class Plugin
-   * Creation: 24/05/2017
-   * Version 1.0 24/05/2017
-   * @author Ludovic APVRILLE
+ * Class Plugin
+ * Creation: 24/05/2017
+ * Version 1.0 24/05/2017
+ *
+ * @author Ludovic APVRILLE
  */
 public class Plugin {
     private String path;
@@ -64,12 +62,12 @@ public class Plugin {
     private Class classAvatarCodeGenerator;
     private Class classDiplodocusCodeGenerator;
     private Class classGraphicalComponent;
-    
+
 
     public Plugin(String _path, String _name, String _packageName) {
-	path = _path;
+        path = _path;
         name = _name;
-	packageName = _packageName.trim();
+        packageName = _packageName.trim();
         listOfClasses = new HashMap<String, Class>();
     }
 
@@ -78,113 +76,113 @@ public class Plugin {
     }
 
     public String getPath() {
-	return path;
+        return path;
     }
 
     public boolean hasAvatarCodeGenerator() {
-	String ret = executeRetStringMethod(removeJar(name), "hasAvatarCodeGenerator");
-	if (ret != null) {
-	    classAvatarCodeGenerator = getClass(ret);
-	    return true;
-	}
+        String ret = executeRetStringMethod(removeJar(name), "hasAvatarCodeGenerator");
+        if (ret != null) {
+            classAvatarCodeGenerator = getClass(ret);
+            return true;
+        }
 
-	return false;
+        return false;
     }
-    
+
     public boolean hasDiplodocusCodeGenerator() {
-	String ret = executeRetStringMethod(removeJar(name), "hasDiplodocusCodeGenerator");
-	if (ret != null) {
-	    classDiplodocusCodeGenerator = getClass(ret);
-	    return true;
-	}
+        String ret = executeRetStringMethod(removeJar(name), "hasDiplodocusCodeGenerator");
+        if (ret != null) {
+            classDiplodocusCodeGenerator = getClass(ret);
+            return true;
+        }
 
-	return false;
+        return false;
     }
 
     public String getDiplodocusCodeGeneratorIdentifier() {
-	String desc = executeRetStringMethod(classDiplodocusCodeGenerator, "getIdentifier");
-	return desc;
+        String desc = executeRetStringMethod(classDiplodocusCodeGenerator, "getIdentifier");
+        return desc;
     }
-    
+
 
     public boolean hasGraphicalComponent() {
-	String ret = executeRetStringMethod(removeJar(name), "hasGraphicalComponent");
-	if (ret != null) {
-	    classGraphicalComponent = getClass(ret);
-	    String diagOk = executeRetStringMethod(classGraphicalComponent, "getPanelClassName");
-	    if (diagOk != null) {
-		return true;
-	    }
-	}
-	classGraphicalComponent = null;
-	return false;	
+        String ret = executeRetStringMethod(removeJar(name), "hasGraphicalComponent");
+        if (ret != null) {
+            classGraphicalComponent = getClass(ret);
+            String diagOk = executeRetStringMethod(classGraphicalComponent, "getPanelClassName");
+            if (diagOk != null) {
+                return true;
+            }
+        }
+        classGraphicalComponent = null;
+        return false;
     }
 
     public boolean hasGraphicalComponent(String _diagID) {
-	//TraceManager.addDev("Test GC with diag=" + _diagID);
-	String ret = executeRetStringMethod(removeJar(name), "hasGraphicalComponent");
-	if (ret != null) {
-	    classGraphicalComponent = getClass(ret);
-	    String diagOk = executeRetStringMethod(classGraphicalComponent, "getPanelClassName");
-	    if (diagOk != null) {
-		if (diagOk.compareTo(_diagID) == 0) {
-		    //TraceManager.addDev("Found graphical component in plugin:" + name);
-		    return true;
-		}
-	    }
-	}
-	classGraphicalComponent = null;
-
-	return false;
+        //TraceManager.addDev("Test GC with diag=" + _diagID);
+        String ret = executeRetStringMethod(removeJar(name), "hasGraphicalComponent");
+        if (ret != null) {
+            classGraphicalComponent = getClass(ret);
+            String diagOk = executeRetStringMethod(classGraphicalComponent, "getPanelClassName");
+            if (diagOk != null) {
+                if (diagOk.compareTo(_diagID) == 0) {
+                    //TraceManager.addDev("Found graphical component in plugin:" + name);
+                    return true;
+                }
+            }
+        }
+        classGraphicalComponent = null;
+
+        return false;
     }
 
     public Class getClassAvatarCodeGenerator() {
-	return classAvatarCodeGenerator;
+        return classAvatarCodeGenerator;
     }
-    
+
     public Class getClassGraphicalComponent() {
-	return classGraphicalComponent;
+        return classGraphicalComponent;
     }
 
     public Class getClassDiplodocusCodeGenerator() {
-	return classDiplodocusCodeGenerator;
+        return classDiplodocusCodeGenerator;
     }
-    
+
 
     public Class getClass(String _className) {
-	Class<?> c = listOfClasses.get(_className);
-	if (c != null) {
-	    return c;
-	}
+        Class<?> c = listOfClasses.get(_className);
+        if (c != null) {
+            return c;
+        }
 
-	try {
+        try {
             if (c == null) {
-                file = new File( path + java.io.File.separator + name);
+                file = new File(path + java.io.File.separator + name);
                 TraceManager.addDev("Loading plugin=" + path + java.io.File.separator + name);
-                URL[] urls = new URL[] { file.toURI().toURL() };
+                URL[] urls = new URL[]{file.toURI().toURL()};
                 ClassLoader loader = new URLClassLoader(urls);
                 //TraceManager.addDev("getClass() Loader created");
-		if ((packageName == null) || (packageName.length() == 0)) {
-		    c = loader.loadClass( _className );
-		} else {
-		    c = loader.loadClass( packageName + "." + _className);
-		}
+                if ((packageName == null) || (packageName.length() == 0)) {
+                    c = loader.loadClass(_className);
+                } else {
+                    c = loader.loadClass(packageName + "." + _className);
+                }
                 //TraceManager.addDev("getClass() class loaded");
                 if (c == null) {
                     return null;
                 }
                 listOfClasses.put(_className, c);
-		return c;
+                return c;
             }
 
         } catch (Exception e) {
-            TraceManager.addDev( "getClass()\n" );
-            e.printStackTrace( System.out );
-    	    //TraceManager.addDev("Exception when using plugin " + name + " with className=" + _className);
-	    return null;
+            TraceManager.addDev("getClass()\n");
+            e.printStackTrace(System.out);
+            //TraceManager.addDev("Exception when using plugin " + name + " with className=" + _className);
+            return null;
         }
 
-	return null;
+        return null;
     }
 
     public Method getMethod(String _className, String _methodName) {
@@ -194,15 +192,15 @@ public class Plugin {
             if (c == null) {
                 file = new File(path + java.io.File.separator + name);
                 //TraceManager.addDev("Loading plugin=" + path + java.io.File.separator + name);
-                URL[] urls = new URL[] { file.toURI().toURL() };
+                URL[] urls = new URL[]{file.toURI().toURL()};
                 ClassLoader loader = new URLClassLoader(urls);
                 //TraceManager.addDev("Loader created");
-		if ((packageName == null) || (packageName.length() == 0)) {
-		    c = loader.loadClass( _className );
-		} else {
-		    c = loader.loadClass( packageName + "." + _className);
-		}
- 
+                if ((packageName == null) || (packageName.length() == 0)) {
+                    c = loader.loadClass(_className);
+                } else {
+                    c = loader.loadClass(packageName + "." + _className);
+                }
+
                 //TraceManager.addDev( "Class loaded" );
                 if (c == null) {
                     return null;
@@ -213,90 +211,98 @@ public class Plugin {
             return c.getMethod(_methodName);
         } catch (Exception e) {
             //e.printStackTrace( System.out );
-	        TraceManager.addDev("Exception when using plugin " + name + " with className=" + _className + " and method " + _methodName);
-	    return null;
+            TraceManager.addDev("Exception when using plugin " + name + " with className=" + _className + " and method " + _methodName);
+            return null;
         }
 
     }
 
     public String executeRetStringMethod(String _className, String _methodName) {
-	// We have a valid plugin. We now need to get the Method
-	Method m = getMethod(_className, _methodName);
-	if (m == null) {
-	    //TraceManager.addDev("Null method with class as a string class=" + _className + " _method=" + _methodName);
-	    return null;
-	}
-	
-	try {
-	    return (String)(m.invoke(null));
-	} catch (Exception e) {
-	    TraceManager.addDev("Exception occured when executing method " + _methodName + " in class=" + _className);
-	    return null;
-	}
+        // We have a valid plugin. We now need to get the Method
+        Method m = getMethod(_className, _methodName);
+        if (m == null) {
+            //TraceManager.addDev("Null method with class as a string class=" + _className + " _method=" + _methodName);
+            return null;
+        }
+
+        try {
+            return (String) (m.invoke(null));
+        } catch (Exception e) {
+            TraceManager.addDev("Exception occured when executing method " + _methodName + " in class=" + _className);
+            return null;
+        }
     }
 
     public String executeRetStringMethod(Class<?> c, String _methodName) {
-	// We have a valid plugin. We now need to get the Method
-		
-	try {
-	    //TraceManager.addDev("Getting " + _methodName + " in class " + c.getName());
-	    Method m = c.getMethod(_methodName);
-	    
-	    if (m == null) {
-		//TraceManager.addDev("Null method in executeRetStringMethod with Class parameter");
-		return null;
-	    }
-	    return (String)(m.invoke(null));
-	} catch (Exception e) {
-	    TraceManager.addDev("Exception occured when executing method " + _methodName);
-	    return null;
-	}
+        // We have a valid plugin. We now need to get the Method
+
+        try {
+            //TraceManager.addDev("Getting " + _methodName + " in class " + c.getName());
+            Method m = c.getMethod(_methodName);
+
+            if (m == null) {
+                //TraceManager.addDev("Null method in executeRetStringMethod with Class parameter");
+                return null;
+            }
+            return (String) (m.invoke(null));
+        } catch (Exception e) {
+            TraceManager.addDev("Exception occured when executing method " + _methodName);
+            return null;
+        }
     }
 
     public static int executeIntMethod(Object instance, String _methodName) throws Exception {
-	Class[] cArg = new Class[0];
-	Method method = instance.getClass().getMethod(_methodName, cArg);
-	return (int)(method.invoke(instance));
+        Class[] cArg = new Class[0];
+        Method method = instance.getClass().getMethod(_methodName, cArg);
+        return (int) (method.invoke(instance));
     }
 
     public static boolean executeBoolMethod(Object instance, String _methodName) throws Exception {
-	Class[] cArg = new Class[0];
-	Method method = instance.getClass().getMethod(_methodName, cArg);
-	return (boolean)(method.invoke(instance));
+        Class[] cArg = new Class[0];
+        Method method = instance.getClass().getMethod(_methodName, cArg);
+        return (boolean) (method.invoke(instance));
     }
-    
+
     public static boolean executeBoolStringMethod(Object instance, String value, String _methodName, String options) throws Exception {
-	Class[] cArg = new Class[2];
-	cArg[0] = String.class;
-	cArg[1] = String.class;
-	//TraceManager.addDev("Looking for method=" + _methodName + " in instance " + instance);
-	Method method = instance.getClass().getMethod(_methodName, cArg);
-	return (boolean)(method.invoke(instance, value, options));
+        Class[] cArg = new Class[2];
+        cArg[0] = String.class;
+        cArg[1] = String.class;
+        //TraceManager.addDev("Looking for method=" + _methodName + " in instance " + instance);
+        Method method = instance.getClass().getMethod(_methodName, cArg);
+        return (boolean) (method.invoke(instance, value, options));
+    }
+
+    public static void executeOneStringMethod(Object instance, String value, String _methodName) throws Exception {
+        Class[] cArg = new Class[1];
+        cArg[0] = String.class;
+
+        //TraceManager.addDev("Looking for method=" + _methodName + " in instance " + instance);
+        Method method = instance.getClass().getMethod(_methodName, cArg);
+        method.invoke(instance, value);
     }
-    
 
-    
+
     public ImageIcon executeRetImageIconMethod(Class<?> c, String _methodName) {
-	// We have a valid plugin. We now need to get the Method
-	try {
-	Method m = c.getMethod(_methodName);
-	if (m == null) {
-	    return null;
-	}
-	
-	return (ImageIcon)(m.invoke(null));
-	} catch (Exception e) {
-	    TraceManager.addDev("Exception occured when executing method " + _methodName);
-	    return null;
-	}
+        // We have a valid plugin. We now need to get the Method
+        try {
+            Method m = c.getMethod(_methodName);
+            if (m == null) {
+                return null;
+            }
+
+            return (ImageIcon) (m.invoke(null));
+        } catch (Exception e) {
+            TraceManager.addDev("Exception occured when executing method " + _methodName);
+            return null;
+        }
     }
 
     public String removeJar(String withjar) {
-	int index = withjar.indexOf(".jar");
-	if (index == -1) {
-	    return withjar;
-	}
-	return withjar.substring(0, index);
+        int index = withjar.indexOf(".jar");
+        if (index == -1) {
+            return withjar;
+        }
+        return withjar.substring(0, index);
 
     }
 
diff --git a/src/main/java/ui/window/JDialogAvatarModelChecker.java b/src/main/java/ui/window/JDialogAvatarModelChecker.java
index b45c68d7fa..02324eb6ab 100644
--- a/src/main/java/ui/window/JDialogAvatarModelChecker.java
+++ b/src/main/java/ui/window/JDialogAvatarModelChecker.java
@@ -455,6 +455,7 @@ public class JDialogAvatarModelChecker extends javax.swing.JFrame implements Act
     public void displayGraph() {
         // Make this in a different thread
         // And authorize the show only for a small nb of states ...
+
         if (graphAUT != null) {
             AUTGraph rg = new AUTGraph();
             rg.buildGraph(graphAUT);
diff --git a/src/main/java/ui/window/JDialogCCodeGeneration.java b/src/main/java/ui/window/JDialogCCodeGeneration.java
index 47fc2d06c9..5d600a4a55 100755
--- a/src/main/java/ui/window/JDialogCCodeGeneration.java
+++ b/src/main/java/ui/window/JDialogCCodeGeneration.java
@@ -525,6 +525,10 @@ public class JDialogCCodeGeneration extends JDialog implements ActionListener, R
                         if (instance == null) {
                             jta.append("Invalid plugin: could not create an instance\n");
                         } else {
+                            // Try to set the current project directory
+                            Plugin.executeOneStringMethod(instance, code1.getText(), "setCodeDirectory");
+
+                            // then start the plugin itself
                             boolean ret = Plugin.executeBoolStringMethod(instance, XML, "generateCode", codeOpt.getText());
                         }
                     } catch (Exception e) {
-- 
GitLab