Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect #198

Merged
merged 1 commit into from
Nov 2, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.benmanes.caffeine.cache;

import java.lang.reflect.Constructor;
import java.util.Set;

import com.squareup.javapoet.CodeBlock;
Expand Down Expand Up @@ -88,16 +89,18 @@ private LocalCacheSelectorCode expires() {
return this;
}

private LocalCacheSelectorCode selector(Set<String> classNames) {
CodeBlock.Builder switchBuilder = CodeBlock.builder();
switchBuilder.beginControlFlow("switch (sb.toString())");
for (String className : classNames) {
switchBuilder.addStatement(
"case $S: return new $N<>(builder, cacheLoader, async)", className, className);
}
switchBuilder.addStatement("default: throw new $T(sb.toString())", IllegalStateException.class);
switchBuilder.endControlFlow();
block.add(switchBuilder.build());
private LocalCacheSelectorCode selector() {
CodeBlock.Builder reflectBuilder = CodeBlock.builder();
reflectBuilder.add("try {\n"
+ " Class<?> cls = LocalCacheFactory.class.getClassLoader()\n"
+ " .loadClass(\"com.github.benmanes.caffeine.cache.LocalCacheFactory$$\" + sb.toString());\n"
+ " $T<?> ctor = cls.getDeclaredConstructor(Caffeine.class, CacheLoader.class, boolean.class);\n"
+ " return (BoundedLocalCache<K, V>) ctor.newInstance(builder, cacheLoader, async);\n"
+ "} catch (ReflectiveOperationException e) {\n"
+ " throw new IllegalStateException(sb.toString());\n"
+ "}\n"
+ "\n", Constructor.class);
block.add(reflectBuilder.build());
return this;
}

Expand All @@ -113,7 +116,7 @@ public static CodeBlock get(Set<String> classNames) {
.stats()
.maximum()
.expires()
.selector(classNames)
.selector()
.build();
}
}