-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error prone checks for internal javadoc and private constructors (#…
- Loading branch information
Showing
30 changed files
with
340 additions
and
12 deletions.
There are no files selected for viewing
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
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
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/gradle/customchecks/OtelInternalJavadoc.java
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
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.gradle.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
...main/java/io/opentelemetry/gradle/customchecks/OtelPrivateConstructorForUtilityClass.java
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
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.gradle.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 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.gradle.customchecks.OtelInternalJavadoc | ||
io.opentelemetry.gradle.customchecks.OtelPrivateConstructorForUtilityClass |
24 changes: 24 additions & 0 deletions
24
...om-checks/src/test/java/io/opentelemetry/gradle/customchecks/OtelInternalJavadocTest.java
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
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.gradle.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
...resources/io/opentelemetry/gradle/customchecks/internal/InternalJavadocNegativeCases.java
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
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.gradle.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
...resources/io/opentelemetry/gradle/customchecks/internal/InternalJavadocPositiveCases.java
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
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.gradle.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 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 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 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 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 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 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 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 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.