Skip to content

Commit

Permalink
use jakarta.inject for jcache guice integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jan 6, 2025
1 parent ae21802 commit 70f5cf8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 8 deletions.
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ flip-tables = "1.1.1"
forbidden-apis = "3.8"
google-java-format = "1.25.2"
guava = "33.4.0-jre"
guice = "6.0.0"
guice = "7.0.0"
h2 = "2.3.232"
hamcrest = "3.0"
hazelcast = "5.5.0"
Expand All @@ -46,6 +46,7 @@ jacoco = "0.8.12"
jakarta-inject = "2.0.1"
jamm = "0.4.0"
java-object-layout = "0.17"
javax-inject = "1"
javapoet = "1.13.0"
jazzer = "0.23.0"
jcache = "1.1.1"
Expand Down Expand Up @@ -149,6 +150,7 @@ jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version.ref = "
jamm = { module = "com.github.jbellis:jamm", version.ref = "jamm" }
java-object-layout = { module = "org.openjdk.jol:jol-cli", version.ref = "java-object-layout" }
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
javax-inject = { module = "javax.inject:javax.inject", version.ref = "javax-inject" }
jazzer = { module = "com.code-intelligence:jazzer", version.ref = "jazzer" }
jazzer-junit = { module = "com.code-intelligence:jazzer-junit", version.ref = "jazzer" }
jcache = { module = "javax.cache:cache-api", version.ref = "jcache" }
Expand Down Expand Up @@ -214,7 +216,7 @@ errorprone-support = ["errorprone-support", "errorprone-support-refaster"]
jazzer = ["jazzer", "jazzer-junit"]
junit = ["junit4", "junit5"]
junit-engines = ["junit5-vintage", "junit5-testng"]
osgi-test-compile = ["pax-exam-junit4"]
osgi-test-compile = ["javax-inject", "pax-exam-junit4"]
osgi-test-runtime = ["felix-framework", "felix-scr", "osgi-function",
"osgi-promise", "pax-exam-container-native", "pax-exam-link-mvn", "pax-url-aether"]
pmd = ["pmd", "pmd-ant", "pmd-java"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ tasks.named<DependencyUpdatesTask>("dependencyUpdates").configure {
}
}
}
force(libs.guice)
force(libs.commons.collections4)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.jcache;
package com.github.benmanes.caffeine.jcache.guice;

import static com.google.common.truth.Truth.assertThat;

Expand All @@ -28,7 +28,6 @@
import javax.cache.spi.CachingProvider;

import org.jsr107.ri.annotations.DefaultCacheResolverFactory;
import org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -57,7 +56,7 @@ public final class JCacheGuiceTest {

@BeforeMethod
public void beforeMethod() {
var module = Modules.override(new CacheAnnotationsModule()).with(new CaffeineJCacheModule());
var module = Modules.override(new JakartaCacheModule()).with(new CaffeineJCacheModule());
Guice.createInjector(module).injectMembers(this);
}

Expand Down Expand Up @@ -126,7 +125,7 @@ public <T> Factory<T> factoryOf(String className) {
var clazz = (Class<T>) Class.forName(className);
return injector.getProvider(clazz)::get;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
throw new IllegalStateException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2025 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.jcache.guice;

import javax.cache.annotation.CacheKeyGenerator;
import javax.cache.annotation.CachePut;
import javax.cache.annotation.CacheRemove;
import javax.cache.annotation.CacheRemoveAll;
import javax.cache.annotation.CacheResolverFactory;
import javax.cache.annotation.CacheResult;

import org.aopalliance.intercept.MethodInvocation;
import org.jsr107.ri.annotations.CacheContextSource;
import org.jsr107.ri.annotations.DefaultCacheKeyGenerator;
import org.jsr107.ri.annotations.DefaultCacheResolverFactory;
import org.jsr107.ri.annotations.guice.CacheLookupUtil;
import org.jsr107.ri.annotations.guice.CachePutInterceptor;
import org.jsr107.ri.annotations.guice.CacheRemoveAllInterceptor;
import org.jsr107.ri.annotations.guice.CacheRemoveEntryInterceptor;
import org.jsr107.ri.annotations.guice.CacheResultInterceptor;

import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

/**
* A version of {@link org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule} to adapt the
* the usages of javax.inject to their jakarta.inject counterparts.
*
* @author [email protected] (Ben Manes)
*/
final class JakartaCacheModule extends AbstractModule {
private static final TypeLiteral<CacheContextSource<MethodInvocation>> CACHE_LOOKUP =
new TypeLiteral<>() {};

@Override
protected void configure() {
bind(CACHE_LOOKUP).to(JakartaCacheLookup.class);
bind(CacheKeyGenerator.class).to(DefaultCacheKeyGenerator.class);
bind(CacheResolverFactory.class).to(DefaultCacheResolverFactory.class);

interceptCachePut();
incerceptCacheResult();
interceptCacheRemove();
interceptCacheRemoveAll();
}

private void interceptCachePut() {
var interceptor = new JakartaCachePutInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.annotatedWith(CachePut.class), Matchers.any(), interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CachePut.class), interceptor);
}

private void interceptCacheRemove() {
var interceptor = new JakartaCacheRemoveEntryInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.annotatedWith(CacheRemove.class), Matchers.any(), interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemove.class), interceptor);
}

private void incerceptCacheResult() {
var interceptor = new JakartaCacheResultInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.annotatedWith(CacheResult.class), Matchers.any(), interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheResult.class), interceptor);
}

private void interceptCacheRemoveAll() {
var interceptor = new JakartaCacheRemoveAllInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.annotatedWith(CacheRemoveAll.class), Matchers.any(), interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemoveAll.class), interceptor);
}

private static final class JakartaCachePutInterceptor extends CachePutInterceptor {
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
setCacheContextSource(cacheContextSource);
}
}

private static final class JakartaCacheResultInterceptor extends CacheResultInterceptor {
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
setCacheContextSource(cacheContextSource);
}
}

private static final class JakartaCacheRemoveEntryInterceptor extends CacheRemoveEntryInterceptor {
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
setCacheContextSource(cacheContextSource);
}
}

private static final class JakartaCacheRemoveAllInterceptor extends CacheRemoveAllInterceptor {
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
setCacheContextSource(cacheContextSource);
}
}

@Singleton
private static final class JakartaCacheLookup extends CacheLookupUtil {
@Inject public JakartaCacheLookup(Injector injector, CacheKeyGenerator defaultCacheKeyGenerator,
CacheResolverFactory defaultCacheResolverFactory) {
super(injector, defaultCacheKeyGenerator, defaultCacheResolverFactory);
}
}
}
2 changes: 1 addition & 1 deletion jcache/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ caffeine.jcache {
guice {
read-through {
enabled = true
loader = "com.github.benmanes.caffeine.jcache.JCacheGuiceTest$InjectedCacheLoader"
loader = "com.github.benmanes.caffeine.jcache.guice.JCacheGuiceTest$InjectedCacheLoader"
}
}

Expand Down

0 comments on commit 70f5cf8

Please sign in to comment.