Skip to content

Commit

Permalink
feat: add java linting (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Aug 29, 2024
1 parent 6463b0f commit 76edf94
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions .aspect/cli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ lint:
- //tools/lint:linters.bzl%eslint
- //tools/lint:linters.bzl%shellcheck
- //tools/lint:linters.bzl%ruff
- //tools/lint:linters.bzl%pmd
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports_files(
".ruff.toml",
".shellcheckrc",
"buf.yaml",
"pmd.xml",
"pyproject.toml",
],
)
Expand Down
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ bazel_dep(name = "toolchains_protoc", version = "0.3.1")
# with HTTP and make it available. The syntax is slightly different,
# demonstrated here.

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_jar = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")

http_archive(
name = "net_sourceforge_pmd",
build_file_content = """java_import(name = "net_sourceforge_pmd", jars = glob(["*.jar"]), visibility = ["//visibility:public"])""",
sha256 = "21acf96d43cb40d591cacccc1c20a66fc796eaddf69ea61812594447bac7a11d",
strip_prefix = "pmd-bin-6.55.0/lib",
url = "https://github.com/pmd/pmd/releases/download/pmd_releases/6.55.0/pmd-bin-6.55.0.zip",
)

http_jar(
name = "google-java-format",
sha256 = "33068bbbdce1099982ec1171f5e202898eb35f2919cf486141e439fc6e3a4203",
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions logger/client/src/build/aspect/JavaLoggingClientLibrary.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package build.aspect;

import build.aspect.examples.LogMessage;
import build.aspect.examples.LoggerGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import build.aspect.examples.LogMessage;
import build.aspect.examples.LoggerGrpc;

public class JavaLoggingClientLibrary {
private static final Logger logger = Logger.getLogger(JavaLoggingClientLibrary.class.getName());
Expand All @@ -24,6 +24,9 @@ public JavaLoggingClientLibrary(String host, int port) {
.build());
}

// this is confusing and probably a bug
protected void finalize(int a) {}

/** Construct client for accessing the Logging server using the existing channel. */
JavaLoggingClientLibrary(ManagedChannel channel) {
this.channel = channel;
Expand All @@ -45,5 +48,4 @@ public void sendLogMessageToServer(String message) {
return;
}
}

}
11 changes: 11 additions & 0 deletions pmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="pmd-ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

<description>Error</description>

<rule ref="category/java/errorprone.xml/FinalizeOverloaded" />

</ruleset>
7 changes: 7 additions & 0 deletions tools/lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TODO(alex): add Java linting

load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin")
load("@rules_java//java:defs.bzl", "java_binary")

package(default_visibility = ["//:__subpackages__"])

Expand All @@ -15,6 +16,12 @@ alias(

eslint_bin.eslint_binary(name = "eslint")

java_binary(
name = "pmd",
main_class = "net.sourceforge.pmd.PMD",
runtime_deps = ["@net_sourceforge_pmd"],
)

native_binary(
name = "clang_tidy",
src = select(
Expand Down
6 changes: 6 additions & 0 deletions tools/lint/linters.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load("@aspect_rules_lint//lint:buf.bzl", "lint_buf_aspect")
load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect")
load("@aspect_rules_lint//lint:eslint.bzl", "lint_eslint_aspect")
load("@aspect_rules_lint//lint:pmd.bzl", "lint_pmd_aspect")
load("@aspect_rules_lint//lint:ruff.bzl", "lint_ruff_aspect")
load("@aspect_rules_lint//lint:shellcheck.bzl", "lint_shellcheck_aspect")

Expand Down Expand Up @@ -40,3 +41,8 @@ clang_tidy = lint_clang_tidy_aspect(
angle_includes_are_system = False,
verbose = False,
)

pmd = lint_pmd_aspect(
binary = "@@//tools/lint:pmd",
rulesets = ["@@//:pmd.xml"],
)

0 comments on commit 76edf94

Please sign in to comment.