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

wujimin pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 34a69c7b1a43e5433c013e4ffc7bdea4300b65ca
Author: weichao666 <[email protected]>
AuthorDate: Thu Aug 30 10:38:52 2018 +0800

    [SCB-873] cache the getParameterNames,and optimize
---
 .../client/validation/ValidationServiceClient.java |  8 ++++
 .../swagger/generator/core/utils/ParamUtils.java   |  8 ++--
 .../validator/DefaultParameterNameProviderEx.java  | 40 ----------------
 .../invocation/validator/ParameterValidator.java   |  2 +-
 .../validator/ParanamerParameterNameProvider.java  | 55 ++++++++++++++++++++++
 ...ava => TestParanamerParameterNameProvider.java} | 40 ++++++++++------
 6 files changed, 94 insertions(+), 59 deletions(-)

diff --git 
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
 
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
index 9168d78..5600dda 100644
--- 
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
+++ 
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
@@ -19,6 +19,8 @@ package org.apache.servicecomb.demo.jaxrs.client.validation;
 
 import java.util.ArrayList;
 
+import javax.ws.rs.core.Response.Status;
+
 import org.apache.servicecomb.core.CseContext;
 import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.demo.jaxrs.server.validation.ValidationModel;
@@ -52,6 +54,8 @@ public class ValidationServiceClient {
       template.postForObject(urlPrefix + "/validate", model, 
ValidationModel.class);
       TestMgr.check(false, true);
     } catch (InvocationException e) {
+      TestMgr.check(400, e.getStatus().getStatusCode());
+      TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
       
TestMgr.check(e.getErrorData().toString().contains("propertyPath=errorCode.request.age"),
 true);
     }
 
@@ -61,6 +65,8 @@ public class ValidationServiceClient {
       template.postForObject(urlPrefix + "/validate", model, 
ValidationModel.class);
       TestMgr.check(false, true);
     } catch (InvocationException e) {
+      TestMgr.check(400, e.getStatus().getStatusCode());
+      TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
       
TestMgr.check(e.getErrorData().toString().contains("propertyPath=errorCode.request.members"),
 true);
     }
 
@@ -71,6 +77,8 @@ public class ValidationServiceClient {
       template.getForObject(urlPrefix + "/validateQuery", String.class);
       TestMgr.check(false, true);
     } catch (InvocationException e) {
+      TestMgr.check(400, e.getStatus().getStatusCode());
+      TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
       
TestMgr.check(e.getErrorData().toString().contains("propertyPath=queryValidate.name"),
 true);
     }
   }
diff --git 
a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
 
b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
index 2cd5e30..3046103 100644
--- 
a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
+++ 
b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
@@ -17,7 +17,7 @@
 
 package org.apache.servicecomb.swagger.generator.core.utils;
 
-import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
 import java.util.List;
@@ -57,9 +57,9 @@ public final class ParamUtils {
     return existName;
   }
 
