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 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
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.Iterator;
Expand Down Expand Up @@ -137,24 +135,33 @@ 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.<FileVisitOption>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 {
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 super.postVisitDirectory(dir, exc);
Finomosec marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
*/

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
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 +60,7 @@ public static void main( String[] args )
Main m = new Main();
String threshold = null;

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

files.add(new File(args[i]));
try {
files.add(Paths.get(new URI(args[i]))); // attempt to treat it as an URI
Finomosec marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
Finomosec marked this conversation as resolved.
Show resolved Hide resolved
files.add(Paths.get(args[i])); // if that fails: treat is as a normal file/path
}
}

for (int i = 0; i < files.size(); i++)
{
m.process((File) files.get(i));
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);
}
}

protected void process( String name, InputStream image )
Expand Down Expand Up @@ -129,5 +136,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");
}
}