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
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
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 | |
} |
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
// Create the directories: yourmodule/src/main/resources/META-INF/services/ | |
// Create a file named com.google.devtools.ksp.processing.SymbolProcessorProvider | |
// Inside the file, add the following line (and nothing more): | |
com.snaky.ksp.processor.provider.ListedProcessorProvider | |
// For a reference, you can check out here: | |
// https://github.com/adibfara/ListGen/blob/main/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider |
NewerOlder