Skip to content

Commit

Permalink
caffeine: register CacheLoader and implementors for reflection #12074
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 14, 2020
1 parent e887e43 commit 46c3d0e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import java.io.IOException;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;

public class CaffeineProcessor {
private static final String CACHE_LOADER_CLASS_NAME = "com.github.benmanes.caffeine.cache.CacheLoader";
private static final DotName CACHE_LOADER_NAME = DotName.createSimple(CACHE_LOADER_CLASS_NAME);

@BuildStep
ReflectiveClassBuildItem cacheClasses() throws IOException {
Expand All @@ -19,4 +26,13 @@ ReflectiveClassBuildItem cacheClasses() throws IOException {
"com.github.benmanes.caffeine.cache.SSLA",
"com.github.benmanes.caffeine.cache.PSA");
}

@BuildStep
void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
reflectiveClasses.produce(new ReflectiveClassBuildItem(true, false, CACHE_LOADER_CLASS_NAME));

for (ClassInfo info : combinedIndex.getIndex().getAllKnownImplementors(CACHE_LOADER_NAME)) {
reflectiveClasses.produce(new ReflectiveClassBuildItem(true, false, info.name().toString()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkus.it.cache;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.json.Json;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.github.benmanes.caffeine.cache.CacheLoader;

@ApplicationScoped
@Path("/caffeine")
public class CaffeineResource {
/*
* Replicates the behavior of com.github.benmanes.caffeine.cache.LocalLoadingCache.java
*
* https://github.com/ben-manes/caffeine/blob/37f6ad303eb4474cd8a644551d528dfda37c5bfc/caffeine/src/main/java/com/github/
* benmanes/caffeine/cache/LocalLoadingCache.java#L176-L185
*/
public static boolean hasLoadAll(CacheLoader<String, String> cl) {
try {
Method classLoadAll = cl.getClass().getMethod("loadAll", Iterable.class);
Method defaultLoadAll = CacheLoader.class.getMethod("loadAll", Iterable.class);
return !classLoadAll.equals(defaultLoadAll);
} catch (NoSuchMethodException | SecurityException e) {
return false;
}
}

@GET
@Path("/hasLoadAll")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject hasLoadAll() {
return Json.createObjectBuilder()
.add("loader", hasLoadAll(new MyCacheLoader()))
.add("bulk-loader", hasLoadAll(new MyBulkCacheLoader()))
.build();
}

public static class MyCacheLoader implements CacheLoader<String, String> {
@Override
public String load(String unused) throws Exception {
return null;
}
}

public static class MyBulkCacheLoader implements CacheLoader<String, String> {
@Override
public String load(String unused) throws Exception {
return null;
}

@Override
public Map<String, String> loadAll(Iterable<? extends String> unused) throws Exception {
return Collections.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.cache;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
public class CaffeineITCase extends CaffeineTestCase {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.it.cache;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class CaffeineTestCase {

@Test
public void test() {
given().when().get("/caffeine/hasLoadAll").then().statusCode(200).body(
"loader", is(false),
"bulk-loader", is(true));
}
}

0 comments on commit 46c3d0e

Please sign in to comment.