Skip to content

Commit

Permalink
[SCB-1795] when use query object, sdk don't support fluent setter (#1623
Browse files Browse the repository at this point in the history
)

* [SCB-1795] when use query object, sdk don't support fluent setter

* [SCB-1795] when use query object, sdk don't support fluent setter: testNullFieldAndDefaultValue does not support highway

* [SCB-1795] when use query object, sdk don't support fluent setter: modify as review
  • Loading branch information
heyile committed Mar 19, 2020
1 parent 192e4ac commit d0655d7
Show file tree
Hide file tree
Showing 23 changed files with 1,329 additions and 101 deletions.
Expand Up @@ -125,7 +125,10 @@ public static <T> T createLambda(Method instanceMethod, Class<?> functionalIntfC
MethodHandle methodHandle = LOOKUP.unreflect(instanceMethod);

MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
MethodType instanceMethodType = methodHandle.type();

// the return type of fluent setter is object instead of void, but we can assume the return type is void. it doesn't matter
MethodType instanceMethodType = MethodType
.methodType(intfMethod.getReturnType(), methodHandle.type().parameterList());
CallSite callSite = LambdaMetafactory.metafactory(
LOOKUP,
intfMethod.getName(),
Expand Down
Expand Up @@ -42,6 +42,11 @@ public void setF1(int f1) {
this.f1 = f1;
}

public Model fluentSetF1(int f1) {
this.f1 = f1;
return this;
}

public List<Integer> echo(List<Integer> value) {
return value;
}
Expand Down Expand Up @@ -70,13 +75,19 @@ public void createLambda_withInstance() throws Throwable {
public void createGetterSetterByMethod() throws Throwable {
IntGetter<Model> getter = LambdaMetafactoryUtils.createGetter(Model.class.getMethod("getF1"));
IntSetter<Model> setter = LambdaMetafactoryUtils.createSetter(Model.class.getMethod("setF1", int.class));
IntSetter<Model> fluentSetter = LambdaMetafactoryUtils
.createSetter(Model.class.getMethod("fluentSetF1", int.class));
BiFunction<Object, Object, Object> echo = LambdaMetafactoryUtils
.createLambda(Model.class.getMethod("echo", List.class), BiFunction.class);

setter.set(model, 1);
int f1 = getter.get(model);
Assert.assertEquals(1, f1);
Assert.assertThat((List<Integer>) echo.apply(model, Arrays.asList(2)), Matchers.contains(2));

fluentSetter.set(model, 2);
int ff1 = getter.get(model);
Assert.assertEquals(2, ff1);
}

@Test
Expand Down
@@ -0,0 +1,118 @@
/*
* 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.it.schema.objectparams;

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

import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class FluentSetterBeanParamRequest {
private String path;

@QueryParam("query")
private int query;

@HeaderParam("header")
private String header;

@JsonIgnore
private List<FlattenObjectRequest> ignored;

public FluentSetterBeanParamRequest() {
}

public FluentSetterBeanParamRequest(String path, int query, String header) {
this.path = path;
this.query = query;
this.header = header;
}

public String getPath() {
return path;
}

@PathParam("path")
public FluentSetterBeanParamRequest setPath(String path) {
this.path = path;
return this;
}

public int getQuery() {
return query;
}

public FluentSetterBeanParamRequest setQuery(int query) {
this.query = query;
return this;
}

public String getHeader() {
return header;
}

public FluentSetterBeanParamRequest setHeader(String header) {
this.header = header;
return this;
}

public List<FlattenObjectRequest> getIgnored() {
return ignored;
}

public FluentSetterBeanParamRequest setIgnored(
List<FlattenObjectRequest> ignored) {
this.ignored = ignored;
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();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FluentSetterBeanParamRequest that = (FluentSetterBeanParamRequest) o;
return query == that.query &&
Objects.equals(path, that.path) &&
Objects.equals(header, that.header) &&
Objects.equals(ignored, that.ignored);
}

@Override
public int hashCode() {
return Objects.hash(path, query, header, ignored);
}
}

0 comments on commit d0655d7

Please sign in to comment.