Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.apache.servicecomb.foundation.common.utils.bean.ShortGetter;
import org.apache.servicecomb.foundation.common.utils.bean.ShortSetter;

import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;

public final class LambdaMetafactoryUtils {
private static final Lookup LOOKUP = MethodHandles.lookup();

Expand Down Expand Up @@ -148,6 +150,19 @@ public static <T> T createGetter(Method getMethod) {
return createLambda(getMethod, getterCls);
}

@SuppressWarnings("unchecked")
public static Getter<Object, Object> createObjectGetter(Method getMethod) {
return createLambda(getMethod, Getter.class);
}

public static Getter<Object, Object> createObjectGetter(BeanPropertyDefinition propertyDefinition) {
if (propertyDefinition.hasGetter()) {
return createObjectGetter(propertyDefinition.getGetter().getAnnotated());
}

return createGetter(propertyDefinition.getField().getAnnotated());
}

// slower than reflect directly
@SuppressWarnings("unchecked")
public static <C, F> Getter<C, F> createGetter(Field field) {
Expand Down Expand Up @@ -177,6 +192,47 @@ public static <T> T createSetter(Method setMethod) {
return createLambda(setMethod, setterCls);
}

// just for avoid java 9~11 bug: https://bugs.openjdk.java.net/browse/JDK-8174983
// otherwise can be replaced by: createLambda(setMethod, Setter.class)
@SuppressWarnings("unchecked")
public static Setter<Object, Object> createObjectSetter(Method setMethod) {
Object setter = createSetter(setMethod);
if (setter instanceof BoolSetter) {
return (Instance, value) -> ((BoolSetter) setter).set(Instance, (boolean) value);
}
if (setter instanceof ByteSetter) {
return (Instance, value) -> ((ByteSetter) setter).set(Instance, (byte) value);
}
if (setter instanceof CharSetter) {
return (Instance, value) -> ((CharSetter) setter).set(Instance, (char) value);
}
if (setter instanceof DoubleSetter) {
return (Instance, value) -> ((DoubleSetter) setter).set(Instance, (double) value);
}
if (setter instanceof FloatSetter) {
return (Instance, value) -> ((FloatSetter) setter).set(Instance, (float) value);
}
if (setter instanceof IntSetter) {
return (Instance, value) -> ((IntSetter) setter).set(Instance, (int) value);
}
if (setter instanceof LongSetter) {
return (Instance, value) -> ((LongSetter) setter).set(Instance, (long) value);
}
if (setter instanceof ShortSetter) {
return (Instance, value) -> ((ShortSetter) setter).set(Instance, (short) value);
}

return (Setter<Object, Object>) setter;
}

public static Setter<Object, Object> createObjectSetter(BeanPropertyDefinition propertyDefinition) {
if (propertyDefinition.hasSetter()) {
return createObjectSetter(propertyDefinition.getSetter().getAnnotated());
}

return createSetter(propertyDefinition.getField().getAnnotated());
}

// slower than reflect directly
public static <C, F> Setter<C, F> createSetter(Field field) {
checkAccess(field);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.servicecomb.foundation.common.utils;

import static org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils.createObjectGetter;
import static org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils.createObjectSetter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

Expand All @@ -27,12 +29,18 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.apache.servicecomb.foundation.common.utils.bean.Getter;
import org.apache.servicecomb.foundation.common.utils.bean.IntGetter;
import org.apache.servicecomb.foundation.common.utils.bean.IntSetter;
import org.apache.servicecomb.foundation.common.utils.bean.Setter;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;

public class TestLambdaMetafactoryUtils {
public static class Model {
public int f1;
Expand Down Expand Up @@ -107,4 +115,52 @@ public void should_failed_when_createGetterSetterByField_and_field_is_not_public
.hasMessage(
"Can not access field, a public field or accessor is required.Declaring class is org.apache.servicecomb.foundation.common.utils.TestLambdaMetafactoryUtils$Model, field is f2");
}

public static class Base<T> {
private T base;

public T getBase() {
return base;
}

public Base<T> setBase(T base) {
this.base = base;
return this;
}
}

public static class Child extends Base<Integer> {
private int child;

public int getChild() {
return child;
}

public Child setChild(int child) {
this.child = child;
return this;
}
}

@Test
public void should_support_primitive_type() {
Child child = new Child();

ObjectMapper mapper = JsonUtils.OBJ_MAPPER;
BeanDescription beanDescription = mapper.getSerializationConfig().introspect(mapper.constructType(Child.class));
List<BeanPropertyDefinition> properties = beanDescription.findProperties();
assertThat(properties).hasSize(2);

for (int idx = 0; idx < properties.size(); idx++) {
BeanPropertyDefinition property = properties.get(idx);

Setter<Object, Object> setter = createObjectSetter(property.getSetter().getAnnotated());
setter.set(child, idx);

Getter<Object, Object> getter = createObjectGetter(property.getGetter().getAnnotated());
Object value = getter.get(child);

assertThat(value).isEqualTo(idx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.servicecomb.config.inject;

import static org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils.createObjectSetter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,8 +26,7 @@
import org.apache.servicecomb.config.priority.PriorityProperty;
import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils;
import org.apache.servicecomb.foundation.common.utils.bean.SetterWrapper;
import org.apache.servicecomb.foundation.common.utils.bean.Setter;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JavaType;
Expand Down Expand Up @@ -98,10 +99,7 @@ private void doCreate() {
continue;
}

SetterWrapper setter = propertyDefinition.getSetter() == null ?
new SetterWrapper(LambdaMetafactoryUtils.createSetter(propertyDefinition.getField().getAnnotated())) :
new SetterWrapper(LambdaMetafactoryUtils.createSetter(propertyDefinition.getSetter().getAnnotated()));

Setter<Object, Object> setter = createObjectSetter(propertyDefinition);
PriorityProperty<?> priorityProperty = createPriorityProperty(propertyDefinition.getField().getAnnotated());
priorityProperty.setCallback((value, target) -> setter.set(target, value), instance);
priorityProperties.add(priorityProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.servicecomb.it.schema.objectparams;

import java.util.List;
import java.util.Objects;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam;
Expand All @@ -35,16 +34,24 @@ public class BeanParamRequest {
@HeaderParam("header")
private String header;

@QueryParam("query_array")
private String[] queryArray;

@QueryParam("query_list")
private List<String> queryList;

@JsonIgnore
private List<FlattenObjectRequest> ignored;

public BeanParamRequest() {
}

public BeanParamRequest(String path, int query, String header) {
public BeanParamRequest(String path, int query, String header, String[] queryArray, List<String> queryList) {
this.path = path;
this.query = query;
this.header = header;
this.queryArray = queryArray;
this.queryList = queryList;
}

public String getPath() {
Expand Down Expand Up @@ -72,42 +79,29 @@ public void setHeader(String header) {
this.header = header;
}

public List<FlattenObjectRequest> getIgnored() {
return ignored;
public String[] getQueryArray() {
return queryArray;
}

public void setIgnored(List<FlattenObjectRequest> ignored) {
this.ignored = ignored;
public BeanParamRequest setQueryArray(String[] queryArray) {
this.queryArray = queryArray;
return this;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("BeanParamRequest{");
sb.append("path='").append(path).append('\'');
sb.append(", query=").append(query);
sb.append(", header='").append(header).append('\'');
sb.append(", ignored=").append(ignored);
sb.append('}');
return sb.toString();
public List<String> getQueryList() {
return queryList;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BeanParamRequest that = (BeanParamRequest) o;
return query == that.query &&
Objects.equals(path, that.path) &&
Objects.equals(header, that.header) &&
Objects.equals(ignored, that.ignored);
public BeanParamRequest setQueryList(List<String> queryList) {
this.queryList = queryList;
return this;
}

@Override
public int hashCode() {
return Objects.hash(path, query, header, ignored);
public List<FlattenObjectRequest> getIgnored() {
return ignored;
}

public void setIgnored(List<FlattenObjectRequest> ignored) {
this.ignored = ignored;
}
}
Loading