Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added java 9+ compatibility (bugfix & addition) #139

Merged
merged 14 commits into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,8 +35,6 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -130,24 +128,36 @@ else if ( ( file.getName().endsWith( ".jar" ) || file.getName().endsWith( ".jmod
* or a class file (in which case that single class is processed).
*/
public void process( Path path )
throws IOException {
Files.walkFileTree(path, Collections.emptySet(), 10000, new SimpleFileVisitor<Path>() {
throws IOException
{
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
final SortedSet<Path> files = new TreeSet<>();

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(".class")) {
process(file.toString(), Files.newInputStream(file));
files.add(file);
}
// XXX we could add processing of jars here as well
// but it's not necessary for processing: Paths.get(URI.create("jrt:/modules"))
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
if (exc != null) {
throw exc;
}
for (final Path file : files) {
Finomosec marked this conversation as resolved.
Show resolved Hide resolved
try (final InputStream inputStream = Files.newInputStream(file)) {
Copy link
Member

@olamy olamy May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit.: why final? not sure if it helps anything here

Copy link
Contributor Author

@Finomosec Finomosec May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional. Not harmful. It does not change the behavior here.

In my company we decided to make anything final, that can be final.
It's supposed to make the semantics of the code clearer when reading code.

Remove the finals if you want.

p.s. thanks for the merge.

process(file.toString(), inputStream);
}
}
files.clear();
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
Expand Down
Expand Up @@ -26,9 +26,12 @@
*/

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -58,7 +61,7 @@ public static void main( String[] args )
Main m = new Main();
String threshold = null;

List<File> files = new ArrayList<>();
List<Path> files = new ArrayList<Path>();
for ( int i = 0; i < args.length; i++ )
{
if (args[i].equals("-h"))
Expand All @@ -80,16 +83,32 @@ public static void main( String[] args )
continue;
}

files.add(new File(args[i]));
files.add(getPath(args[i]));
}

for ( File f : files )
{
m.process( f );
for (Path file : files) {
m.process(file);
}

if (threshold!=null && m.maximumVersion.compareTo(threshold)>0)
if ( threshold != null && m.maximumVersion.compareTo(threshold) > 0 )
{
System.exit(1);
}
}

private static Path getPath(String s) {
try {
URI uri = new URI(s);
String scheme = uri.getScheme();
// Only allow certain schemes to prevent treating (mistyped) file path unintentionally as URI
if (scheme.equalsIgnoreCase("file") || scheme.equalsIgnoreCase("jrt")) {
return Paths.get(uri);
}
// Fall through
} catch (URISyntaxException e) {
// Fall through; probably not a URI but a file path
}
return Paths.get(s);
}

protected void process( String name, InputStream image )
Expand Down Expand Up @@ -132,5 +151,14 @@ private static int u2( byte u, byte d )
HUMAN_READABLE_NAME.put("48.0","Java4");
HUMAN_READABLE_NAME.put("49.0","Java5");
HUMAN_READABLE_NAME.put("50.0","Java6");
HUMAN_READABLE_NAME.put("51.0","Java7");
HUMAN_READABLE_NAME.put("52.0","Java8");
HUMAN_READABLE_NAME.put("53.0","Java9");
HUMAN_READABLE_NAME.put("54.0","Java10");
HUMAN_READABLE_NAME.put("55.0","Java11");
HUMAN_READABLE_NAME.put("56.0","Java12");
HUMAN_READABLE_NAME.put("57.0","Java13");
HUMAN_READABLE_NAME.put("58.0","Java14");
HUMAN_READABLE_NAME.put("59.0","Java15");
}
}