-  public static String getParameterName(Constructor<?> constructor, int 
paramIdx) {
-    MethodParameter methodParameter = new MethodParameter(constructor, 
paramIdx);
-    return getParameterName(methodParameter, paramIdx);
+  public static String getParameterName(Executable methodOrConstructor, int 
parameterIndex) {
+    MethodParameter methodParameter = 
MethodParameter.forMethodOrConstructor(methodOrConstructor, parameterIndex);
+    return getParameterName(methodParameter, parameterIndex);
   }
 
   public static String getParameterName(Method method, int paramIdx) {
diff --git 
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProviderEx.java
 
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProviderEx.java
deleted file mode 100644
index 7d92169..0000000
--- 
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProviderEx.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package org.apache.servicecomb.swagger.invocation.validator;
-
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
-import java.lang.reflect.Parameter;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import javax.validation.ParameterNameProvider;
-
-import org.apache.servicecomb.swagger.generator.core.utils.ParamUtils;
-
-
-public class DefaultParameterNameProviderEx implements ParameterNameProvider {
-
-  @Override
-  public List<String> getParameterNames(Constructor<?> constructor) {
-    Parameter[] parameters = constructor.getParameters();
-    List<String> parameterNames = new ArrayList<>(parameters.length);
-
-    for (int i = 0; i < parameters.length; i++) {
-      parameterNames.add(ParamUtils.getParameterName(constructor, i));
-    }
-
-    return Collections.unmodifiableList(parameterNames);
-  }
-
-  @Override
-  public List<String> getParameterNames(Method method) {
-    Parameter[] parameters = method.getParameters();
-    List<String> parameterNames = new ArrayList<>(parameters.length);
-
-    for (int i = 0; i < parameters.length; i++) {
-      parameterNames.add(ParamUtils.getParameterName(method, i));
-    }
-
-    return Collections.unmodifiableList(parameterNames);
-  }
-}
diff --git 
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
 
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
index e5bf3f0..878555a 100644
--- 
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
+++ 
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
@@ -46,7 +46,7 @@ public class ParameterValidator implements 
ProducerInvokeExtension {
       ValidatorFactory factory =
           Validation.byDefaultProvider()
               .configure()
-              .parameterNameProvider(new DefaultParameterNameProviderEx())
+              .parameterNameProvider(new ParanamerParameterNameProvider())
               .buildValidatorFactory();
       executableValidator = factory.getValidator().forExecutables();
     }
diff --git 
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParanamerParameterNameProvider.java
 
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParanamerParameterNameProvider.java
new file mode 100644
index 0000000..024186a
--- /dev/null
+++ 
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParanamerParameterNameProvider.java
@@ -0,0 +1,55 @@
+/*
+ * 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.servicecomb.swagger.invocation.validator;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.validation.ParameterNameProvider;
+
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.swagger.generator.core.utils.ParamUtils;
+
+
+public class ParanamerParameterNameProvider implements ParameterNameProvider {
+  private final Map<AccessibleObject, List<String>> methodCache = new 
ConcurrentHashMapEx<>();
+
+  @Override
+  public List<String> getParameterNames(Constructor<?> constructor) {
+    return methodCache.computeIfAbsent(constructor, k -> 
getParameterNamesEx(constructor));
+  }
+
+  @Override
+  public List<String> getParameterNames(Method method) {
+    return methodCache.computeIfAbsent(method, k -> 
getParameterNamesEx(method));
+  }
+
+  private List<String> getParameterNamesEx(Executable methodOrConstructor) {
+    int parameterCount = methodOrConstructor.getParameterCount();
+    List<String> parameterNames = new ArrayList<>(parameterCount);
+
+    for (int i = 0; i < parameterCount; i++) {
+      parameterNames.add(ParamUtils.getParameterName(methodOrConstructor, i));
+    }
+    return Collections.unmodifiableList(parameterNames);
+  }
+}
diff --git 
a/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProviderEx.java
 
b/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestParanamerParameterNameProvider.java
similarity index 53%
rename from 
swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProviderEx.java
rename to 
swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestParanamerParameterNameProvider.java
index 360b60e..df6e154 100644
--- 
a/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProviderEx.java
+++ 
b/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestParanamerParameterNameProvider.java
@@ -1,16 +1,28 @@
+/*
+ * 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.servicecomb.swagger.invocation.validator;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class TestDefaultParameterNameProviderEx {
+public class TestParanamerParameterNameProvider {
   static class ValidatorForTest {
     static class Student {
       private String name;
@@ -74,30 +86,30 @@ public class TestDefaultParameterNameProviderEx {
 
   Class<ValidatorForTest> validatorForTest = ValidatorForTest.class;
 
-  DefaultParameterNameProviderEx parameterNameProviderEx = new 
DefaultParameterNameProviderEx();
+  ParanamerParameterNameProvider parameterNameProvider = new 
ParanamerParameterNameProvider();
 
   @Test
   public void testMethod() throws NoSuchMethodException {
     Method method = validatorForTest.getMethod("add", int.class, int.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(method), 
Matchers.contains("a", "b"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(method), 
Matchers.contains("a", "b"));
     method = validatorForTest.getMethod("sayHi", String.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(method), 
Matchers.contains("hi"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(method), 
Matchers.contains("hi"));
     method = validatorForTest.getMethod("sayHello", 
ValidatorForTest.Student.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(method), 
Matchers.contains("student"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(method), 
Matchers.contains("student"));
     method = validatorForTest.getMethod("setTest", String.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(method), 
Matchers.contains("grade"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(method), 
Matchers.contains("grade"));
     method = validatorForTest.getMethod("getNumber");
-    
Assert.assertTrue(parameterNameProviderEx.getParameterNames(method).isEmpty());
+    
Assert.assertTrue(parameterNameProvider.getParameterNames(method).isEmpty());
     method = validatorForTest.getMethod("setNumber", int.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(method), 
Matchers.contains("number"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(method), 
Matchers.contains("number"));
   }
 
   @Test
   public void testConstructor() throws NoSuchMethodException {
     Constructor<ValidatorForTest> constructor = 
validatorForTest.getConstructor(String.class, int.class);
-    Assert.assertThat(parameterNameProviderEx.getParameterNames(constructor), 
Matchers.contains("grade", "number"));
+    Assert.assertThat(parameterNameProvider.getParameterNames(constructor), 
Matchers.contains("grade", "number"));
     constructor = validatorForTest.getConstructor();
-    
Assert.assertTrue(parameterNameProviderEx.getParameterNames(constructor).isEmpty());
+    
Assert.assertTrue(parameterNameProvider.getParameterNames(constructor).isEmpty());
 
   }
 }

Reply via email to