Skip to content

Commit 1d04094

Browse files
msridharError Prone Team
authored andcommitted
A couple of fixes in MoreAnnotations
I discovered these issues while using this code in NullAway; see uber/NullAway#1055. (We support older Error Prone versions so can't directly call this API and had to adapt the code instead.) The previous code didn't handle explicit annotations on lambda parameters (e.g., `(@nullable Object x) -> { ... }`) and annotations on enum constants. Not sure the best way to add tests for this but happy to add them if given a suggestion. Actually the enum constant case was weird; I was unable to repro with bytecodes output by `javac` and I only observed the case with an `ijar` from Bazel. FYI @cpovirk @cushon Fixes #4620 COPYBARA_INTEGRATE_REVIEW=#4620 from msridhar:more-annotations-tweaks fb3690b PiperOrigin-RevId: 686916301
1 parent 6203a0e commit 1d04094

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

check_api/src/main/java/com/google/errorprone/util/MoreAnnotations.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.sun.tools.javac.code.Type;
3131
import com.sun.tools.javac.code.TypeAnnotationPosition;
3232
import com.sun.tools.javac.code.TypeTag;
33+
import com.sun.tools.javac.tree.JCTree;
3334
import java.util.LinkedHashMap;
3435
import java.util.List;
3536
import java.util.Map;
@@ -130,15 +131,25 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi
130131
case LOCAL_VARIABLE:
131132
return position.type == TargetType.LOCAL_VARIABLE;
132133
case FIELD:
134+
// treated like a field
135+
case ENUM_CONSTANT:
133136
return position.type == TargetType.FIELD;
134137
case CONSTRUCTOR:
135138
case METHOD:
136139
return position.type == TargetType.METHOD_RETURN;
137140
case PARAMETER:
138141
switch (position.type) {
139142
case METHOD_FORMAL_PARAMETER:
140-
return ((MethodSymbol) sym.owner).getParameters().indexOf(sym)
141-
== position.parameter_index;
143+
int parameterIndex = position.parameter_index;
144+
if (position.onLambda != null) {
145+
com.sun.tools.javac.util.List<JCTree.JCVariableDecl> lambdaParams =
146+
position.onLambda.params;
147+
return parameterIndex < lambdaParams.size()
148+
&& lambdaParams.get(parameterIndex).sym.equals(sym);
149+
} else {
150+
return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym)
151+
== parameterIndex;
152+
}
142153
default:
143154
return false;
144155
}

check_api/src/test/java/com/google/errorprone/util/MoreAnnotationsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,33 @@ class InnerStatic {}
223223
""")
224224
.doTest();
225225
}
226+
227+
@Test
228+
public void explicitlyAnnotatedLambdaTest() {
229+
CompilationTestHelper.newInstance(GetTopLevelTypeAttributesTester.class, getClass())
230+
.addSourceLines(
231+
"Annos.java",
232+
"""
233+
import static java.lang.annotation.ElementType.TYPE_USE;
234+
import java.lang.annotation.Target;
235+
236+
@Target(TYPE_USE)
237+
@interface A {}
238+
""")
239+
.addSourceLines(
240+
"Test.java",
241+
"""
242+
import java.util.function.Consumer;
243+
244+
class Test {
245+
Consumer<@A String> c;
246+
247+
void test() {
248+
// BUG: Diagnostic contains: A
249+
c = (@A String s) -> {};
250+
}
251+
}
252+
""")
253+
.doTest();
254+
}
226255
}

0 commit comments

Comments
 (0)