1 11 package org.eclipse.swt.tools.internal; 12 13 import java.io.*; 14 import java.lang.reflect.*; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.zip.*; 18 import java.util.Arrays ; 19 20 import org.eclipse.swt.SWT; 21 22 public class JNIGeneratorApp { 23 24 Class mainClass; 25 ProgressMonitor progress; 26 String mainClassName, outputDir, classpath; 27 MetaData metaData; 28 29 public JNIGeneratorApp() { 30 } 31 32 public String getClasspath() { 33 return classpath; 34 } 35 36 public Class getMainClass() { 37 return mainClass; 38 } 39 40 public String getMainClassName() { 41 return mainClassName; 42 } 43 44 public MetaData getMetaData() { 45 return metaData; 46 } 47 48 String getMetaDataDir() { 49 return "./JNI Generation/org/eclipse/swt/tools/internal/"; 50 } 51 52 public String getOutputDir() { 53 return outputDir; 54 } 55 56 void generateSTATS_C(Class [] classes) { 57 try { 58 StatsGenerator gen = new StatsGenerator(false); 59 gen.setMainClass(mainClass); 60 gen.setClasses(classes); 61 gen.setMetaData(metaData); 62 gen.setProgressMonitor(progress); 63 ByteArrayOutputStream out = new ByteArrayOutputStream(); 64 gen.setOutput(new PrintStream(out)); 65 gen.generate(); 66 if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName()); 67 } catch (Exception e) { 68 System.out.println("Problem"); 69 e.printStackTrace(System.out); 70 } 71 } 72 73 void generateSTATS_H(Class [] classes) { 74 try { 75 StatsGenerator gen = new StatsGenerator(true); 76 gen.setMainClass(mainClass); 77 gen.setClasses(classes); 78 gen.setMetaData(metaData); 79 gen.setProgressMonitor(progress); 80 ByteArrayOutputStream out = new ByteArrayOutputStream(); 81 gen.setOutput(new PrintStream(out)); 82 gen.generate(); 83 if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName()); 84 } catch (Exception e) { 85 System.out.println("Problem"); 86 e.printStackTrace(System.out); 87 } 88 } 89 90 void generateSTRUCTS_H(Class [] classes) { 91 try { 92 StructsGenerator gen = new StructsGenerator(true); 93 gen.setMainClass(mainClass); 94 gen.setClasses(classes); 95 gen.setMetaData(metaData); 96 gen.setProgressMonitor(progress); 97 ByteArrayOutputStream out = new ByteArrayOutputStream(); 98 gen.setOutput(new PrintStream(out)); 99 gen.generate(); 100 if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName()); 101 } catch (Exception e) { 102 System.out.println("Problem"); 103 e.printStackTrace(System.out); 104 } 105 106 } 107 108 void generateSTRUCTS_C(Class [] classes) { 109 try { 110 StructsGenerator gen = new StructsGenerator(false); 111 gen.setMainClass(mainClass); 112 gen.setClasses(classes); 113 gen.setMetaData(metaData); 114 gen.setProgressMonitor(progress); 115 ByteArrayOutputStream out = new ByteArrayOutputStream(); 116 gen.setOutput(new PrintStream(out)); 117 gen.generate(); 118 if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName()); 119 } catch (Exception e) { 120 System.out.println("Problem"); 121 e.printStackTrace(System.out); 122 } 123 124 } 125 126 void generateSWT_C(Class [] classes) { 127 try { 128 NativesGenerator gen = new NativesGenerator(); 129 gen.setMainClass(mainClass); 130 gen.setClasses(classes); 131 gen.setMetaData(metaData); 132 gen.setProgressMonitor(progress); 133 ByteArrayOutputStream out = new ByteArrayOutputStream(); 134 gen.setOutput(new PrintStream(out)); 135 gen.generate(); 136 if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName()); 137 } catch (Exception e) { 138 System.out.println("Problem"); 139 e.printStackTrace(System.out); 140 } 141 } 142 143 144 void generateMetaData(Class [] classes) { 145 try { 146 MetaDataGenerator gen = new MetaDataGenerator(); 147 gen.setMainClass(mainClass); 148 gen.setClasses(classes); 149 gen.setMetaData(metaData); 150 gen.setProgressMonitor(progress); 151 ByteArrayOutputStream out = new ByteArrayOutputStream(); 152 gen.setOutput(new PrintStream(out)); 153 gen.generate(); 154 if (!new File(getMetaDataDir()).exists()) { 155 System.out.println("Warning: Meta data output dir does not exist"); 156 return; 157 } 158 if (out.size() > 0) output(out.toByteArray(), getMetaDataDir() + gen.getFileName()); 159 } catch (Exception e) { 160 System.out.println("Problem"); 161 e.printStackTrace(System.out); 162 } 163 } 164 165 public void generate() { 166 generate(null); 167 } 168 169 public void generate(ProgressMonitor progress) { 170 if (mainClassName == null) return; 171 if (progress != null) progress.setMessage("Initializing..."); 172 Class [] classes = getClasses(); 173 Class [] natives = getNativesClasses(); 174 Class [] structs = getStructureClasses(); 175 this.progress = progress; 176 if (progress != null) { 177 int nativeCount = 0; 178 for (int i = 0; i < natives.length; i++) { 179 Class clazz = natives[i]; 180 Method[] methods = clazz.getDeclaredMethods(); 181 for (int j = 0; j < methods.length; j++) { 182 Method method = methods[j]; 183 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue; 184 nativeCount++; 185 } 186 } 187 int total = nativeCount * 4; 188 total += classes.length; 189 total += natives.length * 3; 190 total += structs.length * 2; 191 progress.setTotal(total); 192 progress.setMessage("Generating structs.h ..."); 193 } 194 generateSTRUCTS_H(structs); 195 if (progress != null) progress.setMessage("Generating structs.c ..."); 196 generateSTRUCTS_C(structs); 197 if (progress != null) progress.setMessage("Generating natives ..."); 198 generateSWT_C(natives); 199 if (progress != null) progress.setMessage("Generating stats.h ..."); 200 generateSTATS_H(natives); 201 if (progress != null) progress.setMessage("Generating stats.c ..."); 202 generateSTATS_C(natives); 203 if (progress != null) progress.setMessage("Generating meta data ..."); 204 generateMetaData(classes); 205 if (progress != null) progress.setMessage("Done."); 206 this.progress = null; 207 } 208 209 boolean compare(InputStream is1, InputStream is2) throws IOException { 210 while (true) { 211 int c1 = is1.read(); 212 int c2 = is2.read(); 213 if (c1 != c2) return false; 214 if (c1 == -1) break; 215 } 216 return true; 217 } 218 219 void output(byte[] bytes, String fileName) throws IOException { 220 FileInputStream is = null; 221 try { 222 is = new FileInputStream(fileName); 223 if (compare(new ByteArrayInputStream(bytes), new BufferedInputStream(is))) return; 224 } catch (FileNotFoundException e) { 225 } finally { 226 try { 227 if (is != null) is.close(); 228 } catch (IOException e) {} 229 } 230 FileOutputStream out = new FileOutputStream(fileName); 231 out.write(bytes); 232 out.close(); 233 } 234 235 String getPackageName(String className) { 236 int dot = mainClassName.lastIndexOf('.'); 237 if (dot == -1) return ""; 238 return mainClassName.substring(0, dot); 239 } 240 241 String [] getClassNames(String mainClassName) { 242 String pkgName = getPackageName(mainClassName); 243 String classpath = getClasspath(); 244 if (classpath == null) classpath = System.getProperty("java.class.path"); 245 String pkgPath = pkgName.replace('.', File.separatorChar); 246 String pkgZipPath = pkgName.replace('.', '/'); 247 ArrayList classes = new ArrayList (); 248 int start = 0; 249 int index = 0; 250 while (index < classpath.length()) { 251 index = classpath.indexOf(File.pathSeparatorChar, start); 252 if (index == -1) index = classpath.length(); 253 String path = classpath.substring(start, index); 254 if (path.toLowerCase().endsWith(".jar")) { 255 ZipFile zipFile = null; 256 try { 257 zipFile = new ZipFile(path); 258 Enumeration entries = zipFile.entries(); 259 while (entries.hasMoreElements()) { 260 ZipEntry entry = (ZipEntry)entries.nextElement(); 261 String name = entry.getName(); 262 if (name.startsWith(pkgZipPath) && name.endsWith(".class")) { 263 String className = name.substring(pkgZipPath.length() + 1, name.length() - 6); 264 className.replace('/', '.'); 265 classes.add(className); 266 } 267 } 268 } catch (IOException e) { 269 } finally { 270 try { 271 if (zipFile != null) zipFile.close(); 272 } catch (IOException ex) {} 273 } 274 } else { 275 File file = new File(path + File.separator + pkgPath); 276 if (file.exists()) { 277 String [] entries = file.list(); 278 for (int i = 0; i < entries.length; i++) { 279 String entry = entries[i]; 280 File f = new File(file, entry); 281 if (!f.isDirectory()) { 282 if (f.getAbsolutePath().endsWith(".class")) { 283 String className = entry.substring(0, entry.length() - 6); 284 classes.add(className); 285 } 286 } else { 287 throw new Error ("SUBDIR NOT DONE=" + f); 288 } 289 } 290 } 291 } 292 start = index + 1; 293 } 294 return (String [])classes.toArray(new String [classes.size()]); 295 } 296 297 public Class [] getClasses() { 298 if (mainClassName == null) return new Class [0]; 299 String [] classNames = getClassNames(mainClassName); 300 Arrays.sort(classNames); 301 String packageName = getPackageName(mainClassName); 302 Class [] classes = new Class [classNames.length]; 303 for (int i = 0; i < classNames.length; i++) { 304 String className = classNames[i]; 305 try { 306 classes[i] = Class.forName(packageName + "." + className, false, getClass().getClassLoader()); 307 } catch (Exception e) { 308 e.printStackTrace(); 309 } 310 } 311 return classes; 312 } 313 314 public Class [] getNativesClasses() { 315 if (mainClassName == null) return new Class [0]; 316 ArrayList result = new ArrayList (); 317 Class [] classes = getClasses(); 318 for (int i = 0; i < classes.length; i++) { 319 Class clazz = classes[i]; 320 Method[] methods = clazz.getDeclaredMethods(); 321 for (int j = 0; j < methods.length; j++) { 322 Method method = methods[j]; 323 int mods = method.getModifiers(); 324 if ((mods & Modifier.NATIVE) != 0) { 325 result.add(clazz); 326 break; 327 } 328 } 329 } 330 return (Class [])result.toArray(new Class [result.size()]); 331 } 332 333 public Class [] getStructureClasses() { 334 if (mainClassName == null) return new Class [0]; 335 ArrayList result = new ArrayList (); 336 Class [] classes = getClasses(); 337 outer: 338 for (int i = 0; i < classes.length; i++) { 339 Class clazz = classes[i]; 340 Method[] methods = clazz.getDeclaredMethods(); 341 for (int j = 0; j < methods.length; j++) { 342 Method method = methods[j]; 343 int mods = method.getModifiers(); 344 if ((mods & Modifier.NATIVE) != 0) continue outer; 345 } 346 Field[] fields = clazz.getFields(); 347 boolean hasPublicFields = false; 348 for (int j = 0; j < fields.length; j++) { 349 Field field = fields[j]; 350 int mods = field.getModifiers(); 351 if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) == 0) { 352 hasPublicFields = true; 353 break; 354 } 355 } 356 if (!hasPublicFields) continue; 357 result.add(clazz); 358 } 359 return (Class [])result.toArray(new Class [result.size()]); 360 } 361 362 public void setClasspath(String classpath) { 363 this.classpath = classpath; 364 } 365 366 public void setMainClassName(String str) { 367 mainClassName = str; 368 metaData = new MetaData(mainClassName); 369 String mainClasses = getMetaData().getMetaData("swt_main_classes", null); 370 if (mainClasses != null) { 371 String [] list = ItemData.split(mainClasses, ","); 372 for (int i = 0; i < list.length; i += 2) { 373 if (mainClassName.equals(list[i].trim())) { 374 setOutputDir(list[i + 1].trim()); 375 } 376 } 377 } 378 if (mainClassName != null) { 379 try { 380 mainClass = Class.forName(mainClassName, false, getClass().getClassLoader()); 381 } catch (Exception e) { 382 e.printStackTrace(); 383 } 384 } 385 } 386 387 public void setOutputDir(String str) { 388 if (str != null) { 389 if (!str.endsWith("\\") && !str.endsWith("/") ) { 390 str += File.separator; 391 } 392 } 393 outputDir = str; 394 } 395 396 public static String getDefaultMainClass() { 397 return "org.eclipse.swt.internal." + getDefaultPlatform() + ".OS"; 398 } 399 400 public static String getDefaultPlatform() { 401 return SWT.getPlatform(); 402 } 403 404 public static void main(String [] args) { 405 JNIGeneratorApp gen = new JNIGeneratorApp (); 406 if (args.length > 0) { 407 gen.setMainClassName(args[0]); 408 if (args.length > 1) gen.setOutputDir(args[1]); 409 if (args.length > 2) gen.setClasspath(args[2]); 410 } else { 411 gen.setMainClassName(getDefaultMainClass()); 412 } 413 gen.generate(); 414 } 415 416 } 417 | Popular Tags |