This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Item { | |
void printStructure(); | |
int weight(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed interface Item permits Box, Container { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
var ensemble = new Container( | |
new Item[]{ | |
new Box(2), | |
new Container(new Item[]{ | |
new Box(3), | |
new Box(4)} | |
)}); | |
ensemble.visit(new PrinterVisitor()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ItemVisitor<T> { | |
T visit(Box box); | |
T visit(Container container); | |
} | |
public interface Item { | |
<T> T visit(ItemVisitor<T> itemVisitor); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WeightCalculator { | |
public int calculate(Item item) { | |
return switch (item) { | |
case Box box -> calculate(box); | |
case Container container -> calculate(container); | |
default -> throw new IllegalStateException("Unexpected value: " + item); | |
}; | |
} | |
public int calculate(Box box) { | |
return box.size(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public record Box(int size) implements Item {} | |
public record Container(Item[] items) implements Item { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to help the IDE recognize the KSP generated files, add the following to your app's build.gradle | |
android { | |
kotlin { | |
sourceSets.debug { | |
kotlin.srcDirs += 'build/generated/ksp/debug/kotlin' | |
} | |
sourceSets.release { | |
kotlin.srcDirs += 'build/generated/ksp/release/kotlin' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class ListedProcessor( | |
private val environment: SymbolProcessorEnvironment, | |
) : SymbolProcessor { | |
private fun Resolver.findAnnotations( | |
kClass: KClass<*>, | |
) = getSymbolsWithAnnotation( | |
kClass.qualifiedName.toString()) | |
.filterIsInstance<KSFunctionDeclaration>().filter { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class ListedProcessor( | |
private val environment: SymbolProcessorEnvironment, | |
) : SymbolProcessor { | |
private fun Resolver.findAnnotations( | |
kClass: KClass<*>, | |
) = getSymbolsWithAnnotation( | |
kClass.qualifiedName.toString()) | |
.filterIsInstance<KSFunctionDeclaration>().filter { | |
it.parameters.isEmpty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inside your app's module, declare a dependency to your ksp module | |
dependencies { | |
implementation project(":ksp") // since you want to use your @Listed annotation | |
ksp project(":ksp") // to make KSP work | |
} |
NewerOlder