Repository: RBusarow/ModuleCheck (kotlin)

Issue:
No value present error
I tried the plugin in a project and getting this error. Unfortunately I cannot provide a sample project but I hope the stacktrace will be helpful. Please let me know if you would like to have more details. Thanks. 

```
Caused by: java.util.NoSuchElementException: No value present
        at modulecheck.parsing.java.JavaFile$parsed$2.invoke(JavaFile.kt:77)
        at modulecheck.parsing.java.JavaFile$parsed$2.invoke(JavaFile.kt:47)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at modulecheck.parsing.java.JavaFile.getParsed(JavaFile.kt:47)
        at modulecheck.parsing.java.JavaFile.access$getParsed(JavaFile.kt:34)
        at modulecheck.parsing.java.JavaFile$imports$2.invoke(JavaFile.kt:89)
        at modulecheck.parsing.java.JavaFile$imports$2.invoke(JavaFile.kt:88)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at modulecheck.parsing.java.JavaFile.getImports(JavaFile.kt:88)
        at modulecheck.api.context.Imports$Key.invoke(Imports.kt:43)
        at modulecheck.api.context.Imports$Key.invoke(Imports.kt:34)
        at modulecheck.parsing.ProjectContextImpl.get(ProjectContext.kt:40)
        at modulecheck.api.RealAndroidMcProject.get(RealAndroidMcProject.kt:42)
        at modulecheck.api.context.ImportsKt.getImports(Imports.kt:58)
        at modulecheck.api.context.ImportsKt.importsForSourceSetName(Imports.kt:60)
        at modulecheck.core.internal.UsesKt.usesInConfig(uses.kt:51)
        at modulecheck.core.internal.UsesKt.uses(uses.kt:35)
        at modulecheck.core.rule.InheritedDependencyRule.check(InheritedDependencyRule.kt:37)
        at modulecheck.gradle.task.ModuleCheckAllTask.evaluate(ModuleCheckAllTask.kt:37)
        at modulecheck.core.ModuleCheckRunner.run-IoAF18A(ModuleCheckRunner.kt:56)
        at modulecheck.gradle.task.ModuleCheckTask.run(ModuleCheckTask.kt:80)
```

Pull request description:
fixes #399

Patch:
diff --git a/modulecheck-parsing/java/src/main/kotlin/modulecheck/parsing/java/ParsedFile.kt b/modulecheck-parsing/java/src/main/kotlin/modulecheck/parsing/java/ParsedFile.kt
index 47f13cb958..8c9e01b186 100644
--- a/modulecheck-parsing/java/src/main/kotlin/modulecheck/parsing/java/ParsedFile.kt
+++ b/modulecheck-parsing/java/src/main/kotlin/modulecheck/parsing/java/ParsedFile.kt
@@ -37,8 +37,8 @@ internal data class ParsedFile(
   companion object {
     fun fromCompilationUnitLazy(compilationUnit: CompilationUnit): Lazy<ParsedFile> {
       return lazy {
-        val packageFqName = compilationUnit.packageDeclaration.get().nameAsString
-        val imports = compilationUnit.imports
+        val packageFqName = compilationUnit.packageDeclaration.getOrNull()?.nameAsString ?: ""
+        val imports = compilationUnit.imports.orEmpty()
 
         val classOrInterfaceTypes = mutableSetOf<ClassOrInterfaceType>()
         val typeDeclarations = mutableListOf<TypeDeclaration<*>>()


Test patch:
diff --git a/modulecheck-parsing/java/src/test/kotlin/modulecheck/parsing/java/JavaFileTest.kt b/modulecheck-parsing/java/src/test/kotlin/modulecheck/parsing/java/JavaFileTest.kt
index 235500f7df..84dba9e4cb 100644
--- a/modulecheck-parsing/java/src/test/kotlin/modulecheck/parsing/java/JavaFileTest.kt
+++ b/modulecheck-parsing/java/src/test/kotlin/modulecheck/parsing/java/JavaFileTest.kt
@@ -164,6 +164,44 @@ internal class JavaFileTest :
         declarations = setOf("com.test.MyRecord")
       )
     }
+
+    // reproducer for https://github.com/RBusarow/ModuleCheck/issues/399
+    @Test
+    fun `file without package should put declarations at the root`() {
+
+      val file = file(
+        """
+
+    import com.lib1.Lib1Class;
+
+    public static record MyRecord(Lib1Class lib1Class) {}
+      """,
+        javaVersion = JavaVersion.VERSION_16
+      )
+
+      file shouldBe javaFile(
+        packageFqName = "",
+        imports = setOf("com.lib1.Lib1Class"),
+        declarations = setOf("MyRecord")
+      )
+    }
+
+    @Test
+    fun `file without imports should still parse`() {
+
+      val file = file(
+        """
+    package com.test;
+
+    public class MyClass {}
+      """,
+        javaVersion = JavaVersion.VERSION_16
+      )
+
+      file shouldBe javaFile(
+        declarations = setOf("com.test.MyClass")
+      )
+    }
   }
 
   @Nested