You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2023/03/27 08:25:58 UTC

[james-project] branch master updated: JAMES-3896 Fix Mailet doc for non-default constructors (#1504)

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 903902834c JAMES-3896 Fix Mailet doc for non-default constructors (#1504)
903902834c is described below

commit 903902834c2e317f50e0d88563d9545533132ce8
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Mon Mar 27 10:25:51 2023 +0200

    JAMES-3896 Fix Mailet doc for non-default constructors (#1504)
---
 .../james/mailet/DefaultDescriptorsExtractor.java  | 18 ++++++-
 .../mailet/DefaultDescriptorsExtractorTest.java    | 19 +++++++
 .../mailet/constructor/ConstructorMailet.java      | 58 ++++++++++++++++++++++
 3 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/mailet/mailetdocs-maven-plugin/src/main/java/org/apache/james/mailet/DefaultDescriptorsExtractor.java b/mailet/mailetdocs-maven-plugin/src/main/java/org/apache/james/mailet/DefaultDescriptorsExtractor.java
index b78ac1dfe1..57fa2b7bfd 100644
--- a/mailet/mailetdocs-maven-plugin/src/main/java/org/apache/james/mailet/DefaultDescriptorsExtractor.java
+++ b/mailet/mailetdocs-maven-plugin/src/main/java/org/apache/james/mailet/DefaultDescriptorsExtractor.java
@@ -20,6 +20,7 @@
 package org.apache.james.mailet;
 
 import java.io.File;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -171,8 +172,9 @@ public class DefaultDescriptorsExtractor {
 
     private Optional<String> fetchInfo(Log log, String nameOfClass, Class<?> klass, String infoMethodName, Type type) {
         try {
-            final Object instance = klass.getDeclaredConstructor().newInstance();
-            final String info = (String) klass.getMethod(infoMethodName).invoke(instance);
+            Object instance = instantiateClass(klass);
+            String info = (String) klass.getMethod(infoMethodName).invoke(instance);
+
             if (info != null && info.length() > 0) {
                 return Optional.of(info);
             }
@@ -184,6 +186,18 @@ public class DefaultDescriptorsExtractor {
         return Optional.empty();
     }
 
+    private Object instantiateClass(Class<?> klass) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
+        Constructor<?>[] constructors = klass.getConstructors();
+        if (constructors.length > 0) {
+            Constructor<?> constructor = constructors[0];
+            Object[] studentObjects = new Object[constructor.getParameterCount()];
+
+            return constructor.newInstance(studentObjects);
+        }
+        return klass.getDeclaredConstructor()
+            .newInstance();
+    }
+
 
     private boolean isExperimental(JavaClass javaClass) {
         return javaClass.getAnnotations()
diff --git a/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/DefaultDescriptorsExtractorTest.java b/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/DefaultDescriptorsExtractorTest.java
index 7be8ee03ee..86171f5ec2 100644
--- a/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/DefaultDescriptorsExtractorTest.java
+++ b/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/DefaultDescriptorsExtractorTest.java
@@ -71,6 +71,25 @@ class DefaultDescriptorsExtractorTest {
 
         assertThat(descriptors).containsOnly(experimentalMailet, nonExperimentalMailet);
     }
+
+    @Test
+    void extractShouldSupportArgumentsInConstructor() {
+        when(mavenProject.getCompileSourceRoots())
+            .thenReturn(ImmutableList.of("src/test/java/org/apache/james/mailet/constructor"));
+
+        List<MailetMatcherDescriptor> descriptors = testee.extract(mavenProject, log)
+            .descriptors();
+
+        MailetMatcherDescriptor mailet = MailetMatcherDescriptor.builder()
+            .name("ConstructorMailet")
+            .fullyQualifiedClassName("org.apache.james.mailet.constructor.ConstructorMailet")
+            .type(Type.MAILET)
+            .info("info")
+            .noClassDocs()
+            .isNotExperimental();
+
+        assertThat(descriptors).containsOnly(mailet);
+    }
     
     @Test
     void extractShouldExcludeAnnotatedClassesWhenScanningMailets() {
diff --git a/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/constructor/ConstructorMailet.java b/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/constructor/ConstructorMailet.java
new file mode 100644
index 0000000000..35a8fca48c
--- /dev/null
+++ b/mailet/mailetdocs-maven-plugin/src/test/java/org/apache/james/mailet/constructor/ConstructorMailet.java
@@ -0,0 +1,58 @@
+/****************************************************************
+ * 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.james.mailet.constructor;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Experimental;
+import org.apache.mailet.Mail;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.MailetConfig;
+
+public class ConstructorMailet implements Mailet {
+    private final String arg;
+
+    public ConstructorMailet(String arg) {
+        this.arg = arg;
+    }
+
+    @Override
+    public void init(MailetConfig config) throws MessagingException {
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+    }
+
+    @Override
+    public void destroy() {
+    }
+
+    @Override
+    public MailetConfig getMailetConfig() {
+        return null;
+    }
+
+    @Override
+    public String getMailetInfo() {
+        return "info";
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org