diff --git a/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java b/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java index 90bbc7b15..f3dcf2780 100644 --- a/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java +++ b/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java @@ -1,6 +1,10 @@ package edu.hm.hafner.analysis; import java.io.Serializable; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Optional; import java.util.UUID; @@ -142,6 +146,9 @@ TreeString internFileName(@CheckForNull final String unsafeFileName) { return UNDEFINED_TREE_STRING; } else { + if (directory != null && isAbsolutePath(normalizeFileName(unsafeFileName))) { + return fileNameBuilder.intern(normalizeFileName(unsafeFileName)); + } return fileNameBuilder.intern(normalizeFileName( new PathUtil().createAbsolutePath(directory, unsafeFileName))); } @@ -578,6 +585,20 @@ private void clean() { additionalProperties = null; } + private static boolean isAbsolutePath(final String stringPath) { + try { + URI uri = new URI(stringPath); + if (uri.isAbsolute()) { + return true; + } + } + catch (URISyntaxException e) { + // Catch and ignore as system paths are not URI and we need to check them separately. + } + Path path = Paths.get(stringPath); + return path.isAbsolute(); + } + private static String normalizeFileName(@CheckForNull final String platformFileName) { return defaultString(StringUtils.replace( StringUtils.strip(platformFileName), "\\", "/")); diff --git a/src/test/java/edu/hm/hafner/analysis/parser/JavacParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/JavacParserTest.java index 23273b060..12505b244 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/JavacParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/JavacParserTest.java @@ -342,5 +342,41 @@ void kotlin18WarningStyle() { .hasFileName("file:///project/src/main/java/com/app/ui/model/Activity.kt") .hasMessage("'PackageStats' is deprecated. Deprecated in Java"); } + + /** + * Parses a warning log written by Gradle containing 3 Kotlin warnings and 1 error. + * Having a cmake directory switch log in between. Following duplicated Kotlin errors should still be treated as duplicates. + */ + @Test + void kotlinAndCmakeDirectoryOuptut() { + Report warnings = parse("kotlin-cmake.txt"); + + assertThat(warnings).hasSize(5); + + assertThat(warnings.get(0)).hasSeverity(Severity.WARNING_NORMAL) + .hasLineStart(214) + .hasColumnStart(35) + .hasFileName("/project/app/src/main/java/ui/Activity.kt"); + assertThat(warnings.get(1)).hasSeverity(Severity.WARNING_NORMAL) + .hasLineStart(424) + .hasColumnStart(29) + .hasFileName("/project/app/src/main/java/ui/Activity.kt"); + assertThat(warnings.get(2)).hasSeverity(Severity.WARNING_NORMAL) + .hasLineStart(425) + .hasColumnStart(29) + .hasFileName("/project/app/src/main/java/ui/Activity.kt") + .hasCategory("Deprecation") + .hasMessage("deprecated: Serializable! to kotlin.collections.HashMap /* = java.util.HashMap */"); + assertThat(warnings.get(3)).hasSeverity(Severity.WARNING_NORMAL) + .hasLineStart(200) + .hasColumnStart(2) + .hasFileName("C:/project/app/src/main/java/ui/Activity.kt"); + assertThat(warnings.get(4)).hasSeverity(Severity.WARNING_NORMAL) + .hasLineStart(8) + .hasColumnStart(27) + .hasCategory("Deprecation") + .hasFileName("file:///project/src/main/java/com/app/ui/model/Activity.kt") + .hasMessage("'PackageStats' is deprecated. Deprecated in Java"); + } } diff --git a/src/test/resources/edu/hm/hafner/analysis/parser/kotlin-cmake.txt b/src/test/resources/edu/hm/hafner/analysis/parser/kotlin-cmake.txt new file mode 100644 index 000000000..96d684a31 --- /dev/null +++ b/src/test/resources/edu/hm/hafner/analysis/parser/kotlin-cmake.txt @@ -0,0 +1,14 @@ +> Configure project :app +Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead. +registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection) +app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.0.1'. +w: /project/app/src/main/java/ui/Activity.kt: (214, 35): Unchecked cast: Serializable! to kotlin.collections.HashMap /* = java.util.HashMap */ +w: /project/app/src/main/java/ui/Activity.kt:424:29 Unchecked cast: Serializable! to kotlin.collections.HashMap /* = java.util.HashMap */ +e: /project/app/src/main/java/ui/Activity.kt:425:29 deprecated: Serializable! to kotlin.collections.HashMap /* = java.util.HashMap */ +w: C:\project\app\src\main\java\ui\Activity.kt:200:2 'PackageStats' is deprecated. Deprecated in Java +[2023-12-20T15:50:19.997Z] w: file:///project/src/main/java/com/app/ui/model/Activity.kt:8:27 'PackageStats' is deprecated. Deprecated in Java +[2023-12-20T15:50:18.292Z] C/C++: -- Build files have been written to: /project/.cxx/Debug/365u2g4u/arm64-v8a +C/C++: /project/src/cpp/MyClass.cpp:35:15: warning: unused parameter 'parameter1' [-Wunused-parameter] +[2023-12-20T15:50:20.997Z] w: file:///project/src/main/java/com/app/ui/model/Activity.kt:8:27 'PackageStats' is deprecated. Deprecated in Java +w: /project/app/src/main/java/ui/Activity.kt:424:29 Unchecked cast: Serializable! to kotlin.collections.HashMap /* = java.util.HashMap */ +w: C:\project\app\src\main\java\ui\Activity.kt:200:2 'PackageStats' is deprecated. Deprecated in Java \ No newline at end of file