Skip to content

Commit 70f5cf8

Browse files
committed
use jakarta.inject for jcache guice integration test
1 parent ae21802 commit 70f5cf8

File tree

5 files changed

+132
-8
lines changed

5 files changed

+132
-8
lines changed

gradle/libs.versions.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ flip-tables = "1.1.1"
3434
forbidden-apis = "3.8"
3535
google-java-format = "1.25.2"
3636
guava = "33.4.0-jre"
37-
guice = "6.0.0"
37+
guice = "7.0.0"
3838
h2 = "2.3.232"
3939
hamcrest = "3.0"
4040
hazelcast = "5.5.0"
@@ -46,6 +46,7 @@ jacoco = "0.8.12"
4646
jakarta-inject = "2.0.1"
4747
jamm = "0.4.0"
4848
java-object-layout = "0.17"
49+
javax-inject = "1"
4950
javapoet = "1.13.0"
5051
jazzer = "0.23.0"
5152
jcache = "1.1.1"
@@ -149,6 +150,7 @@ jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version.ref = "
149150
jamm = { module = "com.github.jbellis:jamm", version.ref = "jamm" }
150151
java-object-layout = { module = "org.openjdk.jol:jol-cli", version.ref = "java-object-layout" }
151152
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
153+
javax-inject = { module = "javax.inject:javax.inject", version.ref = "javax-inject" }
152154
jazzer = { module = "com.code-intelligence:jazzer", version.ref = "jazzer" }
153155
jazzer-junit = { module = "com.code-intelligence:jazzer-junit", version.ref = "jazzer" }
154156
jcache = { module = "javax.cache:cache-api", version.ref = "jcache" }
@@ -214,7 +216,7 @@ errorprone-support = ["errorprone-support", "errorprone-support-refaster"]
214216
jazzer = ["jazzer", "jazzer-junit"]
215217
junit = ["junit4", "junit5"]
216218
junit-engines = ["junit5-vintage", "junit5-testng"]
217-
osgi-test-compile = ["pax-exam-junit4"]
219+
osgi-test-compile = ["javax-inject", "pax-exam-junit4"]
218220
osgi-test-runtime = ["felix-framework", "felix-scr", "osgi-function",
219221
"osgi-promise", "pax-exam-container-native", "pax-exam-link-mvn", "pax-url-aether"]
220222
pmd = ["pmd", "pmd-ant", "pmd-java"]

gradle/plugins/src/main/kotlin/lifecycle/versions.caffeine.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ tasks.named<DependencyUpdatesTask>("dependencyUpdates").configure {
2222
}
2323
}
2424
}
25-
force(libs.guice)
2625
force(libs.commons.collections4)
2726
}
2827
}

jcache/src/test/java/com/github/benmanes/caffeine/jcache/JCacheGuiceTest.java renamed to jcache/src/test/java/com/github/benmanes/caffeine/jcache/guice/JCacheGuiceTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.github.benmanes.caffeine.jcache;
16+
package com.github.benmanes.caffeine.jcache.guice;
1717

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

@@ -28,7 +28,6 @@
2828
import javax.cache.spi.CachingProvider;
2929

