Last active
February 7, 2024 10:08
-
-
Save kosiara/233753e4de606eb5bd8b342209090e25 to your computer and use it in GitHub Desktop.
@ParametersAreNonnullByDefault for all subpackages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* File: nonnull.gradle | |
* | |
* Generates package-info.java for appropriate packages | |
* inside src/main/java folder. | |
* | |
* This is a workaround to define @ParametersAreNonnullByDefault for all Java classes in a package | |
* i.e. including all subpackages (note: edit line no. 19). | |
*/ | |
task generateNonNullJavaFiles(dependsOn: "assembleDebug", type: Copy) { | |
group = "Copying" | |
description = "Generate package-info.java classes" | |
def infoFileContentHeader = getFileContentHeader(); | |
def infoFileContentFooter = getFileContentFooter(); | |
def sourceDir = file( "${projectDir}" + File.separatorChar + "src" + File.separatorChar + | |
"main" + File.separatorChar + "java" + File.separatorChar + | |
"com" + File.separatorChar + "company" + File.separatorChar + "name" ) | |
sourceDir.eachDirRecurse { dir -> | |
def infoFilePath = dir.getAbsolutePath() + File.separatorChar + "package-info.java" | |
if (!file(infoFilePath).exists()) { | |
def infoFileContentPackage = getFileContentPackage(dir.getAbsolutePath()); | |
new File(infoFilePath).write(infoFileContentHeader + | |
infoFileContentPackage + infoFileContentFooter) | |
println "[dir] " + infoFilePath + " created"; | |
} | |
} | |
println "[SUCCESS] NonNull generator: package-info.java files checked" | |
} | |
def getFileContentPackage(path) { | |
def mainSrcPhrase = "src" + File.separatorChar + "main" + File.separatorChar + | |
"java" + File.separatorChar | |
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" + | |
" *\n" + | |
" * Make all method parameters @NonNull by default.\n" + | |
" *\n" + | |
" * We assume that all method parameters and return types are NON-NULL by default.\n" + | |
" *\n" + | |
" * e.g.\n" + | |
" *\n" + | |
" * String trimExampleMethod(String value) {\n" + | |
" * return value.trim();\n" + | |
" * }\n" + | |
" *\n" + | |
" * is equal to:\n" + | |
" *\n" + | |
" * @NonNull\n" + | |
" * String trimExampleMethod(@NonNull String value) {\n" + | |
" * return value.trim();\n" + | |
" * }\n" + | |
" *\n" + | |
" * reverse this behaviour with: @Nullable annotation.\n" + | |
" *\n" + | |
" */\n" + | |
"@ParametersAreNonnullByDefault\n" + | |
"@ReturnValuesAreNonnullByDefault\n" | |
} | |
def getFileContentFooter() { | |
return "\n" + | |
"import javax.annotation.ParametersAreNonnullByDefault;\n" + | |
"\n" + | |
"import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace line: 19 with your package name
"com" + File.separatorChar + "company" + File.separatorChar + "name"
Usage: