Skip to content

Commit

Permalink
Added java 9+ compatibility (bugfix & addition) (#139)
Browse files Browse the repository at this point in the history
* Enabled java9+ compatibility

- SignatureChecker: upgraded from ASM5 to ASM7
- ClassFileVisitor: added ability to process Paths (nedded for "jrt:/modules")
- SignatureBuilder: added java version switch (<=8 / >=9) and implemented signature creation for java 9+

* added support for java7+ and URIs like "jrt:/modules"

* fixed bug: processing failed sometimes when inner classes existed

Inner classes need to be processed first. By using a TreeSet, the files are automatically sorted with inner classes first. eg. like "Outer$Inner.class" before "Outer.class"

* fixed issue: InputStream was not closed

* fixed issue: FileSystemNotFoundException was thrown for paths like "C:/..." (with forward slash)

* removed obsolete method - the inherited method did the same

* changed process(Path path) to process each directory separately

* fixed bug with paths like: "C:\..." (backslash) and others

* implemented suggested changes

#139 (comment)

* improved exception handling

https://github.com/mojohaus/animal-sniffer/pull/139/files

* removed obsolete "throws IOException"
  • Loading branch information
Finomosec committed May 2, 2022
1 parent c551545 commit 29e3e4b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
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,26 +128,38 @@ 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 visitFileFailed(Path file, IOException exc) throws IOException {
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
if (exc != null) {
throw exc;
}
for (final Path file : files) {
try (final InputStream inputStream = Files.newInputStream(file)) {
process(file.toString(), inputStream);
}
}
files.clear();
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}

Expand Down
Original file line number Diff line number Diff line change
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");
}
}

0 comments on commit 29e3e4b

Please sign in to comment.