Skip to content

rf43/ksp-custom-example

Repository files navigation

KSP Custom Example

A demonstration project showcasing a custom Kotlin Symbol Processing (KSP) implementation. This project illustrates how to create and use annotation processors in Kotlin to generate code at compile time.

Project Overview

This project demonstrates:

  • Creating custom annotations in Kotlin
  • Implementing a KSP processor to generate extension functions
  • Using the generated code in a Kotlin application

Project Structure

  • annotation: Contains the annotation definition and processor implementation
    • MockingTextAnnotation.kt: Defines the @MockingText annotation
    • MockingTextProcessor.kt: Implements the KSP processor that generates extension functions
  • app: Contains the main application code
    • ThisIsSerious.kt: Contains data classes annotated with @MockingText
    • Main.kt: Demonstrates the usage of the generated extension functions

How It Works

  1. The @MockingText annotation is applied to data classes
  2. The KSP processor generates an extension function called mockMe() for each annotated class
  3. The mockMe() function transforms string properties by alternating between uppercase and lowercase characters
  4. The generated code is automatically included in the build

How to Run

  1. Clone the repository
  2. Navigate to the root directory of the project
  3. Run the following command:
./gradlew run

This will compile the project, process the annotations, generate the extension functions, and run the main application, which demonstrates the usage of the generated code.

How to Test

The project includes unit tests that verify the functionality of the generated code.

Running All Tests

To run all tests in the project:

./gradlew test

Running Specific Tests

To run tests for a specific class:

./gradlew test --tests "io.cursedfunction.app.ThisIsSeriousTest"

To run a specific test method:

./gradlew test --tests "io.cursedfunction.app.ThisIsSeriousTest.testThisIsSeriousMockMe"

Test Coverage

The tests cover:

  • Verification of the generated mockMe() extension functions
  • Testing with various input types (normal text, empty strings, single characters, special characters)

Requirements

  • JDK 17 or higher
  • Gradle 7.0 or higher

About

An example of a custom KSP implementation using Kotlin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages