1 17 18 package org.objectweb.jac.util; 19 20 import java.io.*; 21 import java.util.*; 22 import java.lang.reflect.*; 23 24 30 31 public class WrapLib { 32 33 public static String listAsString(List l) { 34 String s="["; 35 for(int i=0;i<l.size();i++) { 36 s=s+l.get(i); 37 if(i+1<l.size()) s=s+","; 38 } 39 return s; 40 } 41 42 49 50 public static void main(String [] args) throws Throwable { 51 String propFileName = "jac.prop"; 52 String toAdaptProp = "jac.toWrap"; 53 Properties props; 54 Vector classesToAdapt = new Vector(); 55 56 try { 57 58 FileInputStream fis = new FileInputStream( propFileName ); 59 60 props = new Properties(); 61 props.load( fis ); 62 String prop = props.getProperty(toAdaptProp); 63 64 if ( prop == null ) { 65 System.out.println( "-- ERROR: no property jac.toAdapt found in property file "+propFileName ); 66 System.exit(-1); 67 68 } else { 69 70 StringTokenizer st = new StringTokenizer( prop ); 71 while ( st.hasMoreElements() ) 72 classesToAdapt.add( st.nextElement() ); 73 } 74 75 76 for ( int i = 0; i < classesToAdapt.size(); i++ ) { 77 createDelegator( Class.forName( (String ) classesToAdapt.get(i) )); 78 } 79 80 } 81 catch( FileNotFoundException e ) { 82 System.out.println( "-- ERROR: property file "+propFileName+" not found" ); 83 System.exit(-1); 84 } 85 catch( Exception e ) { e.printStackTrace(); } 86 } 87 88 102 103 protected static void createDelegator( Class c ) { 104 try { 105 System.out.println("-- Generating delegator version of " + c + "."); 106 File f = new File( 107 "src/org/objectweb/jac/lib/" + c.getName().replace('.','/') + ".java" ); 108 if ( f.exists() ) 109 f.delete(); 110 f.getParentFile().mkdirs(); 111 f.createNewFile(); 112 String shortName = c.getName().substring( c.getName().lastIndexOf('.') + 1 ); 113 FileWriter fw = new FileWriter( f ); 114 fw.write( "/**\n" + 115 " * This class delegates to " + c.getName() + "\n" + 116 " * This file was automatically generated by JAC (-g option)\n" + 117 " * DO NOT MODIFY\n" + 118 " * Author: Renaud Pawlak (pawlak@cnam.fr)\n" + 119 " */\n" ); 120 fw.write( "\npackage jac.lib." + c.getPackage().getName() + ";\n" ); 121 fw.write( "\npublic class " + shortName + ((c.getInterfaces().length == 0)? "" : " implements " + 122 arrayToString( createArray( c.getInterfaces() ) )) ); 123 fw.write( " {\n" ); 124 fw.write( "\n private " + c.getName() + " delegate = new " + c.getName() + "();\n" ); 125 fw.write( "\n public Object clone() {\n Object result=null;\n try { result=super.clone(); } catch(Exception e) {};\n (("+ shortName +")result).delegate=("+c.getName()+")delegate.clone();\n return result;\n }\n"); 126 fw.write( "\n public boolean equals(Object o1) {\n return (delegate==null?super.equals(o1):delegate.equals(o1));\n }\n"); 127 Method[] ms = c.getMethods(); 129 for ( int i = 0; i < ms.length; i++ ) { 130 int mod = ms[i].getModifiers(); 131 if( Modifier.isPrivate(mod) || Modifier.isAbstract(mod) || 132 Modifier.isInterface(mod) || Modifier.isProtected(mod) || 133 Modifier.isStatic(mod) ) continue; 134 if( ms[i].getName().equals("clone") ) continue; 135 if( ms[i].getDeclaringClass() == Object .class || 137 ms[i].getDeclaringClass().isInterface() ) continue; 138 139 try { 140 Object .class.getMethod( ms[i].getName(), ms[i].getParameterTypes() ); 141 continue; 142 } catch ( Exception e ) {} 143 144 fw.write( "\n " + getMethodPrototype( ms[i] ) + " {\n" ); 145 if ( ms[i].getReturnType().getName().equals( "void" ) ) { 146 fw.write( " delegate." + ms[i].getName() + "(" + 148 arrayToString( createParameterNames( ms[i].getParameterTypes() ) ) + ");\n" ); 149 } else { 150 fw.write( " return delegate." + ms[i].getName() + "(" + 153 arrayToString( createParameterNames( ms[i].getParameterTypes() ) ) + ");\n" ); 154 } 155 fw.write( " }\n" ); 156 } 157 fw.write( "}\n" ); 158 fw.close(); 159 } catch ( Exception e ) { 160 e.printStackTrace(); 161 } 162 } 163 164 180 181 private static String getDefaultValueFor( Class c ) { 182 if ( c.isPrimitive() ) { 183 if (c == Boolean.TYPE ) 184 return "false"; 185 if (c == Character.TYPE ) 186 return "''"; 187 if ( c == Byte.TYPE ) 188 return "0"; 189 if (c == Short.TYPE || c == Integer.TYPE || 190 c == Long.TYPE || c == Float.TYPE || c == Double.TYPE ) 191 return "-1"; 192 } 193 return "null"; 194 } 195 196 203 204 private static String getMethodPrototype( Method m ) { 205 return "public " + createStringFor( m.getReturnType() ) + " " + m.getName() + 206 " ( " + arrayToString( createTypedParameterNames( m.getParameterTypes() ) ) + 207 " ) " + arrayToString( createArray( m.getExceptionTypes() ) ); 208 } 209 210 224 225 private static String arrayToString( String [] array ) { 226 if ( array.length == 0 ) return ""; 227 String ls = java.util.Arrays.asList(array).toString(); 228 return ls.substring( 1, ls.length() - 1 ); 229 } 230 231 246 247 private static String [] createArray( Class [] array ) { 248 String [] res = new String [array.length]; 249 for ( int i = 0; i < array.length; i ++ ) { 250 res[i] = createStringFor( array[i] ); 251 } 252 return res; 253 } 254 255 268 269 private static String createStringFor ( Class c ) { 270 if ( c.isArray() ) { 271 return c.getComponentType().getName() + "[]"; 272 } else { 273 return c.getName(); 274 } 275 } 276 277 292 293 private static String [] createTypedParameterNames ( Class [] array ) { 294 String [] res = new String [array.length]; 295 for ( int i = 0; i < array.length; i ++ ) { 296 res[i] = createStringFor( array[i] ) + " p" + i; 297 } 298 return res; 299 } 300 301 316 317 private static String [] createParameterNames ( Class [] array ) { 318 String [] res = new String [array.length]; 319 for ( int i = 0; i < array.length; i ++ ) { 320 res[i] = "p" + i; 321 } 322 return res; 323 } 324 325 } 326 327 | Popular Tags |