summaryrefslogtreecommitdiffhomepage
path: root/app/nonnull.gradle
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2018-07-12 19:10:35 -0500
committerJason A. Donenfeld <Jason@zx2c4.com>2018-07-13 03:46:23 +0200
commit67ea8b2936343526ff0b3f476c515f0e11dbb272 (patch)
tree84f904d66f6111a7d8f897164eb236bc62199cae /app/nonnull.gradle
parentfbaa4d9ab1b59ba4610fe273743872f35e7e9091 (diff)
global: Add nullity annotations
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'app/nonnull.gradle')
-rw-r--r--app/nonnull.gradle87
1 files changed, 87 insertions, 0 deletions
diff --git a/app/nonnull.gradle b/app/nonnull.gradle
new file mode 100644
index 00000000..014e999a
--- /dev/null
+++ b/app/nonnull.gradle
@@ -0,0 +1,87 @@
+/*
+ * Copyright © 2018 Eric Kuck <eric@bluelinelabs.com>.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+task generateNonNullJavaFiles(dependsOn: "assembleDebug", type: Copy) {
+ group = "Copying"
+ description = "Generate package-info.java classes"
+
+ def basePackage = "com" + File.separatorChar + "wireguard"
+ def mainSrcPhrase = "src" + File.separatorChar + "main" + File.separatorChar +
+ "java" + File.separatorChar
+ def mainTestSrcPhrase = "src" + File.separatorChar + "test" + File.separatorChar +
+ "java" + File.separatorChar
+ def mainAndroidTestSrcPhrase = "src" + File.separatorChar + "androidTest" + File.separatorChar +
+ "java" + File.separatorChar
+
+ def sourceDir = file( "${projectDir}" + File.separatorChar + "src" + File.separatorChar +
+ "main" + File.separatorChar + "java" + File.separatorChar +
+ basePackage )
+ def testSourceDir = file( "${projectDir}" + File.separatorChar + "src" + File.separatorChar +
+ "test" + File.separatorChar + "java" + File.separatorChar +
+ basePackage)
+ def androidTestSourceDir = file( "${projectDir}" + File.separatorChar + "src" + File
+ .separatorChar +
+ "androidTest" + File.separatorChar + "java" + File.separatorChar +
+ basePackage )
+
+ generateInfoFiles(sourceDir, mainSrcPhrase);
+ sourceDir.eachDirRecurse { dir ->
+ generateInfoFiles(dir, mainSrcPhrase)
+ }
+ if (file(testSourceDir).exists()) {
+ generateInfoFiles(testSourceDir, mainTestSrcPhrase);
+ testSourceDir.eachDirRecurse { dir ->
+ generateInfoFiles(dir, mainTestSrcPhrase)
+ }
+ }
+ if (file(androidTestSourceDir).exists()) {
+ generateInfoFiles(androidTestSourceDir, mainAndroidTestSrcPhrase);
+ androidTestSourceDir.eachDirRecurse { dir ->
+ generateInfoFiles(dir, mainAndroidTestSrcPhrase)
+ }
+ }
+ println "[SUCCESS] NonNull generator: package-info.java files checked"
+}
+
+private void generateInfoFiles(File dir, String mainSrcPhrase) {
+ def infoFileContentHeader = getFileContentHeader();
+ def infoFileContentFooter = getFileContentFooter();
+ def infoFilePath = dir.getAbsolutePath() + File.separatorChar + "package-info.java"
+
+ //file(infoFilePath).delete(); //do not use in production code
+ if (!file(infoFilePath).exists()) {
+ def infoFileContentPackage = getFileContentPackage(dir.getAbsolutePath(), mainSrcPhrase);
+ new File(infoFilePath).write(infoFileContentHeader +
+ infoFileContentPackage + infoFileContentFooter)
+ println "[dir] " + infoFilePath + " created";
+ }
+}
+
+def getFileContentPackage(String path, String mainSrcPhrase) {
+ def mainSrcPhraseIndex = path.indexOf(mainSrcPhrase)
+ def output = path.substring(mainSrcPhraseIndex)
+
+ // Win hotfix
+ if (System.properties['os.name'].toLowerCase().contains('windows')) {
+ output = output.replace("\\", "/")
+ mainSrcPhrase = mainSrcPhrase.replace("\\", "/")
+ }
+
+ return "package " + output.replaceAll(mainSrcPhrase, "").replaceAll(
+ "/", ".") + ";\n"
+}
+
+def getFileContentHeader() {
+ return "/**\n" +
+ " * Make all method parameters @NonNull by default.\n" +
+ " */\n" +
+ "@NonNullForAll\n"
+}
+
+def getFileContentFooter() {
+ return "\n" +
+ "import com.wireguard.util.NonNullForAll;\n"
+}
+