├── .editorconfig ├── .gitignore ├── LICENSE.txt ├── README.md ├── build.gradle.kts ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── plugin-compiler-tests ├── build.gradle.kts └── src │ ├── main │ └── java │ │ └── lombok │ │ └── Generated.java │ └── test │ └── kotlin │ └── nl │ └── fabianm │ └── kotlin │ └── plugin │ └── generated │ └── tests │ └── DataClassTest.kt ├── plugin-compiler ├── build.gradle.kts └── src │ └── main │ ├── kotlin │ └── nl │ │ └── fabianm │ │ └── kotlin │ │ └── plugin │ │ └── generated │ │ └── compiler │ │ ├── GeneratedClassBuilderInterceptorExtension.kt │ │ └── GeneratedPlugin.kt │ └── resources │ └── META-INF │ └── services │ ├── org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor │ └── org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar ├── plugin-gradle ├── build.gradle.kts └── src │ └── main │ ├── kotlin │ └── nl │ │ └── fabianm │ │ └── kotlin │ │ └── plugin │ │ └── generated │ │ └── gradle │ │ ├── GeneratedExtension.kt │ │ └── GeneratedSubplugin.kt │ └── resources │ └── META-INF │ └── services │ └── org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin ├── plugin-idea ├── build.gradle.kts └── src │ └── main │ ├── kotlin │ └── nl │ │ └── fabianm │ │ └── kotlin │ │ └── plugin │ │ └── generated │ │ └── idea │ │ ├── GeneratedGradleImportHandler.kt │ │ └── GeneratedImportHandler.kt │ └── resources │ └── META-INF │ └── plugin.xml └── settings.gradle.kts /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 4 9 | 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | indent_style = space 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Gradle 2 | .gradle 3 | build/ 4 | 5 | # Ignore Gradle GUI config 6 | gradle-app.setting 7 | 8 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 9 | !gradle/wrapper/gradle-wrapper.jar 10 | 11 | # Cache of project 12 | .gradletasknamecache 13 | 14 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 15 | # gradle/wrapper/gradle-wrapper.properties 16 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kotlin-plugin-generated 2 | A Kotlin compiler plugin that annotates Kotlin-generated methods with 3 | `lombok.Generated` to signify to code analyzers that these methods have been 4 | generated by the compiler. 5 | 6 | ## JaCoCo solution (new) 7 | Please note that JaCoCo landed support for recognizing Kotlin-generated 8 | code in 0.8.2, so we advise using that instead if possible. 9 | It has the advantage of not polluting your code with `lombok.Generated` 10 | annotations. 11 | 12 | In Gradle, you can do that as follows: 13 | 14 | ```groovy 15 | jacoco { 16 | toolVersion = '0.8.2' 17 | } 18 | ``` 19 | 20 | ## Gradle 21 | To use the plugin, just apply the plugin in your `build.gradle`: 22 | 23 | ```groovy 24 | plugins { 25 | id 'nl.fabianm.kotlin.plugin.generated' version '1.5.0' 26 | } 27 | ``` 28 | 29 | Make sure you install the version that is compatible with the Kotlin 30 | version you are using: 31 | 32 | | **Kotlin** | **Plugin** | 33 | |:----------:|:----------:| 34 | | 1.2.* | 1.0 | 35 | | 1.3.0+ | 1.3.3 | 36 | | 1.3.50+| 1.4.0 | 37 | | 1.3.61+| 1.5.0 | 38 | 39 | You can optionally configure the plugin as shown below: 40 | 41 | ```groovy 42 | kotlinGenerated { 43 | // The annotation to annotate the generated methods with 44 | annotation = "lombok.Generated" 45 | 46 | // A flag to indicate whether the annotation should be visibile at runtime 47 | visible = true 48 | } 49 | ``` 50 | 51 | ## IntelliJ 52 | To be able to use projects utilizing this plugin within IntelliJ IDEA, 53 | you need to install the IntelliJ plugin (see [#4](https://github.com/fabianishere/kotlin-plugin-generated/issues/4) for more information). You can find the plugin on the 54 | [Releases](https://github.com/fabianishere/kotlin-plugin-generated/releases) page, labeled `plugin-idea-*.zip` 55 | 56 | ## Command Line 57 | To use the plugin from the command line, invoke `kotlinc` using the 58 | following command line arguments: 59 | 60 | ```bash 61 | kotlinc -Xplugin="" -P plugin:nl.fabianm.kotlin.plugin.generated:annotation="lombok.Generated" -P plugin:nl.fabianm.kotlin.plugin.generated:visible=true ... 62 | ``` 63 | 64 | ## License 65 | The code is released under the Apache version 2.0 license. See the 66 | [LICENSE.txt](/LICENSE.txt) file. 67 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | kotlin("jvm") version "1.3.60" apply false 19 | id("org.jlleitschuh.gradle.ktlint") version "8.2.0" apply false 20 | id("com.github.johnrengelman.shadow") version "5.0.0" apply false 21 | } 22 | 23 | allprojects { 24 | group = "nl.fabianm.kotlin.plugin.generated" 25 | version = "1.5.0" 26 | 27 | extra["junitJupiterVersion"] = "5.4.2" 28 | extra["junitPlatformVersion"] = "1.4.2" 29 | } 30 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabianishere/kotlin-plugin-generated/5fbd9251d3c9ee9511c4741d614031d5e8352857/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-5.4.1-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 | # http://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 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin, switch paths to Windows format before running java 129 | if $cygwin ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /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 http://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 Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /plugin-compiler-tests/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 18 | 19 | plugins { 20 | kotlin("jvm") 21 | id("org.jlleitschuh.gradle.ktlint") 22 | jacoco 23 | } 24 | 25 | repositories { 26 | jcenter() 27 | } 28 | 29 | dependencies { 30 | compile("org.jetbrains.kotlin:kotlin-stdlib") 31 | runtimeOnly(project(":plugin-compiler", configuration = "embeddable")) 32 | 33 | val junitJupiterVersion: String by extra 34 | val junitPlatformVersion: String by extra 35 | 36 | testImplementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") 37 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") 38 | testImplementation("org.junit.platform:junit-platform-launcher:$junitPlatformVersion") 39 | } 40 | 41 | /* Compilation */ 42 | tasks.withType { 43 | kotlinOptions { 44 | // Obtain the path to the embeddable (shaded) compiler plugin via the runtime classpath 45 | val pluginJar = configurations.runtimeClasspath.get().first { it.name.startsWith("plugin-compiler") } 46 | 47 | jvmTarget = "1.8" 48 | // We force the plugin to make the annotations visible at runtime 49 | freeCompilerArgs = listOf("-Xplugin=$pluginJar", "-P", "plugin:nl.fabianm.kotlin.plugin.generated:visible=true") 50 | } 51 | } 52 | 53 | /* Test setup */ 54 | tasks.test { 55 | useJUnitPlatform {} 56 | 57 | testLogging { 58 | events("passed", "skipped", "failed") 59 | } 60 | 61 | reports { 62 | html.isEnabled = true 63 | } 64 | } 65 | 66 | tasks.jacocoTestReport { 67 | sourceSets(sourceSets.main.get(), sourceSets.test.get()) 68 | } 69 | -------------------------------------------------------------------------------- /plugin-compiler-tests/src/main/java/lombok/Generated.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package lombok; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * Annotate methods that should be excluded from code generated analysis by JaCoCo 23 | */ 24 | @Documented 25 | @Retention(RetentionPolicy.RUNTIME) 26 | @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE}) 27 | public @interface Generated { 28 | } 29 | -------------------------------------------------------------------------------- /plugin-compiler-tests/src/test/kotlin/nl/fabianm/kotlin/plugin/generated/tests/DataClassTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.tests 18 | 19 | import lombok.Generated 20 | import org.junit.jupiter.api.Assertions.assertFalse 21 | import org.junit.jupiter.api.Assertions.assertTrue 22 | import org.junit.jupiter.api.Test 23 | 24 | /** 25 | * Test the code generated of data classes 26 | */ 27 | internal class DataClassTest { 28 | data class DataClass( 29 | val name: String, 30 | val ssn: String 31 | ) { 32 | val fine: String = name 33 | val test: String get() = name 34 | 35 | override fun toString(): String { 36 | return "tests" 37 | } 38 | } 39 | 40 | @Test 41 | fun `getters are correctly annotated`() { 42 | assertTrue(DataClass::class.java.getMethod("getName").isAnnotationPresent(Generated::class.java)) 43 | } 44 | 45 | @Test 46 | fun `custom getters are not annotated`() { 47 | assertFalse(DataClass::class.java.getMethod("getTest").isAnnotationPresent(Generated::class.java)) 48 | } 49 | 50 | @Test 51 | fun `custom toString is not annotated`() { 52 | assertFalse(DataClass::class.java.getMethod("toString").isAnnotationPresent(Generated::class.java)) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /plugin-compiler/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 18 | 19 | plugins { 20 | kotlin("jvm") 21 | id("org.jlleitschuh.gradle.ktlint") 22 | id("com.github.johnrengelman.shadow") 23 | } 24 | 25 | description = "A compiler plugin for Kotlin that marks Kotlin-generated code with an annotation." 26 | 27 | repositories { 28 | jcenter() 29 | } 30 | 31 | dependencies { 32 | compile(kotlin("stdlib")) 33 | compileOnly(kotlin("compiler")) 34 | } 35 | 36 | /* Compilation */ 37 | tasks.withType { 38 | kotlinOptions { 39 | jvmTarget = "1.8" 40 | } 41 | } 42 | 43 | tasks.jar { 44 | manifest { 45 | attributes["Specification-Title"] = project.name 46 | attributes["Specification-Version"] = project.version 47 | attributes["Implementation-Title"] = "nl.fabianm.kotlin.plugin.generated.compiler" 48 | attributes["Implementation-Version"] = project.version 49 | } 50 | } 51 | 52 | tasks.shadowJar { 53 | configurations = listOf() 54 | archiveClassifier.set("embeddable") 55 | relocate("com.intellij", "org.jetbrains.kotlin.com.intellij") 56 | } 57 | 58 | // Create embeddable configuration 59 | configurations.create("embeddable") { 60 | extendsFrom(configurations.shadow.get()) 61 | } 62 | -------------------------------------------------------------------------------- /plugin-compiler/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/compiler/GeneratedClassBuilderInterceptorExtension.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.compiler 18 | 19 | import com.intellij.psi.PsiElement 20 | import jdk.internal.org.objectweb.asm.Type 21 | import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity 22 | import org.jetbrains.kotlin.cli.common.messages.MessageCollector 23 | import org.jetbrains.kotlin.codegen.ClassBuilder 24 | import org.jetbrains.kotlin.codegen.ClassBuilderFactory 25 | import org.jetbrains.kotlin.codegen.DelegatingClassBuilder 26 | import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension 27 | import org.jetbrains.kotlin.descriptors.FunctionDescriptor 28 | import org.jetbrains.kotlin.diagnostics.DiagnosticSink 29 | import org.jetbrains.kotlin.name.FqName 30 | import org.jetbrains.kotlin.psi.KtClass 31 | import org.jetbrains.kotlin.psi.KtParameter 32 | import org.jetbrains.kotlin.psi.KtProperty 33 | import org.jetbrains.kotlin.resolve.BindingContext 34 | import org.jetbrains.kotlin.resolve.calls.tower.isSynthesized 35 | import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin 36 | import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind 37 | import org.jetbrains.org.objectweb.asm.MethodVisitor 38 | 39 | /** 40 | * A Kotlin compiler plugin that annotates Kotlin-generated methods with `lombok.Generated` to signify to code analyzers 41 | * that these methods have been generated by the compiler. 42 | * 43 | * @property messageCollector The message collector used to log messages. 44 | * @property annotation The annotation to mark the methods with. 45 | * @property visible A flag to indicate whether the annotation should be visible. 46 | */ 47 | class GeneratedClassBuilderInterceptorExtension( 48 | private val messageCollector: MessageCollector, 49 | private val annotation: FqName, 50 | private val visible: Boolean 51 | ) : ClassBuilderInterceptorExtension { 52 | 53 | /** 54 | * The annotation descriptor that is used to annotate the generated methods. 55 | */ 56 | private val annotationDescriptor = Type.getObjectType(annotation.asString().replace(".", "/")).descriptor 57 | 58 | override fun interceptClassBuilderFactory( 59 | interceptedFactory: ClassBuilderFactory, 60 | bindingContext: BindingContext, 61 | diagnostics: DiagnosticSink 62 | ): ClassBuilderFactory = GeneratedClassBuilderFactory(interceptedFactory) 63 | 64 | private inner class GeneratedClassBuilderFactory( 65 | private val delegateFactory: ClassBuilderFactory 66 | ) : ClassBuilderFactory { 67 | 68 | override fun newClassBuilder(origin: JvmDeclarationOrigin): ClassBuilder = 69 | GeneratedClassBuilder(delegateFactory.newClassBuilder(origin)) 70 | 71 | override fun getClassBuilderMode() = delegateFactory.classBuilderMode 72 | 73 | override fun asText(builder: ClassBuilder?): String? = 74 | delegateFactory.asText((builder as GeneratedClassBuilder).delegateClassBuilder) 75 | 76 | override fun asBytes(builder: ClassBuilder?): ByteArray? = 77 | delegateFactory.asBytes((builder as GeneratedClassBuilder).delegateClassBuilder) 78 | 79 | override fun close() = delegateFactory.close() 80 | } 81 | 82 | private inner class GeneratedClassBuilder( 83 | internal val delegateClassBuilder: ClassBuilder 84 | ) : DelegatingClassBuilder() { 85 | /** 86 | * The current class we are building 87 | */ 88 | private var currentClass: KtClass? = null 89 | 90 | override fun getDelegate() = delegateClassBuilder 91 | 92 | override fun defineClass( 93 | origin: PsiElement?, 94 | version: Int, 95 | access: Int, 96 | name: String, 97 | signature: String?, 98 | superName: String, 99 | interfaces: Array 100 | ) { 101 | if (origin is KtClass) { 102 | currentClass = origin 103 | } 104 | 105 | super.defineClass(origin, version, access, name, signature, superName, interfaces) 106 | } 107 | 108 | override fun newMethod(origin: JvmDeclarationOrigin, access: Int, name: String, desc: String, signature: String?, exceptions: Array?): MethodVisitor { 109 | val descriptor = origin.descriptor as? FunctionDescriptor 110 | val visitor = super.newMethod(origin, access, name, desc, signature, exceptions) 111 | 112 | val mark = heuristicSynthesized(origin) || 113 | heuristicProperty(origin) 114 | 115 | if (mark) { 116 | messageCollector.report(CompilerMessageSeverity.LOGGING, "Generated: Annotating $descriptor") 117 | visitor.visitAnnotation(annotationDescriptor, visible) 118 | } 119 | 120 | return visitor 121 | } 122 | 123 | /** 124 | * A heuristic that detects default getter and setter functions for properties. 125 | */ 126 | private fun heuristicProperty(origin: JvmDeclarationOrigin): Boolean = 127 | origin.element is KtProperty || origin.element is KtParameter 128 | 129 | /** 130 | * A heuristic that detects synthesized methods on a data class like "equals", "copy" and "hashCode". 131 | */ 132 | private fun heuristicSynthesized(origin: JvmDeclarationOrigin): Boolean = 133 | (origin.descriptor as? FunctionDescriptor)?.isSynthesized == true || origin.originKind == JvmDeclarationOriginKind.SYNTHETIC 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /plugin-compiler/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/compiler/GeneratedPlugin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.compiler 18 | 19 | import com.intellij.mock.MockProject 20 | import com.intellij.openapi.extensions.Extensions 21 | import com.intellij.openapi.extensions.impl.ExtensionPointImpl 22 | import com.intellij.openapi.project.Project 23 | import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.ANNOTATION 24 | import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.DEFAULT_ANNOTATION 25 | import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.VISIBLE 26 | import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys 27 | import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity 28 | import org.jetbrains.kotlin.cli.common.messages.MessageCollector 29 | import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension 30 | import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption 31 | import org.jetbrains.kotlin.compiler.plugin.CliOption 32 | import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException 33 | import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor 34 | import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar 35 | import org.jetbrains.kotlin.config.CompilerConfiguration 36 | import org.jetbrains.kotlin.config.CompilerConfigurationKey 37 | import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor 38 | import org.jetbrains.kotlin.name.FqName 39 | 40 | object GeneratedConfigurationKeys { 41 | /** 42 | * The configuration key for specifying the qualified name of the annotation to annotate generated methods with. 43 | */ 44 | val ANNOTATION: CompilerConfigurationKey = 45 | CompilerConfigurationKey.create("annotation qualified name") 46 | 47 | /** 48 | * The configuration key for specifying whether the annotations should be visible during runtime. 49 | */ 50 | val VISIBLE: CompilerConfigurationKey = 51 | CompilerConfigurationKey.create("annotation visibility") 52 | 53 | /** 54 | * By default, the plugin will annotate generated methods with the `lombok.Generated` annotation. 55 | */ 56 | val DEFAULT_ANNOTATION: FqName = FqName("lombok.Generated") 57 | } 58 | 59 | /** 60 | * The [CommandLineProcessor] for this compiler plugin. 61 | */ 62 | class GeneratedCommandLineProcessor : CommandLineProcessor { 63 | companion object { 64 | val ANNOTATION_OPTION = CliOption("annotation", "", "Annotation qualified name", required = false) 65 | val VISIBLE_OPTION = CliOption("visible", "", "Flag to indicate whether annotation should be visible", required = false) 66 | 67 | const val PLUGIN_ID = "nl.fabianm.kotlin.plugin.generated" 68 | } 69 | 70 | override val pluginId = PLUGIN_ID 71 | override val pluginOptions = listOf(ANNOTATION_OPTION, VISIBLE_OPTION) 72 | 73 | override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) = when (option) { 74 | ANNOTATION_OPTION -> configuration.put(ANNOTATION, FqName(value)) 75 | VISIBLE_OPTION -> configuration.put(VISIBLE, value.toBoolean()) 76 | else -> throw CliOptionProcessingException("Unknown option: ${option.optionName}") 77 | } 78 | } 79 | 80 | /** 81 | * The [ComponentRegistrar] for this plugin, which registers the [GeneratedClassBuilderInterceptorExtension]. 82 | */ 83 | class GeneratedComponentRegistrar : ComponentRegistrar { 84 | override fun registerProjectComponents( 85 | project: MockProject, 86 | configuration: CompilerConfiguration 87 | ) { 88 | // see https://github.com/JetBrains/kotlin/blob/1.1.2/plugins/annotation-collector/src/org/jetbrains/kotlin/annotation/AnnotationCollectorPlugin.kt#L92 89 | val messageCollector = configuration.get( 90 | CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, 91 | MessageCollector.NONE 92 | ) 93 | 94 | messageCollector.report(CompilerMessageSeverity.INFO, "Generated: Compiler plugin activated") 95 | 96 | ClassBuilderInterceptorExtension.registerExtensionAsFirst( 97 | project, 98 | GeneratedClassBuilderInterceptorExtension( 99 | messageCollector, 100 | configuration[ANNOTATION] ?: DEFAULT_ANNOTATION, 101 | configuration[VISIBLE] ?: false 102 | ) 103 | ) 104 | } 105 | } 106 | 107 | fun ProjectExtensionDescriptor.registerExtensionAsFirst(project: Project, extension: T) { 108 | Extensions.getArea(project) 109 | .getExtensionPoint(extensionPointName) 110 | .let { it as ExtensionPointImpl } 111 | .registerExtension(extension, {}) 112 | } 113 | -------------------------------------------------------------------------------- /plugin-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor: -------------------------------------------------------------------------------- 1 | nl.fabianm.kotlin.plugin.generated.compiler.GeneratedCommandLineProcessor 2 | -------------------------------------------------------------------------------- /plugin-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar: -------------------------------------------------------------------------------- 1 | nl.fabianm.kotlin.plugin.generated.compiler.GeneratedComponentRegistrar 2 | -------------------------------------------------------------------------------- /plugin-gradle/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 18 | 19 | plugins { 20 | kotlin("jvm") 21 | id("org.jlleitschuh.gradle.ktlint") 22 | `java-gradle-plugin` 23 | `maven-publish` 24 | id("com.gradle.plugin-publish") version "0.10.1" 25 | id("com.jfrog.bintray") version "1.8.4" 26 | } 27 | 28 | description = "Gradle plugin for marking Kotlin-generated code with an annotation." 29 | 30 | repositories { 31 | jcenter() 32 | } 33 | 34 | dependencies { 35 | compile(gradleApi()) 36 | compile(kotlin("stdlib")) 37 | compile(kotlin("gradle-plugin-api")) 38 | compileOnly(project(":plugin-compiler", configuration = "embeddable")) 39 | 40 | runtimeOnly(kotlin("compiler-embeddable")) 41 | } 42 | 43 | /* Compilation */ 44 | tasks.withType { 45 | kotlinOptions { 46 | jvmTarget = "1.8" 47 | } 48 | } 49 | 50 | tasks.jar { 51 | // Embed compiler plugin in jar 52 | from(zipTree(configurations.compileClasspath.get().first { it.name.startsWith("plugin-compiler") })) 53 | 54 | manifest { 55 | attributes["Specification-Title"] = project.name 56 | attributes["Specification-Version"] = project.version 57 | attributes["Implementation-Title"] = "nl.fabianm.kotlin.plugin.generated.gradle" 58 | attributes["Implementation-Version"] = project.version 59 | } 60 | } 61 | 62 | /* Gradle plugin */ 63 | gradlePlugin { 64 | plugins { 65 | create("generated") { 66 | id = "nl.fabianm.kotlin.plugin.generated" 67 | implementationClass = "nl.fabianm.kotlin.plugin.generated.gradle.GeneratedGradleSubplugin" 68 | } 69 | } 70 | } 71 | 72 | /* Publishing */ 73 | pluginBundle { 74 | website = "https://github.com/fabianishere/kotlin-plugin-generated" 75 | vcsUrl = "https://github.com/fabianishere/kotlin-plugin-generated.git" 76 | description = "A Kotlin compiler plugin that annotates Kotlin-generated methods to signify to code analyzers" + 77 | " that these methods have been generated by the compiler." 78 | tags = listOf("kotlin", "compiler", "generated", "plugin", "coverage", "test") 79 | 80 | (plugins) { 81 | "generated" { 82 | id = "nl.fabianm.kotlin.plugin.generated" 83 | displayName = "Kotlin Generated compiler plugin" 84 | } 85 | } 86 | 87 | mavenCoordinates { 88 | groupId = "nl.fabianm.kotlin.plugin.generated" 89 | } 90 | } 91 | 92 | bintray { 93 | user = (project.properties["bintray.user"] ?: System.getenv("BINTRAY_USER")).toString() 94 | key = (project.properties["bintray.key"] ?: System.getenv("BINTRAY_KEY")).toString() 95 | setPublications("pluginMaven", "generatedPluginMarkerMaven") 96 | with(pkg) { 97 | repo = "kotlin-plugin-generated" 98 | name = project.name 99 | userOrg = "fabianishere" 100 | desc = "A Kotlin compiler plugin that annotates Kotlin-generated methods to signify to code analyzers" + 101 | " that these methods have been generated by the compiler." 102 | websiteUrl = "https://github.com/fabianishere/kotlin-plugin-generated" 103 | issueTrackerUrl = "https://github.com/fabianishere/kotlin-plugin-generated/issues" 104 | vcsUrl = "https://github.com/fabianishere/kotlin-plugin-generated.git" 105 | setLicenses("Apache-2.0") 106 | githubRepo = "fabianishere/kotlin-plugin-generated" 107 | githubReleaseNotesFile = "README.md" 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /plugin-gradle/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/gradle/GeneratedExtension.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.gradle 18 | 19 | /** 20 | * The available Gradle configuration for this plugin. 21 | */ 22 | open class GeneratedExtension { 23 | /** 24 | * The annotation to use for annotating generated methods. 25 | */ 26 | var annotation: String? = null 27 | 28 | /** 29 | * A flag to make the annotations visible during runtime. 30 | */ 31 | var visible: Boolean = false 32 | 33 | /** 34 | * Set the annotation to use for annotating generated methods. 35 | */ 36 | open fun annotation(fqName: String) { 37 | annotation = fqName 38 | } 39 | 40 | /** 41 | * Set the runtime visibility of the annotations. 42 | */ 43 | open fun visible(visible: Boolean) { 44 | this.visible = visible 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /plugin-gradle/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/gradle/GeneratedSubplugin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.gradle 18 | 19 | import org.gradle.api.Plugin 20 | import org.gradle.api.Project 21 | import org.gradle.api.tasks.compile.AbstractCompile 22 | import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions 23 | import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation 24 | import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin 25 | import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact 26 | import org.jetbrains.kotlin.gradle.plugin.SubpluginOption 27 | 28 | /** 29 | * The project-level Gradle plugin behavior that is used specifying the plugin's configuration through the 30 | * [GeneratedExtension] class. 31 | */ 32 | class GeneratedGradleSubplugin : Plugin { 33 | companion object { 34 | fun isEnabled(project: Project) = project.plugins.findPlugin(GeneratedGradleSubplugin::class.java) != null 35 | 36 | fun getGeneratedExtension(project: Project): GeneratedExtension { 37 | return project.extensions.getByType(GeneratedExtension::class.java) 38 | } 39 | } 40 | 41 | override fun apply(project: Project) { 42 | project.extensions.create("kotlinGenerated", GeneratedExtension::class.java) 43 | } 44 | } 45 | 46 | /** 47 | * The compilation-level Gradle plugin for applying the compiler plugin to the Kotlin compiler configuration. 48 | */ 49 | class GeneratedKotlinGradleSubplugin : KotlinGradleSubplugin { 50 | companion object { 51 | private const val GENERATED_ARTIFACT_NAME = "plugin-gradle" 52 | private const val GENERATED_GROUP_ID = "nl.fabianm.kotlin.plugin.generated" 53 | private const val GENERATED_VERSION = "1.5.0" 54 | private const val GENERATED_COMPILER_PLUGIN_ID = "nl.fabianm.kotlin.plugin.generated" 55 | private val ANNOTATION_ARG_NAME = "annotation" 56 | private val VISIBLE_ARG_NAME = "visible" 57 | } 58 | 59 | override fun isApplicable(project: Project, task: AbstractCompile) = GeneratedGradleSubplugin.isEnabled(project) 60 | 61 | override fun apply( 62 | project: Project, 63 | kotlinCompile: AbstractCompile, 64 | javaCompile: AbstractCompile?, 65 | variantData: Any?, 66 | androidProjectHandler: Any?, 67 | kotlinCompilation: KotlinCompilation? 68 | ): List { 69 | if (!GeneratedGradleSubplugin.isEnabled(project)) { 70 | return emptyList() 71 | } 72 | 73 | val extension = project.extensions.findByType(GeneratedExtension::class.java) ?: return emptyList() 74 | return listOfNotNull( 75 | extension.annotation?.let { SubpluginOption(ANNOTATION_ARG_NAME, it) }, 76 | SubpluginOption(VISIBLE_ARG_NAME, extension.visible.toString()) 77 | ) 78 | } 79 | 80 | override fun getPluginArtifact(): SubpluginArtifact = 81 | SubpluginArtifact(GENERATED_GROUP_ID, GENERATED_ARTIFACT_NAME, GENERATED_VERSION) 82 | 83 | override fun getCompilerPluginId() = GENERATED_COMPILER_PLUGIN_ID 84 | } 85 | -------------------------------------------------------------------------------- /plugin-gradle/src/main/resources/META-INF/services/org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin: -------------------------------------------------------------------------------- 1 | nl.fabianm.kotlin.plugin.generated.gradle.GeneratedKotlinGradleSubplugin 2 | -------------------------------------------------------------------------------- /plugin-idea/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 18 | 19 | plugins { 20 | kotlin("jvm") 21 | id("org.jlleitschuh.gradle.ktlint") 22 | id("org.jetbrains.intellij") version "0.4.8" 23 | } 24 | 25 | description = "IntelliJ IDEA plugin for marking Kotlin-generated code with an annotation." 26 | 27 | repositories { 28 | jcenter() 29 | } 30 | 31 | dependencies { 32 | compile(kotlin("stdlib")) 33 | compile(project(":plugin-compiler")) 34 | } 35 | 36 | /* Compilation */ 37 | tasks.withType { 38 | kotlinOptions { 39 | jvmTarget = "1.8" 40 | } 41 | } 42 | 43 | tasks.jar { 44 | manifest { 45 | attributes["Specification-Title"] = project.name 46 | attributes["Specification-Version"] = project.version 47 | attributes["Implementation-Title"] = "nl.fabianm.kotlin.plugin.generated.gradle" 48 | attributes["Implementation-Version"] = project.version 49 | } 50 | } 51 | 52 | /* IntelliJ */ 53 | intellij { 54 | pluginName = "kotlin-plugin-generated" 55 | version = "2019.3" 56 | setPlugins("gradle", "org.jetbrains.kotlin:1.3.61-release-IJ2019.3-1") 57 | } 58 | -------------------------------------------------------------------------------- /plugin-idea/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/idea/GeneratedGradleImportHandler.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.idea 18 | 19 | import com.intellij.openapi.externalSystem.model.DataNode 20 | import com.intellij.openapi.externalSystem.model.project.ModuleData 21 | import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler 22 | import org.jetbrains.kotlin.idea.facet.KotlinFacet 23 | import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData 24 | 25 | class GeneratedGradleImportHandler : GradleProjectImportHandler { 26 | override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) { 27 | GeneratedImportHandler.modifyCompilerArguments(facet, PLUGIN_GRADLE_JAR, PLUGIN_GRADLE_TITLE) 28 | } 29 | 30 | override fun importByModule(facet: KotlinFacet, moduleNode: DataNode) { 31 | GeneratedImportHandler.modifyCompilerArguments(facet, PLUGIN_GRADLE_JAR, PLUGIN_GRADLE_TITLE) 32 | } 33 | 34 | companion object { 35 | private val PLUGIN_GRADLE_JAR = "plugin-gradle" 36 | 37 | /** 38 | * The implementation title to search for. 39 | */ 40 | private val PLUGIN_GRADLE_TITLE = "nl.fabianm.kotlin.plugin.generated.gradle" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /plugin-idea/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/idea/GeneratedImportHandler.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package nl.fabianm.kotlin.plugin.generated.idea 18 | 19 | import com.intellij.openapi.application.PathManager 20 | import com.intellij.openapi.diagnostic.Logger 21 | import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedCommandLineProcessor 22 | import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments 23 | import org.jetbrains.kotlin.idea.facet.KotlinFacet 24 | import java.io.File 25 | import java.io.FileInputStream 26 | import java.util.jar.JarInputStream 27 | 28 | internal object GeneratedImportHandler { 29 | /** 30 | * The path to the IntelliJ compatible compiler plugin version. 31 | */ 32 | private val PLUGIN_JPS_JAR: String? 33 | get() = PathManager.getJarPathForClass(GeneratedCommandLineProcessor::class.java) 34 | 35 | /** 36 | * The [Logger] instance for this class. 37 | */ 38 | private val logger = Logger.getInstance(GeneratedImportHandler::class.java) 39 | 40 | /** 41 | * Modify the compiler arguments for the specified [KotlinFacet] to remove the incompatible compiler plugin from 42 | * the classpath and replace it with an IntelliJ compatible version. 43 | * 44 | * @param facet The facet to modify. 45 | * @param buildSystemPluginJar The name of the jar file to remove rom the classpath. 46 | * @param implementationTitle The implementation title to validate. 47 | */ 48 | fun modifyCompilerArguments(facet: KotlinFacet, buildSystemPluginJar: String, implementationTitle: String) { 49 | logger.info("Probing for Gradle plugin") 50 | 51 | val facetSettings = facet.configuration.settings 52 | val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() 53 | val regex = "(.*\\${File.separator})?$buildSystemPluginJar-.*\\.jar".toRegex() 54 | 55 | // Remove the incompatible compiler plugin from the classpath if found 56 | var isEnabled = false 57 | val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) { 58 | val match = regex.matches(it) && validateJar(it, implementationTitle) 59 | if (match) { 60 | logger.info("$it [match=$match]") 61 | isEnabled = true 62 | } 63 | !match 64 | } 65 | 66 | // Add the compatible compiler plugin version to the classpath if available and is enabled in Gradle 67 | val newPluginClasspaths = if (isEnabled && PLUGIN_JPS_JAR != null) 68 | oldPluginClasspaths + PLUGIN_JPS_JAR!! 69 | else 70 | oldPluginClasspaths 71 | 72 | commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray() 73 | facetSettings.compilerArguments = commonArguments 74 | } 75 | 76 | /** 77 | * Validate whether the specified jar file is actually our compiler plugin. 78 | * 79 | * We need to perform this rather ugly check, because the artifact id of the Gradle plugin is not unique and rather 80 | * general (`plugin-gradle`). We therefore check whether the manifest contains references to this project. 81 | */ 82 | private fun validateJar(path: String, implementationTitle: String): Boolean { 83 | return try { 84 | val jar = JarInputStream(FileInputStream(path)) 85 | val manifest = jar.manifest 86 | manifest.mainAttributes.getValue("Implementation-Title").trim().equals(implementationTitle, ignoreCase = true) 87 | } catch (e: Exception) { 88 | false 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /plugin-idea/src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | Kotlin Plugin Generated 19 | nl.fabianm.kotlin.plugin.generated 20 | 21 | 22 | 26 | 27 | 28 | org.jetbrains.plugins.gradle 29 | org.jetbrains.kotlin 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Fabian Mastenbroek. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | rootProject.name = "kotlin-plugin-generated" 18 | 19 | include(":plugin-compiler") 20 | include(":plugin-compiler-tests") 21 | include(":plugin-gradle") 22 | include(":plugin-idea") 23 | --------------------------------------------------------------------------------