You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/11/02 20:49:16 UTC

svn commit: r1767781 - /uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java

Author: schor
Date: Wed Nov  2 20:49:16 2016
New Revision: 1767781

URL: http://svn.apache.org/viewvc?rev=1767781&view=rev
Log:
[UIMA-4518] minor updates to migrate v3 jcas conversion utility

Modified:
    uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java

Modified: uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java
URL: http://svn.apache.org/viewvc/uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java?rev=1767781&r1=1767780&r2=1767781&view=diff
==============================================================================
--- uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java (original)
+++ uima/uimaj/uimaj-v3migration-jcas/src/main/java/org/apache/uima/migratev3/jcas/MigrateJCas.java Wed Nov  2 20:49:16 2016
@@ -189,13 +189,14 @@ public class MigrateJCas extends VoidVis
   private String outDirSkipped;
   /** includes trailing / */
   private String outDirLog;
-  
-  
-  private String[] roots;
+   
+  private String[] sourcesRoots = new String[0];
+  private String[] classesRoots = new String[0];
   
   private CompilationUnit cu;
   
-  private ClassLoader migrateClassLoader = null;
+  // save this value in the class instance to avoid recomputing it
+  private ClassLoader cachedMigrateClassLoader = null;
   
   private String migrateClasspath = null;
   
@@ -235,10 +236,7 @@ public class MigrateJCas extends VoidVis
   private final List<PathAndReason> nonJCasFiles = new ArrayList<>();  // path, reason
   private final List<PathAndReason> failedMigration = new ArrayList<>(); // path, reason
   private final List<ClassnameAndPath> c2ps = new ArrayList<>();            // class, path
-  private final List<ClassnameAndPath> extendableBuiltins = new ArrayList<>();  // class, path
-  private final List<ClassnameAndPath> nonExtendableBuiltins = new ArrayList<>();  // class, path
-//  private final List<ClassnameAndPath> nonIdenticalDuplicates = new ArrayList<>();  // class, path
-//  private final List<ClassnameAndPath> identicalDuplicates = new ArrayList<>();  // class, path
+  private final List<ClassnameAndPath> skippedBuiltins = new ArrayList<>();  // class, path
   private final List<PathAndReason> deletedCheckModified = new ArrayList<>();  // path, deleted check string
   private final List<String1AndString2> pathWorkaround = new ArrayList<>(); // original, workaround
   private final List<PathAndReason> manualInspection = new ArrayList<>(); // path, reason
@@ -292,7 +290,65 @@ public class MigrateJCas extends VoidVis
     (new MigrateJCas()).run(args);
   }
   
