|
| 1 | +/* |
| 2 | + * Copyright 2026 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.matchers; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 20 | + |
| 21 | +import com.google.errorprone.VisitorState; |
| 22 | +import com.google.errorprone.scanner.Scanner; |
| 23 | +import com.sun.source.tree.ExpressionTree; |
| 24 | +import com.sun.source.tree.MemberSelectTree; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.junit.runners.JUnit4; |
| 28 | + |
| 29 | +@RunWith(JUnit4.class) |
| 30 | +public class FieldMatchersTest extends CompilerBasedAbstractTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void compilesWithPrimitiveClassLiteral() { |
| 34 | + writeFile( |
| 35 | + "A.java", |
| 36 | + """ |
| 37 | + class A { |
| 38 | + Class<?> clazz = long.class; |
| 39 | + } |
| 40 | + """); |
| 41 | + |
| 42 | + assertCompiles( |
| 43 | + memberSelectMatches( |
| 44 | + /* shouldMatch= */ false, FieldMatchers.anyFieldInClass("A"))); |
| 45 | + assertCompiles( |
| 46 | + memberSelectMatches( |
| 47 | + /* shouldMatch= */ false, FieldMatchers.instanceField("A", "irrelevant"))); |
| 48 | + assertCompiles( |
| 49 | + memberSelectMatches( |
| 50 | + /* shouldMatch= */ false, FieldMatchers.staticField("A", "irrelevant"))); |
| 51 | + } |
| 52 | + |
| 53 | + private static Scanner memberSelectMatches( |
| 54 | + boolean shouldMatch, Matcher<ExpressionTree> matcher) { |
| 55 | + return new Scanner() { |
| 56 | + @Override |
| 57 | + public Void visitMemberSelect(MemberSelectTree node, VisitorState visitorState) { |
| 58 | + assertWithMessage(node.toString()) |
| 59 | + .that(matcher.matches(node, visitorState)) |
| 60 | + .isEqualTo(shouldMatch); |
| 61 | + return super.visitMemberSelect(node, visitorState); |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | +} |
0 commit comments