Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ static void parseResponse(
DockerImage parsedImage = DockerImage.parse(value);
if (parsedImage != null) {
attrBuilders.put(CONTAINER_IMAGE_NAME, parsedImage.getRepository());
attrBuilders.put(
io.opentelemetry.contrib.aws.resource.IncubatingAttributes.CONTAINER_IMAGE_TAGS,
parsedImage.getTag());
attrBuilders.put(IncubatingAttributes.CONTAINER_IMAGE_TAGS, parsedImage.getTag());
}
break;
case "ImageID":
Expand Down
16 changes: 12 additions & 4 deletions buildSrc/src/main/kotlin/otel.errorprone-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {
dependencies {
errorprone("com.google.errorprone:error_prone_core")
errorprone("com.uber.nullaway:nullaway")
errorprone(project(":custom-checks"))
}

val disableErrorProne = properties["disableErrorProne"]?.toString()?.toBoolean() ?: false
Expand Down Expand Up @@ -45,10 +46,6 @@ tasks {
// Suggests using Guava types for fields but we don't use Guava
disable("ImmutableMemberCollection")

// Fully qualified names may be necessary when deprecating a class to avoid
// deprecation warning.
disable("UnnecessarilyFullyQualified")

// TODO (trask) use animal sniffer
disable("Java8ApiChecker")

Expand Down Expand Up @@ -110,3 +107,14 @@ tasks {
}
}
}

// 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.name == project.name
}
}
}
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ tasks {
"-Xlint:-processing",
// We suppress the "options" warning because it prevents compilation on modern JDKs
"-Xlint:-options",
// We suppress the "deprecation" warning because --release 8 causes javac to warn on
// importing deprecated classes (fixed in JDK 9+, see https://bugs.openjdk.org/browse/JDK-8032211).
// We use a custom Error Prone check instead (OtelDeprecatedApiUsage).
"-Xlint:-deprecation",

// Fail build on any warning
"-Werror",
Expand Down
87 changes: 87 additions & 0 deletions custom-checks/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
plugins {
id("otel.java-conventions")
}

dependencies {
compileOnly("com.google.errorprone:error_prone_core")

testImplementation("com.google.errorprone:error_prone_test_helpers")
}

otelJava.moduleName.set("io.opentelemetry.contrib.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_21
targetCompatibility = JavaVersion.VERSION_21
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 21+
val testJavaVersion: String? by project
if (testJavaVersion != null && Integer.valueOf(testJavaVersion) < 21) {
test {
enabled = false
}
}
}

tasks.withType<Test>().configureEach {
// required when accessing javac internals
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.name == project.name
}
}
}

// Skip OWASP dependencyCheck task on test module
dependencyCheck {
skip = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.gradle.customchecks;

import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol;
import javax.annotation.Nullable;

/**
* Error Prone check that detects usage of deprecated APIs.
*
* <p>This is similar to javac's -Xlint:deprecation but properly honors {@code @SuppressWarnings}
* (including on import statements, which javac doesn't support with --release 8 due to <a
* href="https://bugs.openjdk.org/browse/JDK-8032211">JDK-8032211</a>).
*
* <p>Can be suppressed with {@code @SuppressWarnings("deprecation")}.
*/
@BugPattern(
summary = "Use of deprecated API",
severity = ERROR,
linkType = NONE,
altNames = "deprecation", // so it can be suppressed with @SuppressWarnings("deprecation")
suppressionAnnotations = SuppressWarnings.class)
public class OtelDeprecatedApiUsage extends BugChecker
implements BugChecker.MethodInvocationTreeMatcher,
BugChecker.NewClassTreeMatcher,
BugChecker.MemberSelectTreeMatcher,
BugChecker.MemberReferenceTreeMatcher,
BugChecker.IdentifierTreeMatcher {

private static final long serialVersionUID = 1L;

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}

@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}

@Override
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}

@Override
public Description matchIdentifier(IdentifierTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}

private Description checkDeprecated(Symbol sym, Tree tree, VisitorState state) {
if (sym == null) {
return Description.NO_MATCH;
}

// Don't warn on import statements
if (isInsideImport(state)) {
return Description.NO_MATCH;
}

// Check if the symbol itself is deprecated
if (!isDeprecated(sym, state)) {
return Description.NO_MATCH;
}

// Don't warn if we're inside a deprecated context (class, method, etc.)
if (isInsideDeprecatedCode(state)) {
return Description.NO_MATCH;
}

// Don't warn if the deprecated symbol is in the same top-level class (matches javac behavior)
if (isInSameTopLevelClass(sym, state)) {
return Description.NO_MATCH;
}

return buildDescription(tree).setMessage("Use of deprecated API: " + sym).build();
}

private static boolean isInsideImport(VisitorState state) {
for (Tree tree : state.getPath()) {
if (tree instanceof ImportTree) {
return true;
}
}
return false;
}

private static boolean isDeprecated(Symbol sym, VisitorState state) {
// First try the symbol's isDeprecated() method
if (sym.isDeprecated()) {
return true;
}
// Also check for @Deprecated annotation (some symbols may not have flag set)
return ASTHelpers.hasAnnotation(sym, "java.lang.Deprecated", state);
}

private static boolean isInsideDeprecatedCode(VisitorState state) {
// Check enclosing elements (method, class) for @Deprecated
// Skip the first element which is the current node being checked
boolean first = true;
for (Tree tree : state.getPath()) {
if (first) {
first = false;
continue;
}
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym != null && isDeprecated(sym, state)) {
return true;
}
}
return false;
}

private static boolean isInSameTopLevelClass(Symbol sym, VisitorState state) {
// Get the top-level class containing the deprecated symbol
Symbol.ClassSymbol deprecatedTopLevel = getTopLevelClass(sym);
if (deprecatedTopLevel == null) {
return false;
}

// Get the top-level class containing the current usage by walking the tree path
// Skip the first element (the node being checked) to find the enclosing class
Symbol.ClassSymbol usageTopLevel = null;
boolean first = true;
for (Tree tree : state.getPath()) {
if (first) {
first = false;
continue;
}
Symbol treeSym = ASTHelpers.getSymbol(tree);
if (treeSym instanceof Symbol.ClassSymbol classSymbol) {
usageTopLevel = getTopLevelClass(classSymbol);
if (usageTopLevel != null) {
break;
}
}
}
if (usageTopLevel == null) {
return false;
}

return deprecatedTopLevel.equals(usageTopLevel);
}

@Nullable
private static Symbol.ClassSymbol getTopLevelClass(Symbol sym) {
Symbol current = sym;
while (current != null) {
if (current instanceof Symbol.ClassSymbol classSymbol) {
Symbol owner = classSymbol.owner;
// Top-level class is owned by a package
if (owner instanceof Symbol.PackageSymbol) {
return classSymbol;
}
}
current = current.owner;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.gradle.customchecks.OtelDeprecatedApiUsage
Loading
Loading