From aaa00b0f6f6a07984bb2a2e5bb72b8fde3a7e0f6 Mon Sep 17 00:00:00 2001
From: Ivan Gavrilovic <gavra@google.com>
Date: Mon, 23 Sep 2019 16:47:21 +0100
Subject: [PATCH] KT-33889: Do not use private non-API methods from
 JavacProcessingEnvironment

Avoid using JavacProcessingEnvironment.validImportStringToPattern method
because it has been removed in JDK9. This commit changes how we compute
pattern to match the class names, and it create instances of Pattern
manually by following spec for Processor.getSupportedAnnotationTypes().
Support for module prefix is not added yet.
---
 .../kapt3/base/incremental/classStructureCache.kt | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

--- a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/classStructureCache.kt
+++ b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/classStructureCache.kt
@@ -6,12 +6,12 @@
 @file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE")
 package org.jetbrains.kotlin.kapt3.base.incremental
 
-import com.sun.tools.javac.processing.JavacProcessingEnvironment
 import java.io.File
 import java.io.ObjectInputStream
 import java.io.ObjectOutputStream
 import java.io.Serializable
 import java.net.URI
+import java.util.regex.Pattern
 
 class JavaClassCache() : Serializable {
     private var sourceCache = mutableMapOf<URI, SourceFileStructure>()
@@ -131,7 +131,18 @@
      * annotation processor. This search is not transitive.
      */
     fun invalidateEntriesAnnotatedWith(annotations: Set<String>): Set<File> {
-        val patterns = annotations.map { JavacProcessingEnvironment.validImportStringToPattern(it) }
+        val patterns: List<Pattern> = if ("*" in annotations) {
+            // optimize this case - create only one pattern
+            listOf(Pattern.compile(".*"))
+        } else {
+            annotations.map {
+                Pattern.compile(
+                    // These are already valid import statements, otherwise run fails when loading the annotation processor.
+                    // Handles structure; TypeName [.*] e.g. org.jetbrains.annotations.NotNull and org.jetbrains.annotations.*
+                    it.replace(".", "\\.").replace("*", ".+")
+                )
+            }
+        }
         val matchesAnyPattern = { name: String -> patterns.any { it.matcher(name).matches() } }
 
         val toReprocess = mutableSetOf<URI>()
