Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
43 changes: 33 additions & 10 deletions src/main/java/io/neonbee/internal/scanner/ClassPathScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -70,6 +71,8 @@ public class ClassPathScanner {
*/
public static final Pattern SEPARATOR_PATTERN = Pattern.compile(";");

private static final String NEONBEE_MANIFEST_PREFIX = "NeonBee-";

private final ClassLoader classLoader;

/**
Expand Down Expand Up @@ -195,14 +198,15 @@ public Future<List<String>> scanForAnnotation(Vertx vertx,
ElementType... elementTypes) {

Future<List<String>> classesFromDirectories = scanWithPredicate(vertx, ClassPathScanner::isClassFile);
Future<List<String>> classesFromJars = scanJarFilesWithPredicate(vertx, ClassPathScanner::isClassFile)
.map(uris -> {
List<String> result = new ArrayList<>(uris.size());
for (URI uri : uris) {
result.add(JarHelper.extractFilePath(uri));
}
return result;
});
Future<List<String>> classesFromJars =
scanJarFilesWithPredicate(vertx, ClassPathScanner::isClassFile, ClassPathScanner::isNeonBeeJar)
.map(uris -> {
List<String> result = new ArrayList<>(uris.size());
for (URI uri : uris) {
result.add(JarHelper.extractFilePath(uri));
}
return result;
});

return Future.all(classesFromDirectories, classesFromJars).compose(v -> vertx.executeBlocking(() -> {
List<AnnotationClassVisitor> classVisitors = new ArrayList<>(annotationClasses.size());
Expand Down Expand Up @@ -282,12 +286,16 @@ public Future<List<String>> scanWithPredicate(Vertx vertx, Predicate<String> pre
* @return a future to a list of URIs representing the files which matches the given predicate
*/
public Future<List<URI>> scanJarFilesWithPredicate(Vertx vertx, Predicate<String> predicate) {
return scanJarFilesWithPredicate(vertx, predicate, url -> true);
}

private Future<List<URI>> scanJarFilesWithPredicate(Vertx vertx, Predicate<String> predicate,
Predicate<URL> jarFilter) {
return vertx.executeBlocking(() -> {
List<URI> resources = new ArrayList<>();
for (URL manifestResource : getManifestResourceURLs()) {
URI uri = manifestResource.toURI();
// filter for manifest files inside of jar files
if ("jar".equals(uri.getScheme())) {
if ("jar".equals(uri.getScheme()) && jarFilter.test(manifestResource)) {
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Map.of())) {
Path rootPath = fileSystem.getPath("/");
scanDirectoryWithPredicateRecursive(rootPath, predicate)
Expand All @@ -299,6 +307,21 @@ public Future<List<URI>> scanJarFilesWithPredicate(Vertx vertx, Predicate<String
});
}

@SuppressWarnings("PMD.EmptyCatchBlock")
private static boolean isNeonBeeJar(URL manifestUrl) {
try (InputStream is = manifestUrl.openStream()) {
Manifest manifest = new Manifest(is);
Attributes attributes = manifest.getMainAttributes();
for (Object key : attributes.keySet()) {
if (key.toString().startsWith(NEONBEE_MANIFEST_PREFIX)) {
return true;
}
}
} catch (IOException e) { // NOPMD
}
return false;
}

/**
* Blocking method to get all resource URLs from the MANIFEST.MF file(s) on the class loader
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public String getSimpleName() {
}

public BasicJar asJar() throws IOException {
return new BasicJar(Map.of(BasicJar.getJarEntryName(getClassName()), compileToByteCode()));
return new BasicJar(Map.of("NeonBee-Module", "test"),
Map.of(BasicJar.getJarEntryName(getClassName()), compileToByteCode()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ void scanWithPredicateJarFile(Vertx vertx, VertxTestContext testContext) throws
})));
}

@Test
@DisplayName("Should skip non-NeonBee JARs during annotation scanning")
void scanForAnnotationSkipsNonNeonBeeJars(Vertx vertx, VertxTestContext testContext) throws IOException {
BasicJar nonNeonBeeJar = new BasicJar(
Map.of(BasicJar.getJarEntryName("some.ThirdPartyClass"),
new AnnotatedClassTemplate("ThirdPartyClass", "some")
.setTypeAnnotation("@Deprecated").compileToByteCode()));

URLClassLoader urlc = new URLClassLoader(nonNeonBeeJar.writeToTempURL(), null);
new ClassPathScanner(urlc).scanForAnnotation(vertx, Deprecated.class)
.onComplete(testContext.succeeding(list -> testContext.verify(() -> {
assertThat(list).isEmpty();
testContext.completeNow();
})));
}

@Test
@DisplayName("Should find classes which the class or any field or method within is annotated with a given annotation")
void scanForAnnotation(Vertx vertx, VertxTestContext testContext) throws IOException {
Expand Down