├── .gitignore ├── .idea ├── .gitignore ├── .name ├── compiler.xml ├── jarRepositories.xml ├── libraries-with-intellij-classes.xml └── misc.xml ├── build.gradle.kts ├── example ├── build.gradle.kts └── src │ └── main │ └── kotlin │ └── me │ └── tatocaster │ ├── Playground.kt │ └── TestClass.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── processor ├── build.gradle.kts └── src │ └── main │ └── kotlin │ ├── PrintProcessor.kt │ ├── PrintProcessorProvider.kt │ ├── SimpleClassGeneratorProcessor.kt │ ├── SimpleClassGeneratorProcessorProvider.kt │ ├── Utils.kt │ └── annotation │ └── Print.kt ├── readme.md └── settings.gradle.kts /.gitignore: -------------------------------------------------------------------------------- 1 | ### macOS template 2 | # General 3 | .DS_Store 4 | .AppleDouble 5 | .LSOverride 6 | 7 | # Icon must end with two \r 8 | Icon 9 | 10 | # Thumbnails 11 | ._* 12 | 13 | # Files that might appear in the root of a volume 14 | .DocumentRevisions-V100 15 | .fseventsd 16 | .Spotlight-V100 17 | .TemporaryItems 18 | .Trashes 19 | .VolumeIcon.icns 20 | .com.apple.timemachine.donotpresent 21 | 22 | # Directories potentially created on remote AFP share 23 | .AppleDB 24 | .AppleDesktop 25 | Network Trash Folder 26 | Temporary Items 27 | .apdisk 28 | 29 | ### Linux template 30 | *~ 31 | 32 | # temporary files which can be created if a process still has a handle open of a deleted file 33 | .fuse_hidden* 34 | 35 | # KDE directory preferences 36 | .directory 37 | 38 | # Linux trash folder which might appear on any partition or disk 39 | .Trash-* 40 | 41 | # .nfs files are created when an open file is removed but is still being accessed 42 | .nfs* 43 | 44 | ### JetBrains template 45 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 46 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 47 | 48 | # User-specific stuff 49 | .idea/**/workspace.xml 50 | .idea/**/tasks.xml 51 | .idea/**/usage.statistics.xml 52 | .idea/**/dictionaries 53 | .idea/**/shelf 54 | 55 | # Generated files 56 | .idea/**/contentModel.xml 57 | 58 | # Sensitive or high-churn files 59 | .idea/**/dataSources/ 60 | .idea/**/dataSources.ids 61 | .idea/**/dataSources.local.xml 62 | .idea/**/sqlDataSources.xml 63 | .idea/**/dynamic.xml 64 | .idea/**/uiDesigner.xml 65 | .idea/**/dbnavigator.xml 66 | 67 | # Gradle 68 | .idea/**/gradle.xml 69 | .idea/**/libraries 70 | 71 | # Gradle and Maven with auto-import 72 | # When using Gradle or Maven with auto-import, you should exclude module files, 73 | # since they will be recreated, and may cause churn. Uncomment if using 74 | # auto-import. 75 | # .idea/artifacts 76 | # .idea/compiler.xml 77 | # .idea/jarRepositories.xml 78 | # .idea/modules.xml 79 | # .idea/*.iml 80 | # .idea/modules 81 | # *.iml 82 | # *.ipr 83 | 84 | # CMake 85 | cmake-build-*/ 86 | 87 | # Mongo Explorer plugin 88 | .idea/**/mongoSettings.xml 89 | 90 | # File-based project format 91 | *.iws 92 | 93 | # IntelliJ 94 | out/ 95 | 96 | # mpeltonen/sbt-idea plugin 97 | .idea_modules/ 98 | 99 | # JIRA plugin 100 | atlassian-ide-plugin.xml 101 | 102 | # Cursive Clojure plugin 103 | .idea/replstate.xml 104 | 105 | # Crashlytics plugin (for Android Studio and IntelliJ) 106 | com_crashlytics_export_strings.xml 107 | crashlytics.properties 108 | crashlytics-build.properties 109 | fabric.properties 110 | 111 | # Editor-based Rest Client 112 | .idea/httpRequests 113 | 114 | # Android studio 3.1+ serialized cache file 115 | .idea/caches/build_file_checksums.ser 116 | 117 | ### Windows template 118 | # Windows thumbnail cache files 119 | Thumbs.db 120 | Thumbs.db:encryptable 121 | ehthumbs.db 122 | ehthumbs_vista.db 123 | 124 | # Dump file 125 | *.stackdump 126 | 127 | # Folder config file 128 | [Dd]esktop.ini 129 | 130 | # Recycle Bin used on file shares 131 | $RECYCLE.BIN/ 132 | 133 | # Windows Installer files 134 | *.cab 135 | *.msi 136 | *.msix 137 | *.msm 138 | *.msp 139 | 140 | # Windows shortcuts 141 | *.lnk 142 | 143 | ### Kotlin template 144 | # Compiled class file 145 | *.class 146 | 147 | # Log file 148 | *.log 149 | 150 | # BlueJ files 151 | *.ctxt 152 | 153 | # Mobile Tools for Java (J2ME) 154 | .mtj.tmp/ 155 | 156 | # Package Files # 157 | *.jar 158 | *.war 159 | *.nar 160 | *.ear 161 | *.zip 162 | *.tar.gz 163 | *.rar 164 | 165 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 166 | hs_err_pid* 167 | 168 | ### Kotlin template 169 | # Compiled class file 170 | 171 | # Log file 172 | 173 | # BlueJ files 174 | 175 | # Mobile Tools for Java (J2ME) 176 | 177 | # Package Files # 178 | 179 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 180 | 181 | ### Java template 182 | # Compiled class file 183 | 184 | # Log file 185 | 186 | # BlueJ files 187 | 188 | # Mobile Tools for Java (J2ME) 189 | 190 | # Package Files # 191 | 192 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 193 | 194 | ### Android template 195 | # Built application files 196 | *.apk 197 | *.aar 198 | *.ap_ 199 | *.aab 200 | 201 | # Files for the ART/Dalvik VM 202 | *.dex 203 | 204 | # Java class files 205 | 206 | # Generated files 207 | bin/ 208 | gen/ 209 | # Uncomment the following line in case you need and you don't have the release build type files in your app 210 | # release/ 211 | 212 | # Gradle files 213 | .gradle/ 214 | build/ 215 | 216 | # Local configuration file (sdk path, etc) 217 | local.properties 218 | 219 | # Proguard folder generated by Eclipse 220 | proguard/ 221 | 222 | # Log Files 223 | 224 | # Android Studio Navigation editor temp files 225 | .navigation/ 226 | 227 | # Android Studio captures folder 228 | captures/ 229 | 230 | # IntelliJ 231 | *.iml 232 | .idea/workspace.xml 233 | .idea/tasks.xml 234 | .idea/gradle.xml 235 | .idea/assetWizardSettings.xml 236 | .idea/dictionaries 237 | .idea/libraries 238 | # Android Studio 3 in .gitignore file. 239 | .idea/caches 240 | .idea/modules.xml 241 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you 242 | .idea/navEditor.xml 243 | 244 | # Keystore files 245 | # Uncomment the following lines if you do not want to check your keystore files in. 246 | #*.jks 247 | #*.keystore 248 | 249 | # External native build folder generated in Android Studio 2.2 and later 250 | .externalNativeBuild 251 | .cxx/ 252 | 253 | # Google Services (e.g. APIs or Firebase) 254 | # google-services.json 255 | 256 | # Freeline 257 | freeline.py 258 | freeline/ 259 | freeline_project_description.json 260 | 261 | # fastlane 262 | fastlane/report.xml 263 | fastlane/Preview.html 264 | fastlane/screenshots 265 | fastlane/test_output 266 | fastlane/readme.md 267 | 268 | # Version control 269 | vcs.xml 270 | 271 | # lint 272 | lint/intermediates/ 273 | lint/generated/ 274 | lint/outputs/ 275 | lint/tmp/ 276 | # lint/reports/ 277 | 278 | # Android Profiling 279 | *.hprof 280 | 281 | ### JetBrains template 282 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 283 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 284 | 285 | # User-specific stuff 286 | 287 | # Generated files 288 | 289 | # Sensitive or high-churn files 290 | 291 | # Gradle 292 | 293 | # Gradle and Maven with auto-import 294 | # When using Gradle or Maven with auto-import, you should exclude module files, 295 | # since they will be recreated, and may cause churn. Uncomment if using 296 | # auto-import. 297 | # .idea/artifacts 298 | # .idea/compiler.xml 299 | # .idea/jarRepositories.xml 300 | # .idea/modules.xml 301 | # .idea/*.iml 302 | # .idea/modules 303 | # *.iml 304 | # *.ipr 305 | 306 | # CMake 307 | 308 | # Mongo Explorer plugin 309 | 310 | # File-based project format 311 | 312 | # IntelliJ 313 | 314 | # mpeltonen/sbt-idea plugin 315 | 316 | # JIRA plugin 317 | 318 | # Cursive Clojure plugin 319 | 320 | # Crashlytics plugin (for Android Studio and IntelliJ) 321 | 322 | # Editor-based Rest Client 323 | 324 | # Android studio 3.1+ serialized cache file 325 | 326 | ### Gradle template 327 | .gradle 328 | **/build/ 329 | !src/**/build/ 330 | 331 | # Ignore Gradle GUI config 332 | gradle-app.setting 333 | 334 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 335 | !gradle-wrapper.jar 336 | 337 | # Cache of project 338 | .gradletasknamecache 339 | 340 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 341 | # gradle/wrapper/gradle-wrapper.properties 342 | 343 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | KspExample -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/libraries-with-intellij-classes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 64 | 65 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | kotlin("jvm") 3 | } 4 | 5 | repositories { 6 | mavenCentral() 7 | google() 8 | } 9 | 10 | -------------------------------------------------------------------------------- /example/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.google.devtools.ksp") 3 | kotlin("jvm") 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | google() 9 | } 10 | 11 | dependencies { 12 | implementation(kotlin("stdlib")) 13 | implementation(project(":processor")) 14 | ksp(project(":processor")) 15 | } 16 | 17 | sourceSets.main { 18 | java.srcDirs("src/main/kotlin") 19 | } 20 | 21 | ksp { 22 | arg("enabled", "true") 23 | } 24 | -------------------------------------------------------------------------------- /example/src/main/kotlin/me/tatocaster/Playground.kt: -------------------------------------------------------------------------------- 1 | package me.tatocaster 2 | 3 | import me.tatocaster.generated.SimpleGeneratedClass 4 | 5 | fun main() { 6 | println("start") 7 | 8 | val generatedClass = SimpleGeneratedClass() 9 | println("option with the name {enabled} passed to KSP via Gradle: ${generatedClass.enabled}") 10 | 11 | val testClassPrint = TestClassPrint() 12 | testClassPrint.printAllProperties() 13 | } -------------------------------------------------------------------------------- /example/src/main/kotlin/me/tatocaster/TestClass.kt: -------------------------------------------------------------------------------- 1 | package me.tatocaster 2 | 3 | import me.tatocaster.annotation.Print 4 | 5 | @Print 6 | class TestClass { 7 | private val stringProperty = "123" 8 | private val intProperty = 456 9 | var booleanProperty: Boolean = true 10 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | kotlinVersion=1.5.21 3 | kspVersion=1.5.21-1.0.0-beta05 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tatocaster/KSP-Example/1d5e7a78da439c16edc4e87c388086cef90a9004/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /processor/build.gradle.kts: -------------------------------------------------------------------------------- 1 | val kspVersion: String by project 2 | 3 | plugins { 4 | id("com.google.devtools.ksp") 5 | kotlin("jvm") 6 | } 7 | 8 | group = "me.tatocaster" 9 | version = "1.0" 10 | 11 | repositories { 12 | mavenCentral() 13 | google() 14 | } 15 | 16 | dependencies { 17 | implementation(kotlin("stdlib")) 18 | implementation("com.squareup:kotlinpoet:1.9.0") 19 | implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") 20 | implementation("com.google.auto.service:auto-service-annotations:1.0") 21 | ksp("dev.zacsweers.autoservice:auto-service-ksp:0.5.2") 22 | } 23 | 24 | sourceSets.main { 25 | java.srcDirs("src/main/kotlin") 26 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/PrintProcessor.kt: -------------------------------------------------------------------------------- 1 | import com.google.devtools.ksp.processing.* 2 | import com.google.devtools.ksp.symbol.* 3 | import com.google.devtools.ksp.validate 4 | import com.squareup.kotlinpoet.* 5 | 6 | class PrintProcessor( 7 | private val codeGenerator: CodeGenerator, 8 | private val logger: KSPLogger, 9 | private val options: Map 10 | ) : SymbolProcessor { 11 | override fun process(resolver: Resolver): List { 12 | val symbols = resolver.getSymbolsWithAnnotation("me.tatocaster.annotation.Print") 13 | val invalidSymbols = symbols.filter { !it.validate() }.toList() 14 | symbols 15 | .filter { it is KSClassDeclaration && it.validate() } 16 | .forEach { it.accept(PrintVisitor(), Unit) } 17 | return invalidSymbols 18 | } 19 | 20 | inner class PrintVisitor : KSVisitorVoid() { 21 | override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { 22 | logger.logging("PrintProcessor#visitClassDeclaration", classDeclaration) 23 | val packageName = classDeclaration.containingFile!!.packageName.asString() 24 | val className = "${classDeclaration.simpleName.asString()}Print" 25 | val fileKotlinPoet = FileSpec.builder(packageName, className) 26 | 27 | val functionBuilder = FunSpec.builder("printAllProperties") 28 | classDeclaration.getAllProperties().forEach { 29 | functionBuilder.addStatement( 30 | "println(%S)", 31 | "${it.simpleName.asString()}: ${it.type.resolve()}" 32 | ).build() 33 | } 34 | 35 | fileKotlinPoet.addType( 36 | TypeSpec.classBuilder(className) 37 | .addFunction( 38 | functionBuilder.build() 39 | ).build() 40 | ).build() 41 | fileKotlinPoet.build().writeTo(codeGenerator, Dependencies(true, classDeclaration.containingFile!!)) 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/PrintProcessorProvider.kt: -------------------------------------------------------------------------------- 1 | import com.google.auto.service.AutoService 2 | import com.google.devtools.ksp.processing.SymbolProcessor 3 | import com.google.devtools.ksp.processing.SymbolProcessorEnvironment 4 | import com.google.devtools.ksp.processing.SymbolProcessorProvider 5 | 6 | @AutoService(SymbolProcessorProvider::class) 7 | class PrintProcessorProvider : SymbolProcessorProvider { 8 | override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { 9 | return PrintProcessor( 10 | environment.codeGenerator, 11 | environment.logger, 12 | environment.options 13 | ) 14 | } 15 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/SimpleClassGeneratorProcessor.kt: -------------------------------------------------------------------------------- 1 | import com.google.devtools.ksp.processing.* 2 | import com.google.devtools.ksp.symbol.KSAnnotated 3 | import com.squareup.kotlinpoet.* 4 | 5 | class SimpleClassGeneratorProcessor( 6 | private val codeGenerator: CodeGenerator, 7 | private val logger: KSPLogger, 8 | private val options: Map 9 | ) : SymbolProcessor { 10 | // we are not going to use visitor so manually avoid the processing over and over 11 | private var visited = false 12 | 13 | override fun process(resolver: Resolver): List { 14 | if (visited) { 15 | return emptyList() 16 | } 17 | val name = "SimpleGeneratedClass" 18 | val fileKotlinPoet = FileSpec.builder("me.tatocaster.generated", name) 19 | .addType( 20 | TypeSpec.classBuilder(name) 21 | .addProperty( 22 | PropertySpec.builder("enabled", Boolean::class) 23 | .initializer(options["enabled"] ?: "false") 24 | .build() 25 | ) 26 | .build() 27 | ) 28 | .build() 29 | fileKotlinPoet.writeTo(codeGenerator) 30 | visited = true 31 | return emptyList() 32 | } 33 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/SimpleClassGeneratorProcessorProvider.kt: -------------------------------------------------------------------------------- 1 | import com.google.auto.service.AutoService 2 | import com.google.devtools.ksp.processing.SymbolProcessor 3 | import com.google.devtools.ksp.processing.SymbolProcessorEnvironment 4 | import com.google.devtools.ksp.processing.SymbolProcessorProvider 5 | 6 | @AutoService(SymbolProcessorProvider::class) 7 | class SimpleClassGeneratorProcessorProvider : SymbolProcessorProvider { 8 | override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { 9 | return SimpleClassGeneratorProcessor( 10 | environment.codeGenerator, 11 | environment.logger, 12 | environment.options 13 | ) 14 | } 15 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/Utils.kt: -------------------------------------------------------------------------------- 1 | import com.google.devtools.ksp.processing.CodeGenerator 2 | import com.google.devtools.ksp.processing.Dependencies 3 | import com.squareup.kotlinpoet.FileSpec 4 | import java.io.OutputStreamWriter 5 | import java.nio.charset.StandardCharsets.UTF_8 6 | 7 | fun FileSpec.writeTo(codeGenerator: CodeGenerator, dependencies: Dependencies = Dependencies(false)) { 8 | val file = codeGenerator.createNewFile(dependencies, packageName, name) 9 | OutputStreamWriter(file, UTF_8).use(::writeTo) 10 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/annotation/Print.kt: -------------------------------------------------------------------------------- 1 | package me.tatocaster.annotation 2 | 3 | annotation class Print 4 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | KSP example 2 | --- 3 | Exploring Kotlin Symbol Processing - KSP. This is just an experiment. 4 | 5 | Project contains 2 modules 6 | - Processing 7 | - Example 8 | 9 | Processing module is the one which is responsible for code generation. 10 | There are two samples: `PrintProcessor` and `SimpleClassGeneratorProcessor`. 11 | 12 | - `SimpleClassGeneratorProcessor` generates the class with the arguments passed to processor from Gradle 13 | - `PrintProcessor` generates class which prints all properties with the names and types from the class annotated with "@Print". 14 | Also it shows how annotation parsing is done in KSP and utilizes visitor pattern. 15 | 16 | Project uses Zac Sweers's [`AutoService`](https://github.com/ZacSweers/auto-service-ksp) which is using 17 | KSP already instead of KAPT and KotlinPoet. 18 | 19 | Keep in mind : *"The reduction of build times is only applicable if there are no other processors that use KAPT."* - 20 | Source: https://developer.android.com/jetpack/androidx/releases/room#2.3.0-beta02 21 | 22 | ## Processor Options 23 | Processor options in `SymbolProcessorEnvironment.options` are specified in gradle build scripts: 24 | ``` 25 | ksp { 26 | arg("enabled", "true") 27 | } 28 | ``` 29 | 30 | ## IDEs and Generated Code 31 | Mark directory as a "generated source root" to make it resolvable 32 | ``` 33 | build/generated/ksp/main/kotlin/ 34 | ``` 35 | 36 | and also "resource directory": 37 | ``` 38 | build/generated/ksp/main/resources 39 | ``` 40 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | val kotlinVersion: String by settings 3 | val kspVersion: String by settings 4 | repositories { 5 | gradlePluginPortal() 6 | google() 7 | } 8 | plugins { 9 | id("com.google.devtools.ksp") version kspVersion 10 | kotlin("jvm") version kotlinVersion 11 | } 12 | } 13 | 14 | rootProject.name = "KspExample" 15 | 16 | include(":processor") 17 | include("example") 18 | --------------------------------------------------------------------------------