Created
September 22, 2015 15:24
-
-
Save cboehme/035a586cc9eb9a5e6563 to your computer and use it in GitHub Desktop.
Jar Scanner
This file contains hidden or 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
| package net.b3e.test.jarloader; | |
| import static java.util.Arrays.stream; | |
| import java.io.IOException; | |
| import java.lang.annotation.Annotation; | |
| import java.net.URL; | |
| import java.net.URLClassLoader; | |
| import java.util.Enumeration; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipFile; | |
| public class JarScanner { | |
| private static String JAR_NAME = | |
| "C:/Program Files/Java/jdk1.8.0_60/lib/tools.jar"; | |
| public static void main(String[] args) throws ClassNotFoundException, IOException { | |
| final URL[] urls = { new URL("file:///" + JAR_NAME) }; | |
| final ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); | |
| try( | |
| final URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader); | |
| final ZipFile jarFile = new ZipFile(JAR_NAME); | |
| ) { | |
| final Class<?> jdkExported = classLoader.loadClass("jdk.Exported"); | |
| final Enumeration<? extends ZipEntry> jarEntries = jarFile.entries(); | |
| while(jarEntries.hasMoreElements()) { | |
| final ZipEntry jarEntry = jarEntries.nextElement(); | |
| final String name = jarEntry.getName(); | |
| if (name.endsWith(".class")) { | |
| System.out.println("Found: " + name); | |
| final String className = name.substring(0, name.length() - 6).replaceAll("/", "."); | |
| Class<?> metaClass = classLoader.loadClass(className); | |
| System.out.println("Loaded: " + metaClass.getCanonicalName()); | |
| Annotation[] annotations = metaClass.getAnnotations(); | |
| boolean isAnnotated = stream(annotations) | |
| .map(Annotation::annotationType) | |
| .filter(t -> jdkExported.equals(t)) | |
| .findFirst() | |
| .isPresent(); | |
| if (isAnnotated) { | |
| System.out.println("\t --> has jdk.Exported annotation"); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment