gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2935 - freeway/src/org/gnu/freeway/cwrappers/util


From: mdonoughe
Subject: [GNUnet-SVN] r2935 - freeway/src/org/gnu/freeway/cwrappers/util
Date: Sun, 28 May 2006 20:41:23 -0700 (PDT)

Author: mdonoughe
Date: 2006-05-28 20:41:21 -0700 (Sun, 28 May 2006)
New Revision: 2935

Modified:
   freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
Log:
added more code to SwitchTableGenerator. not yet using Christian's code. 
Currently prints out the beginnings of a Java class file that implements 
the interface, but it doesn't compile yet because it doesn't return 
anything. I will work on it more tomorrow.


Modified: freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-05-29 01:59:44 UTC (rev 2934)
+++ freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-05-29 03:41:21 UTC (rev 2935)
@@ -21,13 +21,18 @@
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.ArrayList;
 
 /**
  * @file SwitchTableGenerator.java
  * @brief This class should generate the switch-table.c, switch-table.h and
  *        service.impl.* classes by reflecting over the various service 
interfaces
  *        and generating the appropriate code 
- * @author Christina Grothoff
+ * @author Christian Grothoff
+ * @author mdonoughe
  */
 public class SwitchTableGenerator {
 
@@ -45,8 +50,86 @@
         * @param args
         */
        public static void main(String[] args) {
-
+               // for now, start with this
+               Class c = String.class;
+               try {
+                       c = 
Class.forName("org.gnu.freeway.services.StatsService");
+               } catch(ClassNotFoundException e) {
+                       e.printStackTrace();
+               }
+               implementClass(c);
        }
+       
+       public static void implementClass(Class c) {
+               if(!c.isInterface()) {
+                       System.err.println(c.getName() + " is not an 
interface.");
+                       return;
+               }
+               System.err.println("Implementing " + c.getName());
+               
+               //open file
+               //write class head
+               HashMap imports = new HashMap();
+               //use some magic to include the yet-to-be-determined import 
statements here
+               
+               Method[] methods = c.getMethods();
+               ArrayList methodList = new ArrayList();
+               
+               for(int i = 0; i < methods.length; i++) {
+                       //write methods
+                       StringBuffer buffer = new StringBuffer();
+                       buffer.append("public " + 
cleanClassName(methods[i].getReturnType().getName(), imports) + " " + 
methods[i].getName() + "(");
+                       Class[] argTypes = methods[i].getParameterTypes();
+                       for(int j = 0; j < argTypes.length; j++) {
+                               if(j > 0)
+                                       buffer.append(", ");
+                               
buffer.append(cleanClassName(argTypes[j].getName(), imports) + " arg" + j);
+                       }
+                       buffer.append(") {\n");
+                       buffer.append("//TODO: write code that writes code 
here\n");
+                       buffer.append("}");
+                       methodList.add(buffer.toString());
+               }
+               
+               System.out.println("// This class was autogenerated by 
SwitchTableGenerator");
+               System.out.println("pacakge org.gnu.freeway.services.impl;");
+               System.out.println();
+               Collection importSet = imports.values();
+               for(Iterator i = importSet.iterator(); i.hasNext(); )
+                       System.out.println("import " + (String) i.next() + ";");
+               System.out.println();
+               System.out.println("public class " + stripPackage(c.getName()) 
+ " implements " + c.getName() + " {");
+               for(Iterator i = methodList.iterator(); i.hasNext(); )
+                       System.out.println((String) i.next());
+               System.out.println("}");
+       }
+       
+       public static String cleanClassName(String input, HashMap importMap) {
+               int lastDot = input.lastIndexOf(".");
+               if(lastDot <= 1)
+                       return input;
+               String nameStart = input.substring(0, lastDot - 1);
+               String nameEnd = input.substring(lastDot + 1);
+               if("java.lang".equals(nameStart)) 
+                       return nameEnd; // already included
+               if(importMap.containsKey(nameEnd)) {
+                       if(input.equals((String) importMap.get(nameEnd)))
+                               return nameEnd; // already imported
+                       else
+                               return input; // another class with the same 
name has already been imported
+               } else {
+                       // this is the first occurance
+                       importMap.put(nameEnd, input);
+                       return nameEnd;
+               }
+       }
+       
+       public static String stripPackage(String input) {
+               int lastDot = input.lastIndexOf(".");
+               if(lastDot <= 0)
+                       return input;
+               return input.substring(lastDot + 1);
+       }
 
        public static int getFunctionType(Method m) {
                Class[] args = m.getParameterTypes();





reply via email to

[Prev in Thread] Current Thread [Next in Thread]