ppkarwasz commented on code in PR #102:
URL: 
https://github.com/apache/logging-log4j-tools/pull/102#discussion_r1484531862


##########
log4j-docgen/src/main/java/org/apache/logging/log4j/docgen/processor/DocGenProcessor.java:
##########
@@ -0,0 +1,692 @@
+/*
+ * 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.apache.logging.log4j.docgen.processor;
+
+import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;
+import static org.apache.commons.lang3.StringUtils.defaultString;
+
+import aQute.bnd.annotation.Resolution;
+import aQute.bnd.annotation.spi.ServiceProvider;
+import com.sun.source.doctree.DocCommentTree;
+import com.sun.source.doctree.DocTree;
+import com.sun.source.doctree.ParamTree;
+import com.sun.source.util.DocTrees;
+import com.sun.source.util.SimpleDocTreeVisitor;
+import java.io.IOException;
+import java.io.Writer;
+import java.lang.annotation.Annotation;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Messager;
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.annotation.processing.Processor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.Name;
+import javax.lang.model.element.QualifiedNameable;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.ArrayType;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.NoType;
+import javax.lang.model.type.PrimitiveType;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.type.TypeVariable;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.SimpleElementVisitor8;
+import javax.lang.model.util.SimpleTypeVisitor8;
+import javax.lang.model.util.Types;
+import javax.tools.Diagnostic;
+import javax.tools.FileObject;
+import javax.tools.StandardLocation;
+import javax.xml.stream.XMLStreamException;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.docgen.AbstractType;
+import org.apache.logging.log4j.docgen.Description;
+import org.apache.logging.log4j.docgen.PluginAttribute;
+import org.apache.logging.log4j.docgen.PluginElement;
+import org.apache.logging.log4j.docgen.PluginSet;
+import org.apache.logging.log4j.docgen.PluginType;
+import org.apache.logging.log4j.docgen.ScalarType;
+import org.apache.logging.log4j.docgen.ScalarValue;
+import org.apache.logging.log4j.docgen.Type;
+import org.apache.logging.log4j.docgen.io.stax.PluginBundleStaxWriter;
+import org.apache.logging.log4j.plugins.Factory;
+import org.apache.logging.log4j.plugins.Namespace;
+import org.apache.logging.log4j.plugins.Plugin;
+import org.apache.logging.log4j.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.plugins.PluginFactory;
+import org.apache.logging.log4j.plugins.validation.constraints.Required;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
+
+@ServiceProvider(value = Processor.class, resolution = Resolution.OPTIONAL)
+@SupportedAnnotationTypes("org.apache.logging.log4j.plugins.*")
+@SupportedSourceVersion(SourceVersion.RELEASE_17)
+@NullMarked
+public class DocGenProcessor extends AbstractProcessor {
+
+    private static final String MULTIPLICITY_UNBOUNDED = "*";
+    private static final CharSequence[] GETTER_SETTER_PREFIXES = {"get", "is", 
"set"};
+    private static final List<Class<? extends Annotation>> 
PROPERTY_ANNOTATION_TYPES = List.of(
+            PluginBuilderAttribute.class,
+            org.apache.logging.log4j.plugins.PluginAttribute.class,
+            org.apache.logging.log4j.plugins.PluginElement.class);
+    private static final List<Class<? extends Annotation>> 
FACTORY_ANNOTATION_TYPES =
+            List.of(Factory.class, PluginFactory.class);
+    /**
+     * Reference types from the {@code java.*} namespace that are described
+     * in {@code org/apache/logging/log4j/docgen/internal/configuration.xml}
+     */
+    private static final Set<String> KNOWN_SCALAR_TYPES = Set.of(
+            "java.lang.Boolean",
+            "java.lang.Character",
+            "java.lang.Byte",
+            "java.lang.Short",
+            "java.lang.Integer",
+            "java.lang.Long",
+            "java.lang.Float",
+            "java.lang.Double",
+            "java.lang.String");
+
+    // Plugins
+    private final Map<String, PluginType> pluginTypes = new TreeMap<>();
+    // Other interfaces
+    private final Map<String, TypeElement> abstractTypes = new TreeMap<>();
+    // Scalar types
+    private final Map<String, TypeElement> scalarTypes = new TreeMap<>();
+
+    private AsciidocConverter converter;
+    private DocTrees docTrees;
+    private Elements elements;
+    private Types types;
+    private Messager messager;
+    // Type corresponding to java.util.Collection
+    private DeclaredType collectionType;
+    // Type corresponding to java.lang.Enum
+    private DeclaredType enumType;
+
+    @SuppressWarnings({"DataFlowIssue", "unused"})
+    public DocGenProcessor() {
+        converter = null;
+        docTrees = null;
+        elements = null;
+        types = null;
+        messager = null;
+        collectionType = null;
+        enumType = null;
+    }
+
+    // For testing and silencing nullability warnings
+    @SuppressWarnings("unused")
+    DocGenProcessor(final ProcessingEnvironment processingEnv) {
+        docTrees = DocTrees.instance(processingEnv);
+        converter = new AsciidocConverter(docTrees);
+        elements = processingEnv.getElementUtils();
+        types = processingEnv.getTypeUtils();
+        messager = processingEnv.getMessager();
+        collectionType = (DeclaredType)
+                
types.erasure(elements.getTypeElement("java.util.Collection").asType());
+        enumType = (DeclaredType)
+                
types.erasure(elements.getTypeElement("java.lang.Enum").asType());
+    }
+
+    @Override
+    public synchronized void init(final ProcessingEnvironment processingEnv) {
+        super.init(processingEnv);
+        docTrees = DocTrees.instance(processingEnv);
+        converter = new AsciidocConverter(docTrees);
+        elements = processingEnv.getElementUtils();
+        types = processingEnv.getTypeUtils();
+        messager = processingEnv.getMessager();
+        collectionType = (DeclaredType)
+                
types.erasure(elements.getTypeElement("java.util.Collection").asType());
+        enumType = (DeclaredType)
+                
types.erasure(elements.getTypeElement("java.lang.Enum").asType());
+    }
+
+    @Override
+    public boolean process(final Set<? extends TypeElement> annotations, final 
RoundEnvironment roundEnv) {
+        final PluginSet set = new PluginSet();
+        // First step: document plugins
+        roundEnv.getElementsAnnotatedWith(Plugin.class).forEach(element -> {
+            if (element instanceof final TypeElement typeElement) {
+                final PluginType pluginType = new PluginType();
+                final Plugin pluginAnnotation = 
element.getAnnotation(Plugin.class);
+                processPlugin(typeElement, pluginAnnotation, pluginType);
+                pluginTypes.put(pluginType.getClassName(), pluginType);
+            }
+        });
+        pluginTypes.values().forEach(set::addPlugin);
+        // Second step: document abstract types
+        abstractTypes.values().forEach(type -> {
+            final AbstractType abstractType = new AbstractType();
+            processAbstractType(type, abstractType);
+            if (!abstractType.getDescription().getText().isEmpty()) {
+                set.addAbstractType(abstractType);
+            }
+        });
+        // Second step: document scalars
+        scalarTypes.values().forEach(type -> {
+            final ScalarType scalarType = new ScalarType();
+            processScalarType(type, scalarType);
+            set.addScalar(scalarType);
+        });
+        // Write the result file
+        if (roundEnv.processingOver()) {
+            try {
+                final FileObject output = processingEnv
+                        .getFiler()
+                        .createResource(StandardLocation.CLASS_OUTPUT, "", 
"META-INF/log4j/plugins.xml");
+
+                try (final Writer writer = output.openWriter()) {
+                    new PluginBundleStaxWriter().write(writer, set);
+                }
+            } catch (final IOException | XMLStreamException e) {
+                messager.printMessage(
+                        Diagnostic.Kind.ERROR,
+                        "An error occurred while writing to 
`META-INF/log4j/plugins.xml`: " + e.getMessage());
+            }
+        }
+        return false;
+    }
+
+    private void processType(final QualifiedNameable element, final Type 
docgenType) {
+        // Class name
+        docgenType.setClassName(element.getQualifiedName().toString());
+        // Description
+        docgenType.setDescription(createDescription(element, null));
+    }
+
+    private void processAbstractType(final QualifiedNameable element, final 
AbstractType abstractType) {
+        processType(element, abstractType);
+    }
+
+    private void processScalarType(final TypeElement element, final ScalarType 
scalarType) {
+        processType(element, scalarType);
+        if (types.isSubtype(element.asType(), enumType)) {
+            for (final Element member : element.getEnclosedElements()) {
+                if (member instanceof final VariableElement field
+                        && field.getModifiers().contains(Modifier.STATIC)
+                        && types.isSameType(field.asType(), element.asType())) 
{
+                    final ScalarValue value = new ScalarValue();

Review Comment:
   Maybe the code lacks some comments. Now it should be better.
   
   Thank your for the review.



-- 
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]

Reply via email to