Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add static analysis gradle plugins (spotless + palantir, errorprone + nullaway) #2

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
A MongoDB Dialect for the Hibernate ORM

## Build

[Gradle](https://gradle.org/) is the building tool with [Kotlin](https://kotlinlang.org/) DSL as default.
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved

Starting with Gradle 8.2, creating new builds using gradle init defaults to generating Gradle builds using Kotlin DSL.
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved

See https://blog.gradle.org/kotlin-dsl-is-now-the-default-for-new-gradle-builds for further details.

* Kotlin DSL Primer: https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotlin_dsl
* Kotlin DSL API: https://docs.gradle.org/current/kotlin-dsl/index.html
* Migration from Groovy: https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved

## IntelliJ IDEA
41 changes: 41 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
* limitations under the License.
*/

import net.ltgt.gradle.errorprone.errorprone

plugins {
// Apply the java-library plugin for API and implementation separation.
`java-library`
alias(libs.plugins.spotless)
alias(libs.plugins.errorprone)
}

repositories {
Expand All @@ -29,6 +33,11 @@ dependencies {
testImplementation(libs.junit.jupiter)

testRuntimeOnly("org.junit.platform:junit-platform-launcher")

errorprone(libs.nullaway)
api(libs.jspecify)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is documented as right, it seems wrong to have this as an api dependency - as its really for compile time only. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the following statement at https://jspecify.dev/docs/using/

Screenshot 2024-10-21 at 9 56 50 AM

So let us stick to api scope for now and always keep it in mind to switch to lower visibility. Currently given almost zero experience on real usage, let us trust the doc. The switch cost should be minor in the future, if needed.


errorprone(libs.google.errorprone.core)
}

// Apply a specific Java toolchain to ease working on different environments.
Expand All @@ -42,3 +51,35 @@ tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Static Analysis Tasks

spotless {
java {
// note: you can use an empty string for all the imports you didn't specify explicitly, '|' to join group without blank line, and '\\#` prefix for static imports
importOrder("java|javax", "org.hibernate", "com.mongo", "", "\\#")
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved

removeUnusedImports()

// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
cleanthat()
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved

palantirJavaFormat(libs.versions.palantir.get()).style("GOOGLE").formatJavadoc(true)
formatAnnotations() // fixes formatting of type annotations, see below

licenseHeaderFile("copyright.txt") // contains '$YEAR' parameter
}
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
disableWarningsInGeneratedCode.set(true)
option("NullAway:AnnotatedPackages", "com.mongo.hibernate")
}
}
tasks.compileJava {
// The check defaults to a warning, bump it up to an error for the main sources
options.errorprone.error("NullAway")
}

16 changes: 16 additions & 0 deletions copyright.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2008-$YEAR MongoDB, Inc.
NathanQingyangXu marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

13 changes: 13 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

[versions]
junit-jupiter = "5.10.3"
spotless = "6.25.0"
palantir = "2.50.0"
errorprone = "4.0.1"
google-errorprone-core = "2.9.0"
nullaway = "0.10.26"
jspecify = "0.3.0"

[libraries]
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
jspecify = { module = "org.jspecify:jspecify", version.ref = "jspecify" }
nullaway = { module = "com.uber.nullaway:nullaway", version.ref = "nullaway" }
google-errorprone-core = { module = "com.google.errorprone:error_prone_core", version.ref = "google-errorprone-core" }

[plugins]
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
errorprone = { id = "net.ltgt.errorprone", version.ref = "errorprone" }