From 60c8c3fd5fbcda9e34f9dfecc4cb7b5fbcd21188 Mon Sep 17 00:00:00 2001 From: Pieter Dirk Soels Date: Fri, 30 Sep 2022 18:44:21 +0200 Subject: [PATCH] Pass null for urlLink to Description.Builder --- .../AnnotatedCompositeCodeTransformer.java | 35 ++++++++++--------- .../refaster/runner/RefasterTest.java | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/AnnotatedCompositeCodeTransformer.java b/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/AnnotatedCompositeCodeTransformer.java index 658ab289bbf..ff4a1363185 100644 --- a/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/AnnotatedCompositeCodeTransformer.java +++ b/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/AnnotatedCompositeCodeTransformer.java @@ -67,7 +67,7 @@ private Description augmentDescription( return Description.builder( description.position, shortCheckName, - getLinkPattern(delegate, shortCheckName), + getLinkPattern(delegate, shortCheckName).orElse(null), overrideSeverity(getSeverity(delegate), context), getDescription(delegate)) .addAllFixes(description.fixes) @@ -89,14 +89,16 @@ private String getShortCheckName(String fullCheckName) { return fullCheckName.substring(prefix.length()); } - private String getLinkPattern(CodeTransformer delegate, String checkName) { - String urlPattern = - getAnnotationValue(OnlineDocumentation.class, OnlineDocumentation::value, delegate, ""); - + private Optional getLinkPattern(CodeTransformer delegate, String checkName) { Iterator nameComponents = CLASS_NAME_SPLITTER.splitToStream(checkName).iterator(); - return urlPattern - .replace(TOP_LEVEL_CLASS_URL_PLACEHOLDER, nameComponents.next()) - .replace(NESTED_CLASS_URL_PLACEHOLDER, Iterators.getNext(nameComponents, "")); + return getAnnotationValue(OnlineDocumentation.class, OnlineDocumentation::value, delegate) + .map( + urlPattern -> + urlPattern.replace(TOP_LEVEL_CLASS_URL_PLACEHOLDER, nameComponents.next())) + .map( + urlPattern -> + urlPattern.replace( + NESTED_CLASS_URL_PLACEHOLDER, Iterators.getNext(nameComponents, ""))); } private SeverityLevel getSeverity(CodeTransformer delegate) { @@ -105,23 +107,22 @@ private SeverityLevel getSeverity(CodeTransformer delegate) { * `tech.picnic.errorprone.refaster.runner.Refaster` bug checker. (The associated * `RefasterTest#severityAssignment` test verifies this invariant.) */ - return getAnnotationValue(Severity.class, Severity::value, delegate, SUGGESTION); + return getAnnotationValue(Severity.class, Severity::value, delegate).orElse(SUGGESTION); } private String getDescription(CodeTransformer delegate) { return getAnnotationValue( - tech.picnic.errorprone.refaster.annotation.Description.class, - tech.picnic.errorprone.refaster.annotation.Description::value, - delegate, - "Refactoring opportunity"); + tech.picnic.errorprone.refaster.annotation.Description.class, + tech.picnic.errorprone.refaster.annotation.Description::value, + delegate) + .orElse("Refactoring opportunity"); } - private T getAnnotationValue( - Class annotation, Function extractor, CodeTransformer delegate, T defaultValue) { + private Optional getAnnotationValue( + Class annotation, Function extractor, CodeTransformer delegate) { return getAnnotationValue(delegate, annotation) .or(() -> getAnnotationValue(this, annotation)) - .map(extractor) - .orElse(defaultValue); + .map(extractor); } private static Optional getAnnotationValue( diff --git a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/RefasterTest.java b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/RefasterTest.java index 284fc602469..fa965e12ea7 100644 --- a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/RefasterTest.java +++ b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/RefasterTest.java @@ -31,7 +31,7 @@ final class RefasterTest { .expectErrorMessage( "StringOfSizeZeroTemplate", containsPattern( - "\\[Refaster Rule\\] FooTemplates\\.StringOfSizeZeroTemplate: Refactoring opportunity\\s+.+\\s+null")) + "\\[Refaster Rule\\] FooTemplates\\.StringOfSizeZeroTemplate: Refactoring opportunity\\s+.+\\s+")) .expectErrorMessage( "StringOfSizeOneTemplate", containsPattern(