1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 // The Main Entry point
  24 package sun.tools.pack.verify;
  25 
  26 import java.io.*;
  27 
  28 /**
  29  * This class provides a convenient entry point to the pack200 verifier. This
  30  * compares two classes, either in path or in an archive.
  31  * @see xmlkit.XMLKit
  32  * @author ksrini
  33  */
  34 public class Main {
  35 
  36     private static void syntax() {
  37         System.out.println("Usage: ");
  38         System.out.println("\tREFERENCE_CLASSPATH COMPARED_CLASSPATH [Options]");
  39         System.out.println("\tOptions:");
  40         System.out.println("\t\t-O check jar ordering");
  41         System.out.println("\t\t-C ignore compile attributes (Deprecated, SourceFile, Synthetic, )");
  42         System.out.println("\t\t-D ignore debug attributes (LocalVariable, LineNumber)");
  43         System.out.println("\t\t-u ignore unknown attributes");
  44         System.out.println("\t\t-V turn off class validation");
  45         System.out.println("\t\t-c CLASS, compare CLASS only");
  46         System.out.println("\t\t-b Compares all entries bitwise only");
  47         System.out.println("\t\t-l Directory or Log File Name");
  48     }
  49 
  50     /**
  51      * main entry point to the class file comparator, which compares semantically
  52      * class files in a classpath or an archive.
  53      * @param args String array as described below
  54      * @throws RuntimeException
  55      * <pre>
  56      *  Usage:
  57      *     ReferenceClasspath SpecimenClaspath [Options]
  58      *     Options:
  59      *      -O check jar ordering
  60      *      -C do not compare compile attributes (Deprecated, SourceFile, Synthetic)
  61      *      -D do not compare debug attribute (LocalVariableTable, LineNumberTable)
  62      *      -u ignore unknown attributes
  63      *      -V turn off class validation
  64      *      -c class, compare a single class
  65      *      -b compares all entries bitwise (fastest)
  66      *      -l directory or log file name
  67      * </pre>
  68      */
  69     public static void main(String args[]) {
  70         Globals.getInstance();
  71         if (args == null || args.length < 2) {
  72             syntax();
  73             System.exit(1);
  74         }
  75         String refJarFileName = null;
  76         String cmpJarFileName = null;
  77         String specificClass = null;
  78         String logDirFileName = null;
  79 
  80         for (int i = 0; i < args.length; i++) {
  81             if (i == 0) {
  82                 refJarFileName = args[0];
  83                 continue;
  84             }
  85             if (i == 1) {
  86                 cmpJarFileName = args[1];
  87                 continue;
  88             }
  89 
  90             if (args[i].startsWith("-O")) {
  91                 Globals.setCheckJarClassOrdering(true);
  92             }
  93 
  94             if (args[i].startsWith("-b")) {
  95                 Globals.setBitWiseClassCompare(true);
  96             }
  97 
  98             if (args[i].startsWith("-C")) {
  99                 Globals.setIgnoreCompileAttributes(true);
 100             }
 101 
 102             if (args[i].startsWith("-D")) {
 103                 Globals.setIgnoreDebugAttributes(true);
 104             }
 105 
 106             if (args[i].startsWith("-V")) {
 107                 Globals.setValidateClass(false);
 108             }
 109 
 110             if (args[i].startsWith("-c")) {
 111                 i++;
 112                 specificClass = args[i].trim();
 113             }
 114 
 115             if (args[i].startsWith("-u")) {
 116                 i++;
 117                 Globals.setIgnoreUnknownAttributes(true);
 118             }
 119 
 120             if (args[i].startsWith("-l")) {
 121                 i++;
 122                 logDirFileName = args[i].trim();
 123             }
 124         }
 125 
 126         Globals.openLog(logDirFileName);
 127 
 128         File refJarFile = new File(refJarFileName);
 129         File cmpJarFile = new File(cmpJarFileName);
 130 
 131         String f1 = refJarFile.getAbsoluteFile().toString();
 132         String f2 = cmpJarFile.getAbsoluteFile().toString();
 133 
 134         System.out.println("LogFile:" + Globals.getLogFileName());
 135         System.out.println("Reference JAR:" + f1);
 136         System.out.println("Compared  JAR:" + f2);
 137 
 138         Globals.println("LogFile:" + Globals.getLogFileName());
 139         Globals.println("Reference JAR:" + f1);
 140         Globals.println("Compared  JAR:" + f2);
 141 
 142         Globals.println("Ignore Compile Attributes:" + Globals.ignoreCompileAttributes());
 143         Globals.println("Ignore Debug   Attributes:" + Globals.ignoreDebugAttributes());
 144         Globals.println("Ignore Unknown Attributes:" + Globals.ignoreUnknownAttributes());
 145         Globals.println("Class ordering check:" + Globals.checkJarClassOrdering());
 146         Globals.println("Class validation check:" + Globals.validateClass());
 147         Globals.println("Bit-wise compare:" + Globals.bitWiseClassCompare());
 148         Globals.println("ClassName:" + ((specificClass == null) ? "ALL" : specificClass));
 149 
 150         if (specificClass == null && Globals.bitWiseClassCompare() == true) {
 151             JarFileCompare.jarCompare(refJarFileName, cmpJarFileName);
 152         } else {
 153             try {
 154                 ClassCompare.compareClass(refJarFileName, cmpJarFileName, specificClass);
 155             } catch (Exception e) {
 156                 Globals.log("Exception " + e);
 157                 throw new RuntimeException(e);
 158             }
 159         }
 160 
 161         if (Globals.getErrors() > 0) {
 162             System.out.println("FAIL");
 163             Globals.println("FAIL");
 164             System.exit(Globals.getErrors());
 165         }
 166 
 167         System.out.println("PASS");
 168         Globals.println("PASS");
 169         System.exit(Globals.getErrors());
 170     }
 171 }