-
Notifications
You must be signed in to change notification settings - Fork 900
Add error prone checks for internal javadoc and private constructors #6844
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
Merged
jack-berg
merged 3 commits into
open-telemetry:main
from
jack-berg:error-prone-custom-checks
Nov 1, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
} | ||
|
||
dependencies { | ||
implementation("com.google.errorprone:error_prone_core") | ||
|
||
testImplementation("com.google.errorprone:error_prone_test_helpers") | ||
} | ||
|
||
otelJava.moduleName.set("io.opentelemetry.javaagent.customchecks") | ||
|
||
// We cannot use "--release" javac option here because that will forbid exporting com.sun.tools package. | ||
// We also can't seem to use the toolchain without the "--release" option. So disable everything. | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
toolchain { | ||
languageVersion.set(null as JavaLanguageVersion?) | ||
} | ||
} | ||
|
||
tasks { | ||
withType<JavaCompile>().configureEach { | ||
with(options) { | ||
release.set(null as Int?) | ||
|
||
compilerArgs.addAll( | ||
listOf( | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", | ||
), | ||
) | ||
} | ||
} | ||
|
||
// only test on java 17+ | ||
val testJavaVersion: String? by project | ||
if (testJavaVersion != null && Integer.valueOf(testJavaVersion) < 17) { | ||
test { | ||
enabled = false | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
// required on jdk17 | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED") | ||
jvmArgs("--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED") | ||
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") | ||
} | ||
|
||
tasks.withType<Javadoc>().configureEach { | ||
// using com.sun.tools.javac.api.JavacTrees breaks javadoc generation | ||
enabled = false | ||
} | ||
|
||
// Our conventions apply this project as a dependency in the errorprone configuration, which would cause | ||
// a circular dependency if trying to compile this project with that still there. So we filter this | ||
// project out. | ||
configurations { | ||
named("errorprone") { | ||
dependencies.removeIf { | ||
it is ProjectDependency && it.dependencyProject == project | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
custom-checks/src/main/java/io/opentelemetry/javaagent/customchecks/OtelInternalJavadoc.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.customchecks; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.doctree.DocCommentTree; | ||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.PackageTree; | ||
import com.sun.tools.javac.api.JavacTrees; | ||
import java.util.regex.Pattern; | ||
import javax.annotation.Nullable; | ||
import javax.lang.model.element.Modifier; | ||
|
||
@BugPattern( | ||
summary = | ||
"This public internal class doesn't end with the javadoc disclaimer: \"" | ||
+ OtelInternalJavadoc.EXPECTED_INTERNAL_COMMENT | ||
+ "\"", | ||
severity = WARNING) | ||
public class OtelInternalJavadoc extends BugChecker implements BugChecker.ClassTreeMatcher { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Pattern INTERNAL_PACKAGE_PATTERN = Pattern.compile("\\binternal\\b"); | ||
|
||
static final String EXPECTED_INTERNAL_COMMENT = | ||
"This class is internal and is hence not for public use." | ||
+ " Its APIs are unstable and can change at any time."; | ||
|
||
@Override | ||
public Description matchClass(ClassTree tree, VisitorState state) { | ||
if (!isPublic(tree) || !isInternal(state) || tree.getSimpleName().toString().endsWith("Test")) { | ||
return Description.NO_MATCH; | ||
} | ||
String javadoc = getJavadoc(state); | ||
if (javadoc != null && javadoc.contains(EXPECTED_INTERNAL_COMMENT)) { | ||
return Description.NO_MATCH; | ||
} | ||
return describeMatch(tree); | ||
} | ||
|
||
private static boolean isPublic(ClassTree tree) { | ||
return tree.getModifiers().getFlags().contains(Modifier.PUBLIC); | ||
} | ||
|
||
private static boolean isInternal(VisitorState state) { | ||
PackageTree packageTree = state.getPath().getCompilationUnit().getPackage(); | ||
if (packageTree == null) { | ||
return false; | ||
} | ||
String packageName = state.getSourceForNode(packageTree.getPackageName()); | ||
return packageName != null && INTERNAL_PACKAGE_PATTERN.matcher(packageName).find(); | ||
} | ||
|
||
@Nullable | ||
private static String getJavadoc(VisitorState state) { | ||
DocCommentTree docCommentTree = | ||
JavacTrees.instance(state.context).getDocCommentTree(state.getPath()); | ||
if (docCommentTree == null) { | ||
return null; | ||
} | ||
return docCommentTree.toString().replace("\n", ""); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...n/java/io/opentelemetry/javaagent/customchecks/OtelPrivateConstructorForUtilityClass.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.customchecks; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.PrivateConstructorForUtilityClass; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.tree.ClassTree; | ||
|
||
@BugPattern( | ||
summary = | ||
"Classes which are not intended to be instantiated should be made non-instantiable with a private constructor. This includes utility classes (classes with only static members), and the main class.", | ||
severity = WARNING) | ||
public class OtelPrivateConstructorForUtilityClass extends BugChecker | ||
implements BugChecker.ClassTreeMatcher { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final PrivateConstructorForUtilityClass delegate = | ||
new PrivateConstructorForUtilityClass(); | ||
|
||
@Override | ||
public Description matchClass(ClassTree tree, VisitorState state) { | ||
Description description = delegate.matchClass(tree, state); | ||
if (description == NO_MATCH) { | ||
return description; | ||
} | ||
return describeMatch(tree); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...-checks/src/main/resources/META-INF/services/com.google.errorprone.bugpatterns.BugChecker
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
io.opentelemetry.javaagent.customchecks.OtelInternalJavadoc | ||
io.opentelemetry.javaagent.customchecks.OtelPrivateConstructorForUtilityClass |
24 changes: 24 additions & 0 deletions
24
...checks/src/test/java/io/opentelemetry/javaagent/customchecks/OtelInternalJavadocTest.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.customchecks; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OtelInternalJavadocTest { | ||
|
||
@Test | ||
void test() { | ||
doTest("internal/InternalJavadocPositiveCases.java"); | ||
doTest("internal/InternalJavadocNegativeCases.java"); | ||
} | ||
|
||
private static void doTest(String path) { | ||
CompilationTestHelper.newInstance(OtelInternalJavadoc.class, OtelInternalJavadocTest.class) | ||
.addSourceFile(path) | ||
.doTest(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ources/io/opentelemetry/javaagent/customchecks/internal/InternalJavadocNegativeCases.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.customchecks.internal; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public class InternalJavadocNegativeCases { | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public static class One {} | ||
|
||
static class Two {} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ources/io/opentelemetry/javaagent/customchecks/internal/InternalJavadocPositiveCases.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.customchecks.internal; | ||
|
||
// BUG: Diagnostic contains: doesn't end with the javadoc disclaimer | ||
public class InternalJavadocPositiveCases { | ||
|
||
// BUG: Diagnostic contains: doesn't end with the javadoc disclaimer | ||
public static class One {} | ||
|
||
/** Doesn't have the disclaimer. */ | ||
// BUG: Diagnostic contains: doesn't end with the javadoc disclaimer | ||
public static class Two {} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you deliberately keep the javaagent package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, good catch!