You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/09/03 11:46:24 UTC

[camel] 03/10: CAMEL-15498: Add java source parser for discovering API methods for API based components.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 7e6a0b13d93eb23b72ab3c339519ae2a18135e1f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Sep 3 10:37:40 2020 +0200

    CAMEL-15498:  Add java source parser for discovering API methods for API based components.
---
 .../maven/JavaSourceApiMethodGeneratorMojo.java    |  27 +---
 .../org/apache/camel/maven/JavaSourceParser.java   |   9 +-
 .../apache/camel/maven/JavaSourceParserTest.java   |  19 ++-
 .../src/test/resources/CustomerGateway.java        | 154 +++++++++++++++++++++
 4 files changed, 185 insertions(+), 24 deletions(-)

diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
index 4a2c9d3..40599f2 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.camel.support.component.ApiMethodParser;
@@ -106,26 +105,14 @@ public class JavaSourceApiMethodGeneratorMojo extends AbstractApiMethodGenerator
                             && (includeMethodPatterns == null || includeMethodPatterns.matcher(method).find())
                             && (excludeMethodPatterns == null || !excludeMethodPatterns.matcher(method).find())) {
 
-                        final int leftBracket = method.indexOf('(');
-                        final String name = method.substring(0, leftBracket);
-                        final String args = method.substring(leftBracket + 1, method.length() - 1);
-                        String[] types;
-                        if (args.isEmpty()) {
-                            types = new String[0];
-                        } else {
-                            // get raw types from args
-                            final List<String> rawTypes = new ArrayList<>();
-                            final Matcher argTypesMatcher = RAW_ARGTYPES_PATTERN.matcher(args);
-                            while (argTypesMatcher.find()) {
-                                rawTypes.add(argTypesMatcher.group(1));
-                            }
-                            types = rawTypes.toArray(new String[rawTypes.size()]);
-                        }
-                        final String resultType = getResultType(aClass, name, types);
-                        if (resultType != null) {
+                        method = method.replace("public ", "");
+                        int whitespace = method.indexOf(' ');
+                        int leftBracket = method.indexOf('(');
+                        String resultType = method.substring(0, whitespace);
+                        String name = method.substring(whitespace + 1, leftBracket);
+                        if (!"void".equals(resultType)) {
                             SignatureModel model = new SignatureModel();
-                            String signature = resultType + " " + name + methodMap.get(method);
-                            model.setSignature(signature);
+                            model.setSignature(method);
                             Map<String, String> params = parser.getParameters().get(name);
                             model.setParameters(params);
                             result.put(method, model);
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
index 730d5f5..f877afe 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
@@ -58,6 +58,9 @@ public class JavaSourceParser {
                 String result = signature.substring(pos + 1).trim();
                 // lets use FQN types
                 result = clazz.resolveType(result);
+                if (result == null || result.isEmpty()) {
+                    result = "void";
+                }
 
                 List<JavaDocTag> params = ms.getJavaDoc().getTags("@param");
 
@@ -80,7 +83,7 @@ public class JavaSourceParser {
                     // need documentation for this parameter
                     docs.put(name, getJavadocValue(params, name));
                 }
-                sb.append(");");
+                sb.append(")");
 
                 signature = sb.toString();
                 parameters.put(ms.getName(), docs);
@@ -94,8 +97,8 @@ public class JavaSourceParser {
     private static String getJavadocValue(List<JavaDocTag> params, String name) {
         for (JavaDocTag tag : params) {
             String key = tag.getValue();
-            if (key.startsWith(name + " ")) {
-                String desc = key.substring(name.length() + 1);
+            if (key.startsWith(name)) {
+                String desc = key.substring(name.length());
                 desc = sanitizeJavaDocValue(desc);
                 return desc;
             }
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
index 369afa0..5b7cc30 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
@@ -33,7 +33,7 @@ public class JavaSourceParserTest {
         assertEquals(4, parser.getMethods().size());
 
         assertEquals(
-                "public com.braintreegateway.Result create(String customerId, com.braintreegateway.AddressRequest request);",
+                "public com.braintreegateway.Result create(String customerId, com.braintreegateway.AddressRequest request)",
                 parser.getMethods().get(0));
         assertEquals(2, parser.getParameters().get("create").size());
         assertEquals("The id of the Customer", parser.getParameters().get("create").get("customerId"));
@@ -43,4 +43,21 @@ public class JavaSourceParserTest {
 
     }
 
+    @Test
+    public void testGetMethodsCustomer() throws Exception {
+        final JavaSourceParser parser = new JavaSourceParser();
+
+        parser.parse(JavaSourceParserTest.class.getResourceAsStream("/CustomerGateway.java"));
+        assertEquals(7, parser.getMethods().size());
+
+        assertEquals(
+                "public com.braintreegateway.Result create(com.braintreegateway.CustomerRequest request)",
+                parser.getMethods().get(1));
+        assertEquals(1, parser.getParameters().get("create").size());
+        assertEquals("The request", parser.getParameters().get("create").get("request"));
+
+        parser.reset();
+
+    }
+
 }
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/resources/CustomerGateway.java b/tooling/maven/camel-api-component-maven-plugin/src/test/resources/CustomerGateway.java
new file mode 100644
index 0000000..aad12dc
--- /dev/null
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/resources/CustomerGateway.java
@@ -0,0 +1,154 @@
+/*
+ * 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 com.braintreegateway;
+
+import com.braintreegateway.exceptions.NotFoundException;
+import com.braintreegateway.util.Http;
+import com.braintreegateway.util.NodeWrapper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Provides methods to create, delete, find, and update {@link Customer}
+ * objects. This class does not need to be instantiated directly. Instead, use
+ * {@link BraintreeGateway#customer()} to get an instance of this class:
+ *
+ * <pre>
+ * BraintreeGateway gateway = new BraintreeGateway(...);
+ * gateway.customer().create(...)
+ * </pre>
+ *
+ * For more detailed information on {@link Customer Customers}, see <a href="https://developers.braintreepayments.com/reference/response/customer/java" target="_blank">https://developers.braintreepayments.com/reference/response/customer/java</a>
+ */
+public class CustomerGateway {
+    private Configuration configuration;
+    private Http http;
+
+    public CustomerGateway(Http http, Configuration configuration) {
+        this.configuration = configuration;
+        this.http = http;
+    }
+
+    /**
+     * Finds all Customers and returns a {@link ResourceCollection}.
+     *
+     * @return a {@link ResourceCollection}.
+     */
+    public ResourceCollection<Customer> all() {
+        NodeWrapper response = http.post(configuration.getMerchantPath() + "/customers/advanced_search_ids");
+        return new ResourceCollection<Customer>(new CustomerPager(this, new CustomerSearchRequest()), response);
+    }
+
+    List<Customer> fetchCustomers(CustomerSearchRequest query, List<String> ids) {
+        query.ids().in(ids);
+        NodeWrapper response = http.post(configuration.getMerchantPath() + "/customers/advanced_search", query);
+
+        List<Customer> items = new ArrayList<Customer>();
+        for (NodeWrapper node : response.findAll("customer")) {
+            items.add(new Customer(node));
+        }
+
+        return items;
+    }
+
+    /**
+     * Creates a {@link Customer}.
+     *
+     * @param request
+     *            the request.
+     * @return a {@link Result}.
+     */
+    public Result<Customer> create(CustomerRequest request) {
+        NodeWrapper node = http.post(configuration.getMerchantPath() + "/customers", request);
+        return new Result<Customer>(node, Customer.class);
+    }
+
+    /**
+     * Deletes a {@link Customer} by id.
+     *
+     * @param id
+     *            the id of the {@link Customer}.
+     * @return a {@link Result}.
+     */
+    public Result<Customer> delete(String id) {
+        http.delete(configuration.getMerchantPath() + "/customers/" + id);
+        return new Result<Customer>();
+    }
+
+    /**
+     * Finds a {@link Customer} by id.
+     *
+     * @param id
+     *            the id of the {@link Customer}.
+     * @return the {@link Customer} or raises a
+     *         {@link com.braintreegateway.exceptions.NotFoundException}.
+     */
+    public Customer find(String id) {
+        if(id == null || id.trim().equals(""))
+            throw new NotFoundException();
+
+        return new Customer(http.get(configuration.getMerchantPath() + "/customers/" + id));
+    }
+
+    /**
+     * Finds a {@link Customer} by id.
+     *
+     * @param id
+     *            the id of the {@link Customer}.
+     * @param associationFilterId
+     *            the id of the association filter to use.
+     * @return the {@link Customer} or raises a
+     *         {@link com.braintreegateway.exceptions.NotFoundException}.
+     */
+    public Customer find(String id, String associationFilterId) {
+        if(id == null || id.trim().equals(""))
+            throw new NotFoundException();
+
+        if(associationFilterId == null || associationFilterId.isEmpty())
+            throw new NotFoundException();
+
+        String queryParams = "?association_filter_id=" + associationFilterId;
+        return new Customer(http.get(configuration.getMerchantPath() + "/customers/" + id + queryParams));
+    }
+
+    /**
+     * Finds all Transactions that match the query and returns a {@link ResourceCollection}.
+     * See: <a href="https://developers.braintreepayments.com/reference/request/transaction/search/java" target="_blank">https://developers.braintreepayments.com/reference/request/transaction/search/java</a>
+     * @param query the request query to use for search
+     * @return a {@link ResourceCollection}.
+     */
+    public ResourceCollection<Customer> search(CustomerSearchRequest query) {
+        NodeWrapper node = http.post(configuration.getMerchantPath() + "/customers/advanced_search_ids", query);
+        return new ResourceCollection<Customer>(new CustomerPager(this, query), node);
+    }
+
+    /**
+     * Updates a {@link Customer}.
+     *
+     * @param id
+     *            the id of the {@link Customer}.
+     * @param request
+     *            the request.
+     * @return a {@link Result}.
+     */
+    public Result<Customer> update(String id, CustomerRequest request) {
+        NodeWrapper node = http.put(configuration.getMerchantPath() + "/customers/" + id, request);
+        return new Result<Customer>(node, Customer.class);
+    }
+
+}