Skip to content

Commit 2286f90

Browse files
author
crypticminds
committed
[Freeze annotation] : Finalizing the freeze annotation
1 parent 2b461b7 commit 2286f90

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.arcane.coldstorage.cache
2+
3+
import com.arcane.coldstorageannotation.CacheKey
4+
import com.arcane.coldstorageannotation.Freeze
5+
6+
/**
7+
* Example of custom class name.
8+
*/
9+
@Freeze(generatedClassName = "MyClassName")
10+
class CacheWithFreezeWithCustomName {
11+
12+
13+
fun method1() {
14+
15+
}
16+
17+
18+
fun method2(@CacheKey key1: String, key2: String) {
19+
20+
}
21+
}

coldstorageannotation/src/main/java/com/arcane/coldstorageannotation/Freeze.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ package com.arcane.coldstorageannotation
1010
* @param maxSize the maximum size allocated for the class to store data in
1111
* the cache.
1212
*
13+
* @param generatedClassName the name of the cache class that will be generated.
14+
* If a value is not provided here a prefix "Generated" will be added to the original
15+
* class.
16+
*
1317
* This cache will generate a class with the original class name prefixed with
1418
* "cacheLayer" with all the methods wrapped with the caching logic.
1519
*/
1620
@Retention(AnnotationRetention.SOURCE)
1721
@Target(AnnotationTarget.CLASS)
18-
annotation class Freeze(val timeToLive: Long = -1, val maxSize: Long = -1)
22+
annotation class Freeze(val timeToLive: Long = -1,
23+
val maxSize: Long = -1,
24+
val generatedClassName : String = "")

coldstoragecompiler/src/main/java/com/arcane/coldstorage_compiler/FreezeAnnotationProcessor.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ class FreezeAnnotationProcessor : AbstractProcessor() {
115115
.addFunctions(methods)
116116
.build()
117117

118+
val fileName =
119+
if (element.getAnnotation(Freeze::class.java).generatedClassName.isBlank())
120+
"Generated${element.simpleName} " else
121+
element.getAnnotation(Freeze::class.java).generatedClassName
122+
118123
val kotlinFile = FileSpec.builder(
119124
GENERATED_PACKAGE_NAME,
120-
"Cache${element.simpleName}"
125+
fileName
121126
)
122127
.addType(kotlinClass)
123128
.build()

coldstoragecompiler/src/main/java/com/arcane/coldstorage_compiler/helper/CodeGenerationHelper.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,31 @@ class CodeGenerationHelper {
179179
if (parameters.isNullOrEmpty()) {
180180
calculateKeyCodeBlock += " \"$name\""
181181
} else {
182-
parameters.forEach { key ->
182+
183+
var keys = mutableListOf<String>()
184+
185+
val annotatedKeys = parameters.stream().map { parameter ->
186+
if (parameter.getAnnotation(CacheKey::class.java) != null) {
187+
parameter.simpleName.toString()
188+
} else {
189+
null
190+
}
191+
}.filter(Objects::nonNull)
192+
.collect(Collectors.toList())
193+
194+
if (annotatedKeys.isEmpty()) {
195+
keys = parameters.stream().map { parameter -> parameter.simpleName.toString() }
196+
.collect(Collectors.toList())
197+
} else {
198+
annotatedKeys.forEach { key -> keys.add(key!!) }
199+
}
200+
201+
202+
203+
204+
keys.forEach { key ->
183205
calculateKeyCodeBlock += "Integer.toString($key.hashCode())"
184-
if (parameters.indexOf(key) != parameters.size - 1) {
206+
if (keys.indexOf(key) != keys.size - 1) {
185207
calculateKeyCodeBlock += " + "
186208
}
187209
}
@@ -261,7 +283,6 @@ class CodeGenerationHelper {
261283
val keys: MutableList<String> = refrigerate.keys.toMutableList()
262284

263285

264-
265286
var annotatedKeys = parameters
266287
.stream()
267288
.map { parameter ->
@@ -277,7 +298,7 @@ class CodeGenerationHelper {
277298

278299
annotatedKeys.addAll(keys)
279300

280-
if(annotatedKeys.isEmpty()) {
301+
if (annotatedKeys.isEmpty()) {
281302
annotatedKeys = parameters.stream().map { parameter ->
282303
parameter.simpleName.toString()
283304
}.collect(Collectors.toList())

0 commit comments

Comments
 (0)