-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16775bc
commit aabd918
Showing
4 changed files
with
68 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...commons/src/main/java/io/github/algomaster99/terminator/commons/fingerprint/JdkClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* A class that represents a JDK class. It contains the name of the class and the bytes of the class as a {@link ByteBuffer}. | ||
*/ | ||
public record JdkClass(String name, ByteBuffer bytes) {} |
56 changes: 43 additions & 13 deletions
56
...mmons/src/main/java/io/github/algomaster99/terminator/commons/fingerprint/JdkIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint; | ||
|
||
import io.github.classgraph.ClassGraph; | ||
import io.github.classgraph.Resource; | ||
import io.github.classgraph.ScanResult; | ||
import java.lang.module.ModuleFinder; | ||
import java.lang.module.ModuleReader; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The JdkIndexer class provides a utility to list all JDK classes by scanning the JDK used for the execution of the application. | ||
*/ | ||
public class JdkIndexer { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JdkIndexer.class); | ||
|
||
/** | ||
* Returns a list of all jdk classes. The list is populated by scanning the jdk used for the execution of this application. | ||
* @return a list of all jdk classes, never null. | ||
* Returns a list of all JDK classes. The list is populated by scanning the JDK used for the execution of this application. | ||
* | ||
* @return a list of all JDK classes, never null. | ||
*/ | ||
public static List<Resource> listJdkClasses() { | ||
try (ScanResult scanResult = new ClassGraph() | ||
.enableSystemJarsAndModules() | ||
.acceptPackages("java.*", "jdk.*", "oracle.*", "sun.*") | ||
.scan()) { | ||
return scanResult.getAllClasses().stream().map(v -> v.getResource()).collect(Collectors.toList()); | ||
} | ||
public static List<JdkClass> listJdkClasses() { | ||
List<JdkClass> jdkClasses = new ArrayList<>(); | ||
ModuleFinder.ofSystem().findAll().forEach(mr -> { | ||
try { | ||
try (ModuleReader reader = mr.open()) { | ||
for (String resource : reader.list().toList()) { | ||
if (!resource.endsWith(".class")) { | ||
continue; | ||
} | ||
Optional<ByteBuffer> contents = reader.read(resource); | ||
if (contents.isPresent()) { | ||
jdkClasses.add(new JdkClass(resource, contents.get())); | ||
} else { | ||
logger.atWarn() | ||
.log( | ||
"Could not read resource {} from module {}", | ||
resource, | ||
mr.descriptor().name()); | ||
} | ||
} | ||
} | ||
} catch (Throwable e) { | ||
logger.atError() | ||
.setCause(e) | ||
.log("Error while reading module {}", mr.descriptor().name()); | ||
} | ||
}); | ||
return jdkClasses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters