Advertisement
ulysses4ever

scan-jar

Feb 16th, 2012
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.19 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.jar.*;
  4. import java.util.zip.*;
  5.  
  6. public class JavaApplication1 {
  7.    
  8.     private static class MyClassLoader extends ClassLoader {
  9.  
  10.         public Class<?> myDefineClass(byte[] b, int off, int len) {
  11.             return defineClass(b, off, len);
  12.         }
  13.     }
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.         Scanner sc = new Scanner(System.in);
  20.         String path = sc.nextLine();
  21.         byte[] buf = new byte[1024*1024*50];
  22.         MyClassLoader loader = new MyClassLoader();
  23.         int cls = 0, intfs = 0, enums = 0;
  24.        
  25.         try {
  26.             ZipInputStream zis = new ZipInputStream(new FileInputStream(path));
  27.            
  28.             while (true) {
  29.                 ZipEntry en = zis.getNextEntry();
  30.                 //System.out.println("entry: " + en);
  31.                 if (null == en)
  32.                     break;
  33.                 if (en.toString() == null ||
  34.                         !en.toString().endsWith("class"))
  35.                     continue;
  36.                 int bytesRead = 0;
  37.                 while (true) {
  38.                     int lastRead = zis.read(buf, bytesRead, 1024);
  39.                     if (-1 == lastRead)
  40.                         break;
  41.                     bytesRead += lastRead;
  42.                 }
  43.  
  44.                 try {
  45.                     Class c = loader.myDefineClass(buf, 0, bytesRead);
  46.                     if (c.isInterface())
  47.                         ++intfs;
  48.                     else if (c.isEnum())
  49.                         ++enums;
  50.                     else
  51.                         ++cls;
  52.                 }
  53.                 catch (Error e) {
  54.                     //System.out.println("!!!");
  55.                     //e.printStackTrace();
  56.                 }
  57.                 //System.out.println("a class defined!");
  58.                
  59.             }
  60.         } catch (Exception ex) {
  61.             //System.out.println("???");
  62.             //ex.printStackTrace();
  63.             cls = -1;
  64.         }
  65.        
  66.         System.out.print(cls);
  67.         if(cls != -1)
  68.             System.out.print(" " + intfs + " " + enums);
  69.     }        
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement