You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/02/04 06:55:16 UTC

[dubbo-spi-extensions] 41/47: 抽取常量

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

liujun pushed a commit to branch 2.7.x
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git

commit d5708fdb6fe9f30c6768ba21bbd704ac2e3041c0
Author: qq213539 <21...@qq.com>
AuthorDate: Wed Feb 3 14:15:27 2021 +0800

    抽取常量
---
 .../org/apache/dubbo/apidocs/core/Constants.java   | 79 ++++++++++++++++++++++
 .../core/DubboApiDocsAnnotationScanner.java        | 23 +++++--
 .../apache/dubbo/apidocs/utils/ClassTypeUtil.java  | 23 +++++--
 3 files changed, 111 insertions(+), 14 deletions(-)

diff --git a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/Constants.java b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/Constants.java
new file mode 100644
index 0000000..9533619
--- /dev/null
+++ b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/Constants.java
@@ -0,0 +1,79 @@
+/*
+ * 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.dubbo.apidocs.core;
+
+/**
+ * constants.
+ *
+ * @date 2021/2/3 10:19
+ */
+public class Constants {
+
+    public static final String DOT = ".";
+
+    /**
+     * Method parameter index left boundary.
+     */
+    public static final String METHOD_PARAM_INDEX_BOUNDARY_LEFT = "[";
+
+    /**
+     * Method parameter index right boundary.
+     */
+    public static final String METHOD_PARAM_INDEX_BOUNDARY_RIGHT = "]";
+
+    /**
+     * Method parameter separator.
+     */
+    public static final String METHOD_PARAMETER_SEPARATOR = " | ";
+
+    /**
+     * To skip when processing fields: serialVersionUID.
+     */
+    public static final String SKIP_FIELD_SERIALVERSIONUID = "serialVersionUID";
+
+    /**
+     * To skip when processing fields: this$0.
+     */
+    public static final String SKIP_FIELD_THIS$0 = "this$0";
+
+    public static final String CLASS_FIELD_NAME = "class";
+
+    /**
+     * Allowed value of drop-down selection box: true.
+     */
+    public static final String ALLOWABLE_BOOLEAN_TRUE = "true";
+
+    /**
+     * Allowed value of drop-down selection box: false.
+     */
+    public static final String ALLOWABLE_BOOLEAN_FALSE = "false";
+
+    /**
+     * Enumerating the name method in a type.
+     */
+    public static final String METHOD_NAME_NAME = "name";
+
+    public static final String SQUARE_BRACKET_LEFT = "【";
+
+    public static final String SQUARE_BRACKET_RIGHT = "】";
+
+    public static final String RESPONSE_STR_EXAMPLE = "example: ";
+
+    public static final String ENUM_VALUES_SEPARATOR = "|";
+
+
+}
diff --git a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
index c9259bc..2c70a59 100644
--- a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
+++ b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
@@ -64,6 +64,15 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import static org.apache.dubbo.apidocs.core.Constants.DOT;
+import static org.apache.dubbo.apidocs.core.Constants.METHOD_PARAM_INDEX_BOUNDARY_LEFT;
+import static org.apache.dubbo.apidocs.core.Constants.METHOD_PARAM_INDEX_BOUNDARY_RIGHT;
+import static org.apache.dubbo.apidocs.core.Constants.METHOD_PARAMETER_SEPARATOR;
+import static org.apache.dubbo.apidocs.core.Constants.SKIP_FIELD_SERIALVERSIONUID;
+import static org.apache.dubbo.apidocs.core.Constants.SKIP_FIELD_THIS$0;
+import static org.apache.dubbo.apidocs.core.Constants.ALLOWABLE_BOOLEAN_TRUE;
+import static org.apache.dubbo.apidocs.core.Constants.ALLOWABLE_BOOLEAN_FALSE;
+import static org.apache.dubbo.apidocs.core.Constants.METHOD_NAME_NAME;
 
 /**
  * Scan and process dubbo doc annotations.
@@ -164,7 +173,7 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
         // API details in cache, contain interface parameters and response information
         ApiCacheItem apiParamsAndResp = new ApiCacheItem();
         DubboApiDocsCache.addApiParamsAndResp(
-                moduleAnn.apiInterface().getCanonicalName() + "." + method.getName(), apiParamsAndResp);
+                moduleAnn.apiInterface().getCanonicalName() + DOT + method.getName(), apiParamsAndResp);
 
         Class<?>[] argsClass = method.getParameterTypes();
         Annotation[][] argsAnns = method.getParameterAnnotations();
@@ -184,9 +193,9 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
         for (int i = 0; i < argsClass.length; i++) {
             Class<?> argClass = argsClass[i];
             Type parameterType = parametersTypes[i];
-            methodParamInfoSb.append("[").append(i).append("]").append(argClass.getCanonicalName());
+            methodParamInfoSb.append(METHOD_PARAM_INDEX_BOUNDARY_LEFT).append(i).append(METHOD_PARAM_INDEX_BOUNDARY_RIGHT).append(argClass.getCanonicalName());
             if (i + 1 < argsClass.length) {
-                methodParamInfoSb.append(" | ");
+                methodParamInfoSb.append(METHOD_PARAMETER_SEPARATOR);
             }
             Annotation[] argAnns = argsAnns[i];
             ApiParamsCacheItem paramListItem = new ApiParamsCacheItem();
@@ -257,7 +266,7 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
         List<Field> allFields = ClassTypeUtil.getAllFields(null, argClass);
         if (allFields.size() > 0) {
             for (Field field : allFields) {
-                if ("serialVersionUID".equals(field.getName()) || "this$0".equals(field.getName())) {
+                if (SKIP_FIELD_SERIALVERSIONUID.equals(field.getName()) || SKIP_FIELD_THIS$0.equals(field.getName())) {
                     continue;
                 }
                 ParamBean paramBean = new ParamBean();
@@ -397,7 +406,7 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
         if (Boolean.class.isAssignableFrom(classType) || boolean.class.isAssignableFrom(classType)) {
             param.setHtmlType(HtmlTypeEnum.SELECT);
             // Boolean can only be true / false. No matter what the previous allowed value is, it is forced to replace
-            param.setAllowableValues(new String[]{"true", "false"});
+            param.setAllowableValues(new String[]{ALLOWABLE_BOOLEAN_TRUE, ALLOWABLE_BOOLEAN_FALSE});
             processed = true;
         } else if (Enum.class.isAssignableFrom(classType)) {
             // process enum
@@ -409,13 +418,13 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
                 Object[] enumConstants = classType.getEnumConstants();
                 String[] enumAllowableValues = new String[enumConstants.length];
                 try {
-                    Method getNameMethod = classType.getMethod("name");
+                    Method getNameMethod = classType.getMethod(METHOD_NAME_NAME);
                     for (int i = 0; i < enumConstants.length; i++) {
                         Object obj = enumConstants[i];
                         enumAllowableValues[i] = (String) getNameMethod.invoke(obj);
                     }
                 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
-                    LOG.error("", e);
+                    LOG.error(e.getMessage(), e);
                 }
                 param.setAllowableValues(enumAllowableValues);
             }
diff --git a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/utils/ClassTypeUtil.java b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/utils/ClassTypeUtil.java
index 5b44a93..b30f7d1 100644
--- a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/utils/ClassTypeUtil.java
+++ b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/utils/ClassTypeUtil.java
@@ -46,6 +46,15 @@ import org.apache.dubbo.apidocs.annotations.ResponseProperty;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 
+import static org.apache.dubbo.apidocs.core.Constants.SKIP_FIELD_SERIALVERSIONUID;
+import static org.apache.dubbo.apidocs.core.Constants.SKIP_FIELD_THIS$0;
+import static org.apache.dubbo.apidocs.core.Constants.CLASS_FIELD_NAME;
+import static org.apache.dubbo.apidocs.core.Constants.SQUARE_BRACKET_LEFT;
+import static org.apache.dubbo.apidocs.core.Constants.SQUARE_BRACKET_RIGHT;
+import static org.apache.dubbo.apidocs.core.Constants.RESPONSE_STR_EXAMPLE;
+import static org.apache.dubbo.apidocs.core.Constants.ENUM_VALUES_SEPARATOR;
+import static org.apache.dubbo.apidocs.core.Constants.METHOD_NAME_NAME;
+
 /**
  * Java class tool class, special for Dubbo doc.
  */
@@ -133,12 +142,12 @@ public class ClassTypeUtil {
 
         Map<String, Object> result = new HashMap<>(16);
         if (isBuildClassAttribute) {
-            result.put("class", classType.getCanonicalName());
+            result.put(CLASS_FIELD_NAME, classType.getCanonicalName());
         }
         // get all fields
         List<Field> allFields = getAllFields(null, classType);
         for (Field field2 : allFields) {
-            if ("serialVersionUID".equals(field2.getName()) || "this$0".equals(field2.getName())) {
+            if (SKIP_FIELD_SERIALVERSIONUID.equals(field2.getName()) || SKIP_FIELD_THIS$0.equals(field2.getName())) {
                 continue;
             }
             if (String.class.isAssignableFrom(field2.getType())) {
@@ -149,7 +158,7 @@ public class ClassTypeUtil {
                     ResponseProperty responseProperty = field2.getAnnotation(ResponseProperty.class);
                     StringBuilder strValue = new StringBuilder(responseProperty.value());
                     if (StringUtils.isNotBlank(responseProperty.example())) {
-                        strValue.append("【example: ").append(responseProperty.example()).append("】");
+                        strValue.append(SQUARE_BRACKET_LEFT).append(RESPONSE_STR_EXAMPLE).append(responseProperty.example()).append(SQUARE_BRACKET_RIGHT);
                     }
                     result.put(field2.getName(), strValue.toString());
                 } else {
@@ -199,14 +208,14 @@ public class ClassTypeUtil {
             return "【" + LocalDateTime.class.getName() + "】yyyy-MM-dd HH:mm:ss";
         } else if (Enum.class.isAssignableFrom(classType)) {
             Object[] enumConstants = classType.getEnumConstants();
-            StringBuilder sb = new StringBuilder("|");
+            StringBuilder sb = new StringBuilder(ENUM_VALUES_SEPARATOR);
             try {
-                Method getName = classType.getMethod("name");
+                Method getName = classType.getMethod(METHOD_NAME_NAME);
                 for (Object obj : enumConstants) {
-                    sb.append(getName.invoke(obj)).append("|");
+                    sb.append(getName.invoke(obj)).append(ENUM_VALUES_SEPARATOR);
                 }
             } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
-                LOG.error("", e);
+                LOG.error(e.getMessage(), e);
             }
             return sb.toString();
         } else if (classType.isArray()) {