美文网首页
2019-08-05Java的class文件版本检查

2019-08-05Java的class文件版本检查

作者: 捕风的逍遥侯 | 来源:发表于2019-08-05 15:14 被阅读0次



import java.io.DataInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Verify that a set of jar files are compiled for Java 5.0, the release
 * standard for FindBugs
 *
 */
public class CheckClassfileVersion {

    private static boolean isJarFile(File f) {
        String name = f.getName();
        return name.endsWith(".jar") || name.endsWith(".zip") || name.endsWith("war") || name.endsWith(".ear");
    }
    /**
     *
     * @see 只能处理jar包
     * @param args
     * @throws Exception
     */

    public static void main(String args[]) throws Exception {

        boolean fail = false;
        ArrayList<File> s = new ArrayList<File>(args.length);
        for (String f : args) {
            File file = new File(f);
            if (!file.canRead()) {
                System.out.println("Can't read " + f);
            }
            if (file.isDirectory()) {
                for (File f2 : file.listFiles()) {
                    if (isJarFile(f2)) {
                        s.add(f2);
                    }
                }
            } else if (isJarFile(file)) {
                s.add(file);
            }else {
                System.out.println("not a jar or dir");
            }
        }

        for (File jarFile : s) {
            String jarFileName = jarFile.getName();
            System.out.println("Checking " + jarFileName);
            try(JarFile z = new JarFile(jarFile)){
                for (Enumeration<JarEntry> e = z.entries(); e.hasMoreElements();) {
                    JarEntry ze = e.nextElement();
                    if (ze.isDirectory()) {
                        continue;
                    }

                    String name = ze.getName();
                    boolean isClassFile = name.endsWith(".class");
                    if (!isClassFile) {
                        continue;
                    }

                    DataInputStream zipIn = new DataInputStream(z.getInputStream(ze));
                    int magic = zipIn.readInt();//读取的是十六进制的前面八位
                    int minorVersion = zipIn.readUnsignedShort();//紧接着读取四位
                    int majorVersion = zipIn.readUnsignedShort();//再读取四位(jdk版本号)
                    System.out.printf("magic %s ,minorVersion %s,majorVersion:%s \n",0xCAFEBABE,minorVersion,majorVersion);

                    if (magic != 0xCAFEBABE) {
                        System.out.printf("bad magic %x: %s %s%n", magic, jarFileName, name);
                        fail = true;
                    } else if (minorVersion >= 60) {
                        System.out.printf("bad version %d:%s %s%n", minorVersion, jarFileName, name);
                        fail = true;
                    }

                    switch (majorVersion) {
                    case 45:
                        System.out.printf("%s is jdk 1.1\n",name);
                        break;
                    case 46:
                        System.out.printf("%s is jdk 1.2\n",name);
                        break;
                    case 47:
                        System.out.printf("%s is jdk 1.3\n",name);
                        break;
                    case 48:
                        System.out.printf("%s is jdk 1.4\n",name);
                        break;
                    case 49:
                        System.out.printf("%s is jdk 1.5\n",name);
                        break;
                    case 50:
                        System.out.printf("%s is jdk 1.6\n",name);
                        break;
                    case 51:
                        System.out.printf("%s is jdk 1.7\n",name);
                        break;
                    case 52:
                        System.out.printf("%s is jdk 1.8\n",name);
                        break;
                    case 53:
                        System.out.printf("%s is jdk 9\n",name);
                        break;
                    case 54:
                        System.out.printf("%s is jdk 10\n",name);
                        break;
                    case 55:
                        System.out.printf("%s is jdk 11\n",name);
                        break;
                    case 56:
                        System.out.printf("%s is jdk 12\n",name);
                        break;
                    case 57:
                        System.out.printf("%s is jdk 13\n",name);
                        break;

                    default:
                        break;
                    }

                    zipIn.close();
                }
            }
        }
        if (fail) {
            System.exit(1);
        }
    }

}



相关文章

网友评论

      本文标题:2019-08-05Java的class文件版本检查

      本文链接:https://www.haomeiwen.com/subject/qvsmdctx.html