3030
import org.jsr107.ri.annotations.DefaultCacheResolverFactory;
31-
import org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule;
3231
import org.testng.annotations.AfterClass;
3332
import org.testng.annotations.BeforeMethod;
3433
import org.testng.annotations.Test;
@@ -57,7 +56,7 @@ public final class JCacheGuiceTest {
5756

5857
@BeforeMethod
5958
public void beforeMethod() {
60-
var module = Modules.override(new CacheAnnotationsModule()).with(new CaffeineJCacheModule());
59+
var module = Modules.override(new JakartaCacheModule()).with(new CaffeineJCacheModule());
6160
Guice.createInjector(module).injectMembers(this);
6261
}
6362

@@ -126,7 +125,7 @@ public <T> Factory<T> factoryOf(String className) {
126125
var clazz = (Class<T>) Class.forName(className);
127126
return injector.getProvider(clazz)::get;
128127
} catch (ClassNotFoundException e) {
129-
throw new RuntimeException(e);
128+
throw new IllegalStateException(e);
130129
}
131130
}
132131
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2025 Ben Manes. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.benmanes.caffeine.jcache.guice;
17+
18+
import javax.cache.annotation.CacheKeyGenerator;
19+
import javax.cache.annotation.CachePut;
20+
import javax.cache.annotation.CacheRemove;
21+
import javax.cache.annotation.CacheRemoveAll;
22+
import javax.cache.annotation.CacheResolverFactory;
23+
import javax.cache.annotation.CacheResult;
24+
25+
import org.aopalliance.intercept.MethodInvocation;
26+
import org.jsr107.ri.annotations.CacheContextSource;
27+
import org.jsr107.ri.annotations.DefaultCacheKeyGenerator;
28+
import org.jsr107.ri.annotations.DefaultCacheResolverFactory;
29+
import org.jsr107.ri.annotations.guice.CacheLookupUtil;
30+
import org.jsr107.ri.annotations.guice.CachePutInterceptor;
31+
import org.jsr107.ri.annotations.guice.CacheRemoveAllInterceptor;
32+
import org.jsr107.ri.annotations.guice.CacheRemoveEntryInterceptor;
33+
import org.jsr107.ri.annotations.guice.CacheResultInterceptor;
34+
35+
import com.google.inject.AbstractModule;
36+
import com.google.inject.Injector;
37+
import com.google.inject.TypeLiteral;
38+
import com.google.inject.matcher.Matchers;
39+
40+
import jakarta.inject.Inject;
41+
import jakarta.inject.Singleton;
42+
43+
/**
44+
* A version of {@link org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule} to adapt the
45+
* the usages of javax.inject to their jakarta.inject counterparts.
46+
*
47+
* @author [email protected] (Ben Manes)
48+
*/
49+
final class JakartaCacheModule extends AbstractModule {
50+
private static final TypeLiteral<CacheContextSource<MethodInvocation>> CACHE_LOOKUP =
51+
new TypeLiteral<>() {};
52+
53+
@Override
54+
protected void configure() {
55+
bind(CACHE_LOOKUP).to(JakartaCacheLookup.class);
56+
bind(CacheKeyGenerator.class).to(DefaultCacheKeyGenerator.class);
57+
bind(CacheResolverFactory.class).to(DefaultCacheResolverFactory.class);
58+
59+
interceptCachePut();
60+
incerceptCacheResult();
61+
interceptCacheRemove();
62+
interceptCacheRemoveAll();
63+
}
64+
65+
private void interceptCachePut() {
66+
var interceptor = new JakartaCachePutInterceptor();
67+
requestInjection(interceptor);
68+
bindInterceptor(Matchers.annotatedWith(CachePut.class), Matchers.any(), interceptor);
69+
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CachePut.class), interceptor);
70+
}
71+
72+
private void interceptCacheRemove() {
73+
var interceptor = new JakartaCacheRemoveEntryInterceptor();
74+
requestInjection(interceptor);
75+
bindInterceptor(Matchers.annotatedWith(CacheRemove.class), Matchers.any(), interceptor);
76+
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemove.class), interceptor);
77+
}
78+
79+
private void incerceptCacheResult() {
80+
var interceptor = new JakartaCacheResultInterceptor();
81+
requestInjection(interceptor);
82+
bindInterceptor(Matchers.annotatedWith(CacheResult.class), Matchers.any(), interceptor);
83+
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheResult.class), interceptor);
84+
}
85+
86+
private void interceptCacheRemoveAll() {
87+
var interceptor = new JakartaCacheRemoveAllInterceptor();
88+
requestInjection(interceptor);
89+
bindInterceptor(Matchers.annotatedWith(CacheRemoveAll.class), Matchers.any(), interceptor);
90+
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemoveAll.class), interceptor);
91+
}
92+
93+
private static final class JakartaCachePutInterceptor extends CachePutInterceptor {
94+
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
95+
setCacheContextSource(cacheContextSource);
96+
}
97+
}
98+
99+
private static final class JakartaCacheResultInterceptor extends CacheResultInterceptor {
100+
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
101+
setCacheContextSource(cacheContextSource);
102+
}
103+
}
104+
105+
private static final class JakartaCacheRemoveEntryInterceptor extends CacheRemoveEntryInterceptor {
106+
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
107+
setCacheContextSource(cacheContextSource);
108+
}
109+
}
110+
111+
private static final class JakartaCacheRemoveAllInterceptor extends CacheRemoveAllInterceptor {
112+
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) {
113+
setCacheContextSource(cacheContextSource);
114+
}
115+
}
116+
117+
@Singleton
118+
private static final class JakartaCacheLookup extends CacheLookupUtil {
119+
@Inject public JakartaCacheLookup(Injector injector, CacheKeyGenerator defaultCacheKeyGenerator,
120+
CacheResolverFactory defaultCacheResolverFactory) {
121+
super(injector, defaultCacheKeyGenerator, defaultCacheResolverFactory);
122+
}
123+
}
124+
}

jcache/src/test/resources/application.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ caffeine.jcache {
7676
guice {
7777
read-through {
7878
enabled = true
79-
loader = "com.github.benmanes.caffeine.jcache.JCacheGuiceTest$InjectedCacheLoader"
79+
loader = "com.github.benmanes.caffeine.jcache.guice.JCacheGuiceTest$InjectedCacheLoader"
8080
}
8181
}
8282

0 commit comments

Comments
 (0)