+  /***********************************
+   * Main
+   * @param args -
+   ***********************************/
   void run(String[] args) {
+    CommandLineParser clp = parseCommandArgs(args);
+    
+    // clear output dir
+    FileUtils.deleteRecursive(new File(outputDirectory));
+    
+    isSource = true;
+    processRootsCollection("source", sourcesRoots, clp);
+    
+    isSource = false;
+    processRootsCollection("classes", classesRoots, clp);
+    
+    if (individualClasses != null) {
+      processCollection("individual classes: ", new Iterator<String>() {
+        Iterator<String> it = Arrays.asList(individualClasses.split(File.pathSeparator)).iterator();
+        public boolean hasNext() {return it.hasNext();}
+        public String next() {
+          return prepareIndividual(it.next());}
+      });
+    }
+
+    report();
+  }
+
+  private void processRootsCollection(String kind, String[] roots, CommandLineParser clp) {
+    for (String root : roots) {
+      processCollection("from " + kind + "  root: " + root, new Iterator<String>() { 
+        Iterator<Path> it = getCandidates(clp, root).iterator();
+        public boolean hasNext() {return it.hasNext();}
+        public String next() {
+          return prepare(it.next());}
+      });
+    }
+  }
+  
+  
+  private void processCollection(String sourceName, Iterator<String> sourceIterator) {
+    System.out.println("Migrating " + sourceName + ", number of classes migrated:");
+    int i = 1;
+    System.out.print("    ");
+    
+    while (sourceIterator.hasNext()) {
+      migrate(sourceIterator.next());
+      if ((i % 50) == 0) System.out.format("%4d%s", Integer.valueOf(i), "\r");
+      i++;
+    }
+    System.out.format("%4d%n", Integer.valueOf(i));   
+  }
+  
+  /**
+   * parse command line args
+   * @param args - 
+   * @return the CommandLineParser instance
+   */
+  private CommandLineParser parseCommandArgs(String[] args) {
     CommandLineParser clp = createCmdLineParser();
     try {
       clp.parseCmdLine(args);
@@ -305,47 +361,28 @@ public class MigrateJCas extends VoidVis
     }
     
     if (clp.isInArgsList(CLASS_FILE_ROOTS)) {
-      roots = getRoots(clp, CLASS_FILE_ROOTS);
-      isSource = false;
-    } else if (clp.isInArgsList(SOURCE_FILE_ROOTS)) {
-      roots = getRoots(clp, SOURCE_FILE_ROOTS);
-      isSource = true;
-    } else {
-      printUsage();
-      System.exit(2);
+      classesRoots = getRoots(clp, CLASS_FILE_ROOTS);
     }
     
-    // clear output dir
-    FileUtils.deleteRecursive(new File(outputDirectory));
-    
-    for (String root : roots) {
-      System.out.println("Migrating from root: " + root);
-      int i = 1;
-      System.out.print("    ");
-      for (Path c : getCandidates(clp, root)) {
-        String source = prepare(c);
-        migrate(source);
-        if ((i % 50) == 0) System.out.format("%n%4d", Integer.valueOf(i));
-        i++;
-      }
-      System.out.println("");
+    if (clp.isInArgsList(SOURCE_FILE_ROOTS)) {
+      sourcesRoots = getRoots(clp, SOURCE_FILE_ROOTS);
     }
     
-    if (individualClasses != null) {
-      String[] cn = individualClasses.split(File.pathSeparator);
-      System.out.println("Migrating individual classes: " );
-      int i = 1;
-      System.out.print("    ");
-      for (String classname : cn) {
-        String source = prepareIndividual(classname);
-        migrate(source);
-        if ((i % 50) == 0) System.out.format("%n%4d", Integer.valueOf(i));
-        i++;
-      }
-      System.out.println("");
+    return clp;
+  }
+  
+  /***************
+   * set roots
+   * set isSource
+   ***************/
+  private void setRootsAndSource(CommandLineParser clp) {
+    if (clp.isInArgsList(CLASS_FILE_ROOTS)) {
+      classesRoots = getRoots(clp, CLASS_FILE_ROOTS);
     }
     
-    report();
+    if (clp.isInArgsList(SOURCE_FILE_ROOTS)) {
+      sourcesRoots = getRoots(clp, SOURCE_FILE_ROOTS);
+    }
   }
   
   private String[] getRoots(CommandLineParser clp, String kind) {
@@ -486,7 +523,7 @@ public class MigrateJCas extends VoidVis
    */
   
   private boolean collectInfoForReports() {
-    String fqcn = (alreadyDone == null) 
+    String fqcn = (alreadyDone == null)    // fully qualified class name with slashes
                     ? packageAndClassNameSlash
                     : alreadyDone;
     // for a given fully qualified class name (slashified), 
@@ -927,23 +964,21 @@ public class MigrateJCas extends VoidVis
    *
    */
   private void report() {
-    System.out.format("%n%nMigration Summary.  Using input from %s files%n", isSource ? ".java" : ".class");
+    System.out.println("\n\nMigration Summary");
     System.out.format("Output top directory: %s%n", outputDirectory);
     System.out.format("Date/time: %tc%n", new Date());
-    System.out.println("  Roots:");
-    for (String s : roots) {
-      System.out.format("    %s%n",  s);
-    }
-    System.out.println("\n");
+    pprintRoots("Sources", sourcesRoots);
+    pprintRoots("Classes", classesRoots);
+
    
     try {
-      reportPaths("Workaround Directories", "workaroundDir", pathWorkaround);
-      reportPaths("Report of converted files where a deleted check was customized", "deletedCheckModified", deletedCheckModified);
-      reportPaths("Report of converted files needing manual inspection", "manualInspection", manualInspection);
+      reportPaths("Workaround Directories", "workaroundDir.txt", pathWorkaround);
+      reportPaths("Report of converted files where a deleted check was customized", "deletedCheckModified.txt", deletedCheckModified);
+      reportPaths("Report of converted files needing manual inspection", "manualInspection.txt", manualInspection);
       reportPaths("Report of files which failed migration", "failed.txt", failedMigration);
       reportPaths("Report of non-JCas files", "NonJCasFiles.txt", nonJCasFiles);
-      reportPaths("Builtin Extendable JCas classes - skipped - need manual checking to see if they are modified",
-          "extendableBuiltins.txt", extendableBuiltins);
+      reportPaths("Builtin JCas classes - skipped - need manual checking to see if they are modified",
+          "skippedBuiltins.txt", skippedBuiltins);
       
 //      computeDuplicates();
 //      reportPaths("Report of duplicates - not identical", "nonIdenticalDuplicates.txt", nonIdenticalDuplicates);
@@ -951,11 +986,20 @@ public class MigrateJCas extends VoidVis
       reportDuplicates();
       
       reportPaths("Report of processed files", "processed.txt", c2ps);
-      reportPaths("Builtin non-Extendable JCas classes - skipped", "builtinsNotExtendable.txt", nonExtendableBuiltins);
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
+
+  private void pprintRoots(String kind, String[] roots) {
+    if (roots.length > 0) {
+      System.out.println(kind + " Roots:");
+      for (String s : roots) {
+        System.out.format("    %s%n",  s);
+      }
+      System.out.println("\n");
+    }
+  }
   
 //  private void computeDuplicates() {
 //    List<ClassnameAndPath> toCheck = new ArrayList<>(c2ps);
@@ -1076,7 +1120,7 @@ public class MigrateJCas extends VoidVis
     }
   }
   
-  private void reportPaths(String title, String fileName, List<? extends Report2> items) throws IOException {
+  private <T, U> void reportPaths(String title, String fileName, List<? extends Report2<T, U>> items) throws IOException {
     if (items.size() == 0) {
       System.out.println("No " + title);
       return;  
@@ -1086,16 +1130,16 @@ public class MigrateJCas extends VoidVis
     System.out.println("");
     
     try (BufferedWriter bw = Files.newBufferedWriter(makePath(outDirLog + fileName), StandardOpenOption.CREATE)) {
-      List<Report2> sorted = new ArrayList<>(items);
+      List<Report2<T, U>> sorted = new ArrayList<>(items);
 
       sortReport2(sorted);  
       int max = 0;
-      for (Report2 p : sorted) {
+      for (Report2<T, U> p : sorted) {
         max = Math.max(max, p.getFirstLength());
       }
       
       int i = 1;
-      for (Report2 p : sorted) {
+      for (Report2<T, U> p : sorted) {
         Object v = p.getSecond();
         String s = String.format("%5d %-" +max+ "s %s%n", Integer.valueOf(i), p.getFirst(), vWithFileSys(v));
         bw.write(s);
@@ -1117,12 +1161,12 @@ public class MigrateJCas extends VoidVis
     return v.toString();
   }
   
-  private void sortReport2(List<? extends Report2> items) {
+  private <T, U> void sortReport2(List<? extends Report2<T, U>> items) {
     Collections.sort(items, 
         (o1, o2) -> {
-          int r = o1.getFirst().compareTo(o2.getFirst());
+          int r = o1.getFirst().compareTo((T) o2.getFirst());
           if (r == 0) {
-            r = o1.getSecond().compareTo(o2.getSecond());
+            r = o1.getSecond().compareTo((U) o2.getSecond());
           }
           return r;
         });
@@ -1371,12 +1415,12 @@ public class MigrateJCas extends VoidVis
 //  }
   
   private ClassLoader getClassLoader() {
-    if (null == migrateClassLoader) {
-      migrateClassLoader = (migrateClasspath == null) 
+    if (null == cachedMigrateClassLoader) {
+      cachedMigrateClassLoader = (migrateClasspath == null) 
                       ? this.getClass().getClassLoader()
                       : new URLClassLoader(Misc.classpath2urls(migrateClasspath));
     }
-    return migrateClassLoader;
+    return cachedMigrateClassLoader;
   }
   
   private void addImport(String s) {
@@ -1563,11 +1607,7 @@ public class MigrateJCas extends VoidVis
       if (null != ti) {
         // is a built-in type
         ClassnameAndPath p = new ClassnameAndPath(packageAndClassNameSlash, candidate);
-        if (!ti.isFeatureFinal()) {
-          extendableBuiltins.add(p);
-        } else {
-          nonExtendableBuiltins.add(p);
-        }
+        skippedBuiltins.add(p);
         v3 = false;   // skip further processing of this class
         return;  
       }
@@ -1709,10 +1749,11 @@ public class MigrateJCas extends VoidVis
         "Usage: java org.apache.uima.migratev3.jcas.MigrateJCas \n"
         + "  [-sourcesRoots <One-or-more-directories-or-jars-separated-by-Path-separator>]\n"
         + "  [-classesRoots <One-or-more-directories-or-jars-separated-by-Path-separator>]\n"
+        + "  [-classes <one-or-more-fully-qualified-class-names-separated-by-Path-separator]\n"
+        + "            example:  -classes mypkg.foo:pkg2.bar\n"
         + "  [-outputDirectory a-writable-directory-path (required)\n"
-        + "  [-migrateClasspath a-class-path to use in decompiling, required if -classesRoots is specified, or \n"
-        + "                        to provide disambiguation information when -sourcesRoots is specified\n"
-        + "                        and more than one instance of a class is found in the roots]\n"
+        + "  [-migrateClasspath a-class-path to use in decompiling, required if -classesRoots is specified, or\n"
+        + "                     "
         + "  [-skipTypeCheck if specified, skips validing a found item by looking for the corresponding _Type file"
         + "  NOTE: either -sourcesRoots or -classesRoots is required, but only one may be specified.\n"
         + "  NOTE: classesRoots are scanned for JCas classes, which are then decompiled, and the results processed like sourcesRoots\n"
@@ -1765,13 +1806,13 @@ public class MigrateJCas extends VoidVis
    * Reporting classes
    *********************************************************************/
   
-  private static abstract class Report2 {
-    public abstract Comparable getFirst(); // Eclipse on linux complained if not public, was OK on windows
-    public abstract Comparable getSecond();  
+  private static abstract class Report2<T, U> {
+    public abstract Comparable<T> getFirst(); // Eclipse on linux complained if not public, was OK on windows
+    public abstract Comparable<U> getSecond();  
     abstract int getFirstLength();
   }
   
-  private static class PathAndReason extends Report2 {
+  private static class PathAndReason extends Report2<Path, String> {
     Path path;
     String reason;
     PathAndReason(Path path, String reason) {
@@ -1786,7 +1827,7 @@ public class MigrateJCas extends VoidVis
     int getFirstLength() { return path.toString().length(); }
   }
   
-  private static class ClassnameAndPath extends Report2 {
+  private static class ClassnameAndPath extends Report2<String, Path> {
     String classname;
     Path path;
     ClassnameAndPath(String classname, Path path) {
@@ -1830,7 +1871,7 @@ public class MigrateJCas extends VoidVis
     }
   }
   
-  private static class String1AndString2 extends Report2 {
+  private static class String1AndString2 extends Report2<String, String> {
     String s1;
     String s2;
     String1AndString2(String s1, String s2) {