Merge branch 'improvement/JAL-1988+JAL-3416_Java8_macOS_APQHandlers_and_FlatLaF_optio...
[jalview.git] / utils / getJavaVersion.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 import java.io.DataInputStream;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.jar.JarEntry;
27 import java.util.jar.JarInputStream;
28
29 public class getJavaVersion
30 {
31   /**
32    * Takes a set of Jars and/or class files as arguments. Reports the java
33    * version for the classes
34    */
35
36   public static void main(String[] args) throws IOException
37   {
38     Hashtable observed = new Hashtable();
39     for (int i = 0; i < args.length; i++)
40     {
41       checkClassVersion(args[i], observed);
42     }
43     printVersions(observed, System.out);
44   }
45
46   public static void printVersions(Hashtable observed,
47           java.io.PrintStream outs)
48   {
49     if (observed.size() > 0)
50     {
51       int space = 0;
52       String key = null;
53       for (Enumeration keys = observed.keys(); keys.hasMoreElements();)
54       {
55         key = (String) keys.nextElement();
56         if (space++ > 0)
57         {
58           outs.print(" ");
59         }
60         outs.print(key);
61       }
62       outs.print("\n");
63     }
64   }
65
66   private static void checkClassVersion(String filename, Hashtable observed)
67           throws IOException
68   {
69     String version = checkClassVersion(filename);
70     if (version == null)
71     {
72       // System.err.println("Reading "+filename+" as  jar:");
73       try
74       {
75         JarInputStream jis = new JarInputStream(new FileInputStream(
76                 filename));
77         JarEntry entry;
78         Hashtable perjar = new Hashtable();
79         while ((entry = jis.getNextJarEntry()) != null)
80         {
81           if (entry != null)
82           {
83             if (entry.getName().endsWith(".class"))
84             {
85               try
86               {
87                 version = getVersion(new DataInputStream(jis));
88                 if (version != null)
89                 {
90                   addVersion(version, observed);
91                   addVersion(version, perjar);
92                 }
93               } catch (Exception e)
94               {
95
96               }
97             }
98           }
99         }
100         System.err.println("Jar : " + filename);
101         printVersions(perjar, System.err);
102       } catch (Exception e)
103       {
104
105       }
106     }
107     else
108     {
109       addVersion(version, observed);
110     }
111   }
112
113   private static void addVersion(String version, Hashtable observed)
114   {
115     if (version != null)
116     {
117       // System.err.println("Version is '"+version+"'");
118       int[] vrs = (int[]) observed.get(version);
119       if (vrs == null)
120       {
121         vrs = new int[] { 0 };
122       }
123       vrs[0]++;
124       observed.put(version, vrs);
125     }
126   }
127
128   private static String checkClassVersion(String filename)
129           throws IOException
130   {
131     DataInputStream in = new DataInputStream(new FileInputStream(filename));
132     return getVersion(in);
133   }
134
135   private static Hashtable versions = null;
136
137   private static String parseVersions(int minor, int major)
138   {
139     if (versions == null)
140     {
141       versions = new Hashtable();
142       versions.put("45.3", "1.0.2");
143       versions.put("45.3", "1.1");
144       versions.put("46.0", "1.2");
145       versions.put("47.0", "1.3");
146       versions.put("48.0", "1.4");
147       versions.put("49.0", "1.5");
148       versions.put("50.0", "1.6");
149       versions.put("51.0", "1.7");
150       versions.put("52.0", "1.8");
151       versions.put("53.0", "9");
152       versions.put("54.0", "10");
153       versions.put("55.0", "11");
154
155     }
156     String version = (String) versions.get(major + "." + minor);
157     if (version == null)
158     {
159       // get nearest known version
160       version = (String) versions.get(major + ".0");
161     }
162     // System.err.println("Version "+version);
163     if (version == null)
164     {
165       versions.put(major + "." + minor, "Class v" + major + ".0");
166     }
167     return version;
168   }
169
170   private static String getVersion(DataInputStream in) throws IOException
171   {
172     int magic = in.readInt();
173     if (magic != 0xcafebabe)
174     {
175       return null;
176     }
177     int minor = in.readUnsignedShort();
178     int major = in.readUnsignedShort();
179     // System.err.println("Version "+major+"."+minor);
180     return parseVersions(minor, major);
181   }
182
183 }