Add @JavaSuppressLint
This is a kind of passthrough annotations for the Java backend. But
adding it as an AIDL annotation is much simpler since it also affects
--checkapi. (JavaSuppressLint can be ignored when checking backward
compatibility.)
Bug: 193460475
Test: aidl_unittests
Change-Id: I640e2268b7bdfeb7c630e3620f5eea20ef354679
diff --git a/aidl_api/aidl-test-versioned-interface/current/android/aidl/versioned/tests/Foo.aidl b/aidl_api/aidl-test-versioned-interface/current/android/aidl/versioned/tests/Foo.aidl
index dad05e6..68364d8 100644
--- a/aidl_api/aidl-test-versioned-interface/current/android/aidl/versioned/tests/Foo.aidl
+++ b/aidl_api/aidl-test-versioned-interface/current/android/aidl/versioned/tests/Foo.aidl
@@ -17,6 +17,7 @@
// later when a module using the interface is updated, e.g., Mainline modules.
package android.aidl.versioned.tests;
+@JavaSuppressLint(value={"NewApi"})
parcelable Foo {
- int intDefault42 = 42;
+ int intDefault42 = 42;
}
diff --git a/aidl_checkapi.cpp b/aidl_checkapi.cpp
index 1b2bb85..effafcf 100644
--- a/aidl_checkapi.cpp
+++ b/aidl_checkapi.cpp
@@ -80,6 +80,7 @@
AidlAnnotation::Type::JAVA_DERIVE,
AidlAnnotation::Type::JAVA_DEFAULT,
AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
+ AidlAnnotation::Type::JAVA_SUPPRESS_LINT,
// @Backing for a enum type is checked by the enum checker
AidlAnnotation::Type::BACKING,
// @RustDerive doesn't affect read/write
diff --git a/aidl_language.cpp b/aidl_language.cpp
index eff9c92..82995dc 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -163,6 +163,10 @@
CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION |
CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
{}},
+ {AidlAnnotation::Type::JAVA_SUPPRESS_LINT,
+ "JavaSuppressLint",
+ CONTEXT_ALL,
+ {{"value", kStringArrayType, /* required= */ true}}},
{AidlAnnotation::Type::FIXED_SIZE,
"FixedSize",
CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
diff --git a/aidl_language.h b/aidl_language.h
index bf9322b..d19e425 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -235,6 +235,7 @@
JAVA_DERIVE,
JAVA_DEFAULT,
JAVA_ONLY_IMMUTABLE,
+ JAVA_SUPPRESS_LINT,
FIXED_SIZE,
DESCRIPTOR,
RUST_DERIVE,
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 6c25f58..2bf1a6d 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -3019,6 +3019,21 @@
EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
}
+TEST_F(AidlTestCompatibleChanges, NewJavaSuppressLint) {
+ io_delegate_.SetFileContents("old/p/IFoo.aidl",
+ "package p;"
+ "interface IFoo {"
+ " void foo(int a);"
+ "}");
+ io_delegate_.SetFileContents("new/p/IFoo.aidl",
+ "package p;"
+ "@JavaSuppressLint({\"NewApi\"})"
+ "interface IFoo {"
+ " void foo(int a);"
+ "}");
+ EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
+}
+
class AidlTestIncompatibleChanges : public AidlTest {
protected:
Options options_ = Options::From("aidl --checkapi old new");
@@ -4056,6 +4071,21 @@
HasSubstr("The interface IFoo enforces permissions using annotations"));
}
+TEST_F(AidlTest, JavaSuppressLint) {
+ io_delegate_.SetFileContents("a/IFoo.aidl", R"(package a;
+ @JavaSuppressLint({"NewApi"})
+ interface IFoo {
+ })");
+
+ Options options = Options::From("aidl --lang=java -o out a/IFoo.aidl");
+ CaptureStderr();
+ EXPECT_TRUE(compile_aidl(options, io_delegate_));
+ EXPECT_EQ(GetCapturedStderr(), "");
+ string code;
+ EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/IFoo.java", &code));
+ EXPECT_THAT(code, HasSubstr("@android.annotation.SuppressLint(value = {\"NewApi\"})"));
+}
+
class AidlOutputPathTest : public AidlTest {
protected:
void SetUp() override {
diff --git a/generate_java.cpp b/generate_java.cpp
index 56ad658..f6c7e29 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -919,6 +919,13 @@
if (annotation->GetType() == AidlAnnotation::Type::JAVA_PASSTHROUGH) {
result.emplace_back(annotation->ParamValue<std::string>("annotation").value());
}
+ if (annotation->GetType() == AidlAnnotation::Type::JAVA_SUPPRESS_LINT) {
+ std::vector<std::string> values;
+ for (const auto& [name, value] : annotation->AnnotationParams(ConstantValueDecorator)) {
+ values.emplace_back(name + " = " + value);
+ }
+ result.emplace_back("@android.annotation.SuppressLint(" + Join(values, ", ") + ")");
+ }
}
if (auto enforce_expr = a.EnforceExpression(); enforce_expr) {
diff --git a/tests/versioned/android/aidl/versioned/tests/Foo.aidl b/tests/versioned/android/aidl/versioned/tests/Foo.aidl
index acf7620..4b8c3e9 100644
--- a/tests/versioned/android/aidl/versioned/tests/Foo.aidl
+++ b/tests/versioned/android/aidl/versioned/tests/Foo.aidl
@@ -1,5 +1,6 @@
package android.aidl.versioned.tests;
+@JavaSuppressLint({"NewApi"})
parcelable Foo {
// V1 is empty
// V2