Skip to content

Commit 3859653

Browse files
committed
Initial code commit.
1 parent f89af67 commit 3859653

File tree

5 files changed

+407
-0
lines changed

5 files changed

+407
-0
lines changed

.project

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>classversion-scanner</name>
4+
<comment/>
5+
<projects/>
6+
<natures>
7+
<nature>org.eclipse.jdt.core.javanature</nature>
8+
</natures>
9+
<buildSpec>
10+
<buildCommand>
11+
<name>org.eclipse.jdt.core.javabuilder</name>
12+
<arguments/>
13+
</buildCommand>
14+
</buildSpec>
15+
<linkedResources/>
16+
</projectDescription>

build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
apply plugin: 'java'
3+
apply plugin: 'eclipse'
4+
apply plugin: 'application'
5+
6+
mainClassName = "com.netmikey.cvscanner.ClassVersionScanner"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
compile (group: 'commons-cli', name: 'commons-cli', version: '1.2')
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.netmikey.cvscanner;
2+
3+
import java.io.File;
4+
import java.util.logging.Level;
5+
6+
import org.apache.commons.cli.CommandLine;
7+
import org.apache.commons.cli.CommandLineParser;
8+
import org.apache.commons.cli.HelpFormatter;
9+
import org.apache.commons.cli.Options;
10+
import org.apache.commons.cli.PosixParser;
11+
12+
/**
13+
* Main class for running the Class Version Scanner.
14+
*
15+
* @author netmikey
16+
*/
17+
public class ClassVersionScanner {
18+
/**
19+
* Main method.
20+
*
21+
* @param argv
22+
* Command-Line arguments.
23+
* @throws Exception
24+
* Something went horribly wrong.
25+
*/
26+
public static void main(String[] argv) throws Exception {
27+
// Command line stuff...
28+
Options opt = new Options();
29+
opt.addOption("d", "dir", true, "the root directory from which to search for class files and java archives "
30+
+ "(if not set, the current working directory will be used).");
31+
opt.addOption("n", "newer", true, "only look for class files compiled for the specified JRE and newer");
32+
opt.addOption("o", "older", true, "only look for class files compiled for the specified JRE and older");
33+
// I know java logging can be configured in an "awesome" *cough* way,
34+
// but srsly, nobody wants to do that for a little command line tool
35+
// like this...
36+
opt.addOption("v", "verbose", false, "display more processing info");
37+
opt.addOption("h", "help", false, "display this help info");
38+
39+
CommandLineParser parser = new PosixParser();
40+
CommandLine cmd = parser.parse(opt, argv);
41+
42+
if (cmd.hasOption("help")) {
43+
HelpFormatter formatter = new HelpFormatter();
44+
formatter.printHelp(ClassVersionScanner.class.getName(), opt);
45+
return;
46+
}
47+
48+
Scanner scanner = new Scanner();
49+
File baseDir = new File(cmd.hasOption("dir") ? cmd.getOptionValue("dir") : System.getProperty("user.dir"));
50+
scanner.setMinVersion(Scanner.VERSIONS.get(cmd.getOptionValue("newer")));
51+
scanner.setMaxVersion(Scanner.VERSIONS.get(cmd.getOptionValue("older")));
52+
if (cmd.hasOption("verbose")) {
53+
scanner.setFineLevel(Level.INFO);
54+
}
55+
scanner.scanForVersion(baseDir);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.netmikey.cvscanner;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Wraps the required metadata of a java version/release.
8+
*
9+
* @author netmikey
10+
*/
11+
public class JavaVersion {
12+
private int version;
13+
14+
private List<String> aliases;
15+
16+
private long classFileVersion;
17+
18+
/**
19+
* Default constructor.
20+
*
21+
* @param version
22+
* The numeric value of the java version.
23+
* @param classFileVersion
24+
* The class file version that corresponds to this java version.
25+
* @param aliases
26+
* A list of aliases that match this version.
27+
*/
28+
public JavaVersion(int version, long classFileVersion, String... aliases) {
29+
this.version = version;
30+
this.classFileVersion = classFileVersion;
31+
this.aliases = Arrays.asList(aliases);
32+
}
33+
34+
/**
35+
* Get the version.
36+
*
37+
* @return Returns the version.
38+
*/
39+
public int getVersion() {
40+
return version;
41+
}
42+
43+
/**
44+
* Get the aliases.
45+
*
46+
* @return Returns the aliases.
47+
*/
48+
public List<String> getAliases() {
49+
return aliases;
50+
}
51+
52+
/**
53+
* Get the classFileVersion.
54+
*
55+
* @return Returns the classFileVersion.
56+
*/
57+
public long getClassFileVersion() {
58+
return classFileVersion;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)