From 9f6240a6b7514b3a827b47527427631390381b58 Mon Sep 17 00:00:00 2001 From: wangwei11 Date: Fri, 24 Feb 2023 20:19:21 +0800 Subject: [PATCH] [issues-1035] fix kotlion incrementalFilter createResource NPE : https://github.com/JetBrains/kotlin/blob/master/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/incrementalProcessors.kt --- .../compiler/processor/RouteProcessor.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/RouteProcessor.java b/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/RouteProcessor.java index 9039f04c..e1af4e57 100644 --- a/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/RouteProcessor.java +++ b/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/RouteProcessor.java @@ -71,8 +71,8 @@ @AutoService(Processor.class) @SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE, ANNOTATION_TYPE_AUTOWIRED}) public class RouteProcessor extends BaseProcessor { - private Map> groupMap = new HashMap<>(); // ModuleName and routeMeta. - private Map rootMap = new TreeMap<>(); // Map of root metas, used for generate class file in order. + private final Map> groupMap = new HashMap<>(); // ModuleName and routeMeta. + private final Map rootMap = new TreeMap<>(); // Map of root metas, used for generate class file in order. private TypeMirror iProvider = null; private Writer docWriter; // Writer used for write doc @@ -81,23 +81,27 @@ public class RouteProcessor extends BaseProcessor { public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); - if (generateDoc) { - try { - docWriter = mFiler.createResource( - StandardLocation.SOURCE_OUTPUT, - PACKAGE_OF_GENERATE_DOCS, - "arouter-map-of-" + moduleName + ".json" - ).openWriter(); - } catch (IOException e) { - logger.error("Create doc writer failed, because " + e.getMessage()); - } - } - iProvider = elementUtils.getTypeElement(Consts.IPROVIDER).asType(); logger.info(">>> RouteProcessor init. <<<"); } + private synchronized Writer getOrCreateDocWriter() { + if (docWriter != null) { + return docWriter; + } + try { + docWriter = mFiler.createResource( + StandardLocation.SOURCE_OUTPUT, + PACKAGE_OF_GENERATE_DOCS, + "arouter-map-of-" + moduleName + ".json" + ).openWriter(); + } catch (IOException e) { + logger.error("Create doc writer failed, because " + e.getMessage()); + } + return docWriter; + } + /** * {@inheritDoc} * @@ -336,9 +340,12 @@ private void parseRoutes(Set routeElements) throws IOExceptio // Output route doc if (generateDoc) { - docWriter.append(JSON.toJSONString(docSource, SerializerFeature.PrettyFormat)); - docWriter.flush(); - docWriter.close(); + Writer docWriter = getOrCreateDocWriter(); + if (docWriter != null) { + docWriter.append(JSON.toJSONString(docSource, SerializerFeature.PrettyFormat)); + docWriter.flush(); + docWriter.close(); + } } // Write provider into disk