-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6876 S2479 Reduce noise by suppressing on strings that are most likely generated #6061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91ed7bf
S2479 Reduce noise by ignoring strings that are most likely generated
lijun-chen-sonarsource 9dcc751
Address review comment: use JParserTestUtils for helper tests
lijun-chen-sonarsource 684f8dd
Address review comment: remove external links
lijun-chen-sonarsource aa0e9c3
Address review comment: add javadoc
lijun-chen-sonarsource b70e237
Address review comment: simplify recognition logic
lijun-chen-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+106 Bytes
(100%)
java-checks-test-sources/default/src/main/java/checks/ControlCharacterInLiteralCheck.java
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...-checks/src/main/java/org/sonar/java/checks/helpers/GeneratedStringLiteralRecognizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.helpers; | ||
|
|
||
| import org.sonar.java.model.ExpressionUtils; | ||
| import org.sonar.plugins.java.api.semantic.Type; | ||
| import org.sonar.plugins.java.api.tree.AnnotationTree; | ||
| import org.sonar.plugins.java.api.tree.ExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.LiteralTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| /** | ||
| * Recognizes strings that are most likely not hand-written and maintained by developers. | ||
| * | ||
| * Currently we ignore: | ||
| * | ||
| * <ul> | ||
| * <li>{@code d1} strings in {@code kotlin.Metadata} annotations</li> | ||
| * <li>All strings in {@code kotlin.jvm.internal.SourceDebugExtension} annotations</li> | ||
| * <li>All strings in {@code kotlin.coroutines.jvm.internal.DebugMetadata} annotations</li> | ||
| * </ul> | ||
| */ | ||
| public final class GeneratedStringLiteralRecognizer { | ||
|
|
||
| private static final String KOTLIN_METADATA = "kotlin.Metadata"; | ||
| private static final String KOTLIN_SOURCE_DEBUG_EXTENSION = "kotlin.jvm.internal.SourceDebugExtension"; | ||
| private static final String KOTLIN_DEBUG_METADATA = "kotlin.coroutines.jvm.internal.DebugMetadata"; | ||
|
|
||
| private GeneratedStringLiteralRecognizer() { | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a string or text block literal is (most likely) generated by a known tool. | ||
| * and not hand-written by developers. Useful for rule suppression for rules that raise on such literals. | ||
| * | ||
| * @param literal the literal to check | ||
| * @return {@code true} when the literal is recognized as generated | ||
| */ | ||
| public static boolean isGenerated(LiteralTree literal) { | ||
| if (!literal.is(Tree.Kind.STRING_LITERAL, Tree.Kind.TEXT_BLOCK)) { | ||
| return false; | ||
| } | ||
|
|
||
| Tree annotationArgument = literal; | ||
| while (annotationArgument.parent() != null && !annotationArgument.parent().is(Tree.Kind.ARGUMENTS)) { | ||
| annotationArgument = annotationArgument.parent(); | ||
| } | ||
|
|
||
| Tree arguments = annotationArgument.parent(); | ||
| if (arguments == null || !(arguments.parent() instanceof AnnotationTree annotation)) { | ||
| return false; | ||
| } | ||
|
|
||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| return isAnnotationType(annotation, KOTLIN_SOURCE_DEBUG_EXTENSION) | ||
| || isAnnotationType(annotation, KOTLIN_DEBUG_METADATA) | ||
| || (annotationArgument instanceof ExpressionTree argument | ||
| && "d1".equals(ExpressionUtils.annotationAttributeName(argument)) | ||
| && isAnnotationType(annotation, KOTLIN_METADATA)); | ||
| } | ||
|
|
||
| private static boolean isAnnotationType(AnnotationTree annotation, String fullyQualifiedName) { | ||
| Type annotationType = annotation.symbolType(); | ||
| String unqualifiedName = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf('.') + 1); | ||
| return annotationType.is(fullyQualifiedName) | ||
| || (annotationType.isUnknown() && annotationType.name().equals(unqualifiedName)); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| } | ||
135 changes: 135 additions & 0 deletions
135
...cks/src/test/java/org/sonar/java/checks/helpers/GeneratedStringLiteralRecognizerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.helpers; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.plugins.java.api.tree.BaseTreeVisitor; | ||
| import org.sonar.plugins.java.api.tree.LiteralTree; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class GeneratedStringLiteralRecognizerTest { | ||
|
lijun-chen-sonarsource marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void recognizes_generated_literals() { | ||
| assertGenerated(""" | ||
| package kotlin; | ||
| @interface Metadata { | ||
| String[] d1(); | ||
| } | ||
| @Metadata(d1 = {"generated"}) | ||
| class A {} | ||
| """); | ||
|
|
||
| assertGenerated(""" | ||
| package kotlin.jvm.internal; | ||
| @interface SourceDebugExtension { | ||
| String[] value(); | ||
| } | ||
| @SourceDebugExtension({ | ||
| "generated", | ||
| \""" | ||
| generated text block | ||
| \"""}) | ||
| class A {} | ||
| """); | ||
|
|
||
| assertGenerated(""" | ||
| package kotlin.coroutines.jvm.internal; | ||
| @interface DebugMetadata { | ||
| String c(); | ||
| String f(); | ||
| String m(); | ||
| String[] n(); | ||
| } | ||
| @DebugMetadata(c = "c", f = "f", m = "m", n = {"n"}) | ||
| class A {} | ||
| """); | ||
| } | ||
|
|
||
| @Test | ||
| void recognizes_generated_literals_without_dependencies() { | ||
| assertGenerated(""" | ||
| @Metadata(d1 = {"generated"}) | ||
| class A {} | ||
| """); | ||
|
|
||
| assertGenerated(""" | ||
| @kotlin.jvm.internal.SourceDebugExtension({"generated"}) | ||
| class A {} | ||
| """); | ||
| } | ||
|
|
||
| @Test | ||
| void does_not_recognize_other_literals() { | ||
| assertNotGenerated(""" | ||
| package kotlin; | ||
| @interface Metadata { | ||
| String[] d2(); | ||
| } | ||
| @Metadata(d2 = {"not generated"}) | ||
| class A {} | ||
| """); | ||
|
|
||
| assertNotGenerated(""" | ||
| @interface NotKotlinMetadata { | ||
| String[] d1(); | ||
| } | ||
| @NotKotlinMetadata(d1 = {"not generated"}) | ||
| class A { | ||
| String value = "not generated"; | ||
| char character = 'a'; | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| @Test | ||
| void does_not_recognize_shadowed_kotlin_metadata() { | ||
| assertNotGenerated(""" | ||
| import kotlin.Metadata; | ||
| class A { | ||
| @interface Metadata { | ||
| String[] d1(); | ||
| } | ||
| @Metadata(d1 = {"not generated"}) | ||
| class B {} | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| private static void assertGenerated(String source) { | ||
| assertThat(literals(source)).allMatch(GeneratedStringLiteralRecognizer::isGenerated); | ||
| } | ||
|
|
||
| private static void assertNotGenerated(String source) { | ||
| assertThat(literals(source)).noneMatch(GeneratedStringLiteralRecognizer::isGenerated); | ||
| } | ||
|
|
||
| private static List<LiteralTree> literals(String source) { | ||
| List<LiteralTree> literals = new ArrayList<>(); | ||
| JParserTestUtils.parse(source).accept(new BaseTreeVisitor() { | ||
| @Override | ||
| public void visitLiteral(LiteralTree tree) { | ||
| literals.add(tree); | ||
| } | ||
| }); | ||
| assertThat(literals).isNotEmpty(); | ||
| return literals; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.