Merge "Propagate class @Immutable.Ignore to references"

This commit is contained in:
TreeHugger Robot
2022-10-05 18:38:12 +00:00
committed by Android (Google) Code Review
2 changed files with 45 additions and 0 deletions

View File

@@ -323,8 +323,15 @@ class ImmutabilityProcessor : AbstractProcessor() {
parentPolicyExceptions: Set<Immutable.Policy.Exception>,
nonInterfaceClassFailure: () -> String = { MessageUtils.nonInterfaceReturnFailure() },
): Boolean {
// Skip if the symbol being considered is itself ignored
if (isIgnored(symbol)) return false
// Skip if the type being checked, like for a typeArg or return type, is ignored
if (isIgnored(type)) return false
// Skip if that typeArg is itself ignored when inspected at the class header level
if (isIgnored(type.asElement())) return false
if (type.isPrimitive) return false
if (type.isPrimitiveOrVoid) {
printError(parentChain, symbol, MessageUtils.voidReturnFailure())
@@ -355,6 +362,8 @@ class ImmutabilityProcessor : AbstractProcessor() {
var anyError = false
type.typeArguments.forEachIndexed { index, typeArg ->
if (isIgnored(typeArg.asElement())) return@forEachIndexed
val argError =
visitType(parentChain, seenTypesByPolicy, symbol, typeArg, newPolicyExceptions) {
MessageUtils.nonInterfaceReturnFailure(

View File

@@ -287,6 +287,42 @@ class ImmutabilityProcessorTest {
)
}
@Test
fun ignoredClass() = test(
JavaFileObjects.forSourceString(
"$PACKAGE_PREFIX.$DATA_CLASS_NAME",
/* language=JAVA */ """
package $PACKAGE_PREFIX;
import java.util.List;
import java.util.Map;
@Immutable
public interface $DATA_CLASS_NAME {
IgnoredClass getInnerClassOne();
NotIgnoredClass getInnerClassTwo();
Map<String, IgnoredClass> getInnerClassThree();
Map<String, NotIgnoredClass> getInnerClassFour();
@Immutable.Ignore
final class IgnoredClass {
public String innerField;
}
final class NotIgnoredClass {
public String innerField;
}
}
""".trimIndent()
), errors = listOf(
nonInterfaceReturnFailure(line = 9),
nonInterfaceReturnFailure(line = 11, prefix = "Value NotIgnoredClass"),
classNotImmutableFailure(line = 18, className = "NotIgnoredClass"),
nonInterfaceClassFailure(line = 18),
memberNotMethodFailure(line = 19),
)
)
private fun test(
source: JavaFileObject,
errors: List<CompilationError>,