[ 
https://issues.apache.org/jira/browse/SCB-922?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16628207#comment-16628207
 ] 

ASF GitHub Bot commented on SCB-922:
------------------------------------

wujimin closed pull request #916: [SCB-922] Collect getter setter from pojo
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/916
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/foundations/foundation-protobuf/pom.xml 
b/foundations/foundation-protobuf/pom.xml
index 9087a0f21..27df475b1 100644
--- a/foundations/foundation-protobuf/pom.xml
+++ b/foundations/foundation-protobuf/pom.xml
@@ -12,6 +12,10 @@
   <artifactId>foundation-protobuf</artifactId>
 
   <dependencies>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-common</artifactId>
+    </dependency>
     <dependency>
       <groupId>io.protostuff</groupId>
       <artifactId>protostuff-parser</artifactId>
diff --git 
a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptor.java
 
b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptor.java
new file mode 100644
index 000000000..bcd865f7e
--- /dev/null
+++ 
b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptor.java
@@ -0,0 +1,100 @@
+/*
+ * 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.foundation.protobuf.internal.bean;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils;
+import org.apache.servicecomb.foundation.common.utils.bean.Getter;
+import org.apache.servicecomb.foundation.common.utils.bean.Setter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.BeanDescription;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.SerializationConfig;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
+
+public class BeanDescriptor {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(BeanDescriptor.class);
+
+  private JavaType javaType;
+
+  private Map<String, Getter> getters = new HashMap<>();
+
+  private Map<String, Setter> setters = new HashMap<>();
+
+  public JavaType getJavaType() {
+    return javaType;
+  }
+
+  public Map<String, Getter> getGetters() {
+    return getters;
+  }
+
+  public Map<String, Setter> getSetters() {
+    return setters;
+  }
+
+  public void init(SerializationConfig serializationConfig, JavaType javaType) 
{
+    this.javaType = javaType;
+
+    BeanDescription beanDescription = serializationConfig.introspect(javaType);
+    for (BeanPropertyDefinition propertyDefinition : 
beanDescription.findProperties()) {
+      try {
+        initGetter(propertyDefinition);
+      } catch (Throwable e) {
+        LOGGER.error("failed to init getter for field {}:{}", 
javaType.getRawClass().getName(),
+            propertyDefinition.getName(), e);
+      }
+
+      try {
+        initSetter(propertyDefinition);
+      } catch (Throwable e) {
+        LOGGER.error("failed to init setter for field {}:{}", 
javaType.getRawClass().getName(),
+            propertyDefinition.getName(), e);
+      }
+    }
+  }
+
+  protected void initGetter(BeanPropertyDefinition propertyDefinition) throws 
Throwable {
+    if (propertyDefinition.hasGetter()) {
+      getters.put(propertyDefinition.getName(),
+          
LambdaMetafactoryUtils.createGetter(propertyDefinition.getGetter().getAnnotated()));
+      return;
+    }
+
+    if (propertyDefinition.hasField() && 
propertyDefinition.getField().isPublic()) {
+      getters.put(propertyDefinition.getName(),
+          
LambdaMetafactoryUtils.createGetter(propertyDefinition.getField().getAnnotated()));
+    }
+  }
+
+  protected void initSetter(BeanPropertyDefinition propertyDefinition) throws 
Throwable {
+    if (propertyDefinition.hasSetter()) {
+      setters.put(propertyDefinition.getName(),
+          
LambdaMetafactoryUtils.createSetter(propertyDefinition.getSetter().getAnnotated()));
+      return;
+    }
+
+    if (propertyDefinition.hasField() && 
propertyDefinition.getField().isPublic()) {
+      setters.put(propertyDefinition.getName(),
+          
LambdaMetafactoryUtils.createSetter(propertyDefinition.getField().getAnnotated()));
+    }
+  }
+}
diff --git 
a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptorManager.java
 
b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptorManager.java
new file mode 100644
index 000000000..58945b007
--- /dev/null
+++ 
b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/bean/BeanDescriptorManager.java
@@ -0,0 +1,50 @@
+/*
+ * 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.foundation.protobuf.internal.bean;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.SerializationConfig;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
+public class BeanDescriptorManager {
+  private SerializationConfig serializationConfig;
+
+  private Map<Type, BeanDescriptor> beanDescriptors = new 
ConcurrentHashMapEx<>();
+
+  public BeanDescriptorManager(SerializationConfig serializationConfig) {
+    this.serializationConfig = serializationConfig;
+  }
+
+  public BeanDescriptor getOrCreateBeanDescriptor(Type type) {
+    return beanDescriptors.computeIfAbsent(type, this::createBeanDescriptor);
+  }
+
+  protected BeanDescriptor createBeanDescriptor(Type type) {
+    return 
createBeanDescriptor(TypeFactory.defaultInstance().constructType(type));
+  }
+
+  protected BeanDescriptor createBeanDescriptor(JavaType javaType) {
+    BeanDescriptor beanDescriptor = new BeanDescriptor();
+    beanDescriptor.init(serializationConfig, javaType);
+    return beanDescriptor;
+  }
+}
diff --git 
a/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/bean/TestBeanDescriptorManager.java
 
b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/bean/TestBeanDescriptorManager.java
new file mode 100644
index 000000000..0501eb213
--- /dev/null
+++ 
b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/internal/bean/TestBeanDescriptorManager.java
@@ -0,0 +1,103 @@
+/*
+ * 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.foundation.protobuf.internal.bean;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class TestBeanDescriptorManager {
+  public static class Model {
+    private int both;
+
+    private int onlyGet;
+
+    private int onlySet;
+
+    public int direct;
+
+    public int getBoth() {
+      return both;
+    }
+
+    public void setBoth(int both) {
+      this.both = both;
+    }
+
+    public int getOnlyGet() {
+      return onlyGet;
+    }
+
+    public void onlyGet(int value) {
+      this.onlyGet = value;
+    }
+
+    public void setOnlySet(int onlySet) {
+      this.onlySet = onlySet;
+    }
+
+    public int onlySet() {
+      return onlySet;
+    }
+  }
+
+  static ObjectMapper mapper = new ObjectMapper();
+
+  static BeanDescriptorManager beanDescriptorManager = new 
BeanDescriptorManager(mapper.getSerializationConfig());
+
+  static BeanDescriptor beanDescriptor = 
beanDescriptorManager.getOrCreateBeanDescriptor(Model.class);
+
+  Model model = new Model();
+
+  @Test
+  public void getOrCreate() {
+    Assert.assertSame(beanDescriptor, 
beanDescriptorManager.getOrCreateBeanDescriptor(Model.class));
+    Assert.assertSame(Model.class, beanDescriptor.getJavaType().getRawClass());
+  }
+
+  @Test
+  public void both() throws Throwable {
+    beanDescriptor.getSetters().get("both").set(model, 1);
+    Assert.assertEquals(1, beanDescriptor.getGetters().get("both").get(model));
+    Assert.assertEquals(1, model.getBoth());
+  }
+
+  @Test
+  public void onlyGet() throws Throwable {
+    Assert.assertNull(beanDescriptor.getSetters().get("onlyGet"));
+
+    model.onlyGet(1);
+    Assert.assertEquals(1, 
beanDescriptor.getGetters().get("onlyGet").get(model));
+    Assert.assertEquals(1, model.getOnlyGet());
+  }
+
+  @Test
+  public void onlySet() throws Throwable {
+    Assert.assertNull(beanDescriptor.getGetters().get("onlySet"));
+
+    beanDescriptor.getSetters().get("onlySet").set(model, 1);
+    Assert.assertEquals(1, model.onlySet());
+  }
+
+  @Test
+  public void direct() throws Throwable {
+    beanDescriptor.getSetters().get("direct").set(model, 1);
+    Assert.assertEquals(1, 
beanDescriptor.getGetters().get("direct").get(model));
+    Assert.assertEquals(1, model.direct);
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> collect Getter/Setter from pojo
> -------------------------------
>
>                 Key: SCB-922
>                 URL: https://issues.apache.org/jira/browse/SCB-922
>             Project: Apache ServiceComb
>          Issue Type: Sub-task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to