From cf662e2b7e72e9ff15555bc9c0a6ceefc4a4b212 Mon Sep 17 00:00:00 2001 From: Le Stephane Date: Tue, 20 Aug 2019 09:07:08 +0300 Subject: [PATCH] ignore bridge methods in generified use cases --- .../SinglePublicUseCaseMethodCaller.java | 9 ++++- .../GenericMethodInterface.java | 26 +++++++++++++ .../usecaseCalling/GenericMethodUseCase.java | 29 +++++++++++++++ .../SinglePublicUseCaseMethodCallerTest.java | 37 +++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodInterface.java create mode 100644 core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodUseCase.java create mode 100644 core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCallerTest.java diff --git a/core/src/main/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCaller.java b/core/src/main/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCaller.java index 0f4678b..34185db 100644 --- a/core/src/main/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCaller.java +++ b/core/src/main/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCaller.java @@ -30,10 +30,12 @@ import java.lang.reflect.Method; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static com.envimate.messageMate.internal.reflections.ForbiddenUseCaseMethods.NOT_ALLOWED_USECASE_PUBLIC_METHODS; import static com.envimate.messageMate.internal.reflections.ReflectionUtils.getAllPublicMethods; import static com.envimate.messageMate.useCases.useCaseAdapter.methodInvoking.SerializingMethodInvoker.serializingMethodInvoker; +import static java.util.function.Predicate.not; import static lombok.AccessLevel.PRIVATE; /** @@ -59,8 +61,11 @@ public static SinglePublicUseCaseMethodCaller singlePublicUseCaseMethodCa return new SinglePublicUseCaseMethodCaller<>(methodInvoker); } - private static Method locateUseCaseMethod(final Class useCaseClass) { - final List useCaseMethods = getAllPublicMethods(useCaseClass, NOT_ALLOWED_USECASE_PUBLIC_METHODS); + static Method locateUseCaseMethod(final Class useCaseClass) { + final List useCaseMethods = + getAllPublicMethods(useCaseClass, NOT_ALLOWED_USECASE_PUBLIC_METHODS).stream() + .filter(not(Method::isBridge)) + .collect(Collectors.toList()); if (useCaseMethods.size() == 1) { return useCaseMethods.get(0); } else { diff --git a/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodInterface.java b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodInterface.java new file mode 100644 index 0000000..bd99273 --- /dev/null +++ b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodInterface.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 envimate GmbH - https://envimate.com/. + * + * 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 com.envimate.messageMate.useCases.useCaseAdapter.usecaseCalling; + +public interface GenericMethodInterface { + O invoke(I request); +} diff --git a/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodUseCase.java b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodUseCase.java new file mode 100644 index 0000000..19e78f3 --- /dev/null +++ b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/GenericMethodUseCase.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019 envimate GmbH - https://envimate.com/. + * + * 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 com.envimate.messageMate.useCases.useCaseAdapter.usecaseCalling; + +public class GenericMethodUseCase implements GenericMethodInterface { + @Override + public String invoke(final String in) { + return in.replace("in", "out"); + } +} diff --git a/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCallerTest.java b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCallerTest.java new file mode 100644 index 0000000..f557196 --- /dev/null +++ b/core/src/test/java/com/envimate/messageMate/useCases/useCaseAdapter/usecaseCalling/SinglePublicUseCaseMethodCallerTest.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 envimate GmbH - https://envimate.com/. + * + * 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 com.envimate.messageMate.useCases.useCaseAdapter.usecaseCalling; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SinglePublicUseCaseMethodCallerTest { + @Test + void locatesGenericMethods_aka_ignoresBridgeMethods() throws Exception { + final Method actualMethod = SinglePublicUseCaseMethodCaller.locateUseCaseMethod(GenericMethodUseCase.class); + final Method expectedMethod = GenericMethodUseCase.class.getMethod("invoke", String.class); + assertEquals(expectedMethod, actualMethod, "located method"); + } +}