dbalek commented on code in PR #6123: URL: https://github.com/apache/netbeans/pull/6123#discussion_r1243400853
########## enterprise/micronaut/src/org/netbeans/modules/micronaut/symbol/MicronautSymbolFinder.java: ########## @@ -0,0 +1,336 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.micronaut.symbol; + +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.MethodTree; +import com.sun.source.util.TreePathScanner; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.stream.Collectors; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Name; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; +import javax.lang.model.util.Elements; +import javax.lang.model.util.Types; +import org.netbeans.api.editor.mimelookup.MimeRegistration; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.source.CompilationController; +import org.netbeans.api.java.source.JavaSource; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.parsing.api.Snapshot; +import org.netbeans.modules.parsing.spi.Parser; +import org.netbeans.modules.parsing.spi.indexing.Context; +import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexer; +import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexerFactory; +import org.netbeans.modules.parsing.spi.indexing.Indexable; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Exceptions; +import org.openide.util.Pair; + +/** + * + * @author Dusan Balek + */ +public final class MicronautSymbolFinder extends EmbeddingIndexer { + + public static final String NAME = "mn"; // NOI18N + public static final int VERSION = 1; + public static final MicronautSymbolFinder INSTANCE = new MicronautSymbolFinder(); + public static final String[] META_ANNOTATIONS = new String[] { + "io.micronaut.http.annotation.HttpMethodMapping", + "io.micronaut.context.annotation.Bean", + "jakarta.inject.Qualifier", + "jakarta.inject.Scope" + }; + + private final Map<Project, Boolean> map = new WeakHashMap<>(); + + @Override + protected void index(Indexable indexable, Parser.Result parserResult, Context context) { + CompilationController cc = CompilationController.get(parserResult); + if (initialize(cc)) { + try { + cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + List<SymbolLocation> symbols = scan(cc); + if (!symbols.isEmpty()) { + store(context.getIndexFolder(), indexable.getURL(), indexable.getRelativePath(), symbols); + } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + + private synchronized boolean initialize(CompilationController cc) { + Project p = FileOwnerQuery.getOwner(cc.getFileObject()); + Boolean ret = map.get(p); + if (ret == null) { + ClassPath cp = ClassPath.getClassPath(p.getProjectDirectory(), ClassPath.COMPILE); + ret = cp.findResource("io/micronaut/http/annotation/HttpMethodMapping.class") != null; + map.put(p, ret); + } + return ret; + } + + private List<SymbolLocation> scan(CompilationController cc) { + final List<SymbolLocation> ret = new ArrayList<>(); + TreePathScanner<Void, String> scanner = new TreePathScanner<Void, String>() { + @Override + public Void visitClass(ClassTree node, String path) { + Element cls = cc.getTrees().getElement(this.getCurrentPath()); + if (cls != null) { + Pair<AnnotationMirror, AnnotationMirror> metaAnnotated = isMetaAnnotated(cls); + if (metaAnnotated != null) { + Element annEl = metaAnnotated.first().getAnnotationType().asElement(); + if ("io.micronaut.http.annotation.Controller".contentEquals(((TypeElement) annEl).getQualifiedName())) { + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : metaAnnotated.first().getElementValues().entrySet()) { + if ("value".contentEquals(entry.getKey().getSimpleName())) { + path = (String) entry.getValue().getValue(); + } + } + } + String name = "@+ '" + getBeanName(node.getSimpleName().toString()) + "' (@" + annEl.getSimpleName() + + (metaAnnotated.second() != null ? " <: @" + metaAnnotated.second().getAnnotationType().asElement().getSimpleName() : "") + + ") " + node.getSimpleName(); + int[] span = cc.getTreeUtilities().findNameSpan(node); + ret.add(new SymbolLocation(name, span[0], span[1])); + } + } + return super.visitClass(node, path); + } + + @Override + public Void visitMethod(MethodTree node, String path) { + MthIterator it = new MthIterator(cc.getTrees().getElement(this.getCurrentPath()), cc.getElements(), cc.getTypes()); + while (it.hasNext()) { + for (AnnotationMirror ann : it.next().getAnnotationMirrors()) { + String method = getEndpointMethod((TypeElement) ann.getAnnotationType().asElement()); + if (method != null) { + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : ann.getElementValues().entrySet()) { + if ("value".contentEquals(entry.getKey().getSimpleName()) || "uri".contentEquals(entry.getKey().getSimpleName())) { + String name = '@' + (path != null ? path : "") + entry.getValue().getValue() + " -- " + method; + int[] span = cc.getTreeUtilities().findNameSpan(node); + ret.add(new SymbolLocation(name, span[0], span[1])); + return null; + } + } + } + } + } + return null; + } + }; + scanner.scan(cc.getCompilationUnit(), null); + return ret; + } + + private void store(FileObject indexFolder, URL url, String resourceName, List<SymbolLocation> symbols) { + File cacheRoot = FileUtil.toFile(indexFolder); + File output = new File(cacheRoot, resourceName + ".mn"); //NOI18N + if (symbols.isEmpty()) { + if (output.exists()) { + output.delete(); + } + } else { + output.getParentFile().mkdirs(); + try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8))) { Review Comment: Fixed. Index file reading is allowed when no indexing task is running. ########## enterprise/micronaut/src/org/netbeans/modules/micronaut/symbol/MicronautSymbolFinder.java: ########## @@ -0,0 +1,336 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.micronaut.symbol; + +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.MethodTree; +import com.sun.source.util.TreePathScanner; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.stream.Collectors; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Name; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; +import javax.lang.model.util.Elements; +import javax.lang.model.util.Types; +import org.netbeans.api.editor.mimelookup.MimeRegistration; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.source.CompilationController; +import org.netbeans.api.java.source.JavaSource; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.parsing.api.Snapshot; +import org.netbeans.modules.parsing.spi.Parser; +import org.netbeans.modules.parsing.spi.indexing.Context; +import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexer; +import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexerFactory; +import org.netbeans.modules.parsing.spi.indexing.Indexable; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Exceptions; +import org.openide.util.Pair; + +/** + * + * @author Dusan Balek + */ +public final class MicronautSymbolFinder extends EmbeddingIndexer { + + public static final String NAME = "mn"; // NOI18N + public static final int VERSION = 1; + public static final MicronautSymbolFinder INSTANCE = new MicronautSymbolFinder(); + public static final String[] META_ANNOTATIONS = new String[] { + "io.micronaut.http.annotation.HttpMethodMapping", + "io.micronaut.context.annotation.Bean", + "jakarta.inject.Qualifier", + "jakarta.inject.Scope" + }; + + private final Map<Project, Boolean> map = new WeakHashMap<>(); + + @Override + protected void index(Indexable indexable, Parser.Result parserResult, Context context) { + CompilationController cc = CompilationController.get(parserResult); + if (initialize(cc)) { + try { + cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + List<SymbolLocation> symbols = scan(cc); + if (!symbols.isEmpty()) { + store(context.getIndexFolder(), indexable.getURL(), indexable.getRelativePath(), symbols); + } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + + private synchronized boolean initialize(CompilationController cc) { + Project p = FileOwnerQuery.getOwner(cc.getFileObject()); + Boolean ret = map.get(p); + if (ret == null) { + ClassPath cp = ClassPath.getClassPath(p.getProjectDirectory(), ClassPath.COMPILE); Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
