Skip to content

Commit

Permalink
Introduce ListenerReflectiveProcessor to SqsListener
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejNedic committed Aug 4, 2023
1 parent 2b1a83e commit ec900a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed 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
*
* https://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 io.awspring.cloud.sqs.annotation;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
import org.springframework.core.MethodParameter;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;

/**
* Heavily inspired by Spring Frameworks ControllerMappingReflectiveProcessor.
*
Expand Down Expand Up @@ -56,6 +70,6 @@ protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter re
private Type getEntityType(MethodParameter parameter) {
MethodParameter nestedParameter = parameter.nested();
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ? null
: nestedParameter.getNestedParameterType());
: nestedParameter.getNestedParameterType());
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed 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
*
* https://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 io.awspring.cloud.sqs.annotation;

import io.awspring.cloud.sqs.operations.SqsTemplate;
import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.assertThat;

public class SqsListenerReflectiveProcessorTest {


private final SqsListenerReflectiveProcessor processor = new SqsListenerReflectiveProcessor();

private final ReflectionHints hints = new ReflectionHints();
Expand All @@ -24,24 +34,17 @@ public class SqsListenerReflectiveProcessorTest {
void registerClassAndListenerParameterForReflection() throws NoSuchMethodException {
Method method = MyService.class.getDeclaredMethod("listen", MyService.SampleRecord.class);
processor.registerReflectionHints(hints, method);
assertThat(
hints.typeHints())
.satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType())
.isEqualTo(TypeReference.of(MyService.class)),
typeHint -> {
assertThat(typeHint.getType())
.isEqualTo(TypeReference.of(MyService.SampleRecord.class));
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(MyService.class)), typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(MyService.SampleRecord.class));
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.DECLARED_FIELDS);
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS);
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
hint -> assertThat(hint.getName()).isEqualTo("propertyOne"),
hint -> assertThat(hint.getName()).isEqualTo("propertyTwo"));
},
typeHint -> assertThat(typeHint.getType())
.isEqualTo(TypeReference.of(String.class)));
hint -> assertThat(hint.getName()).isEqualTo("propertyOne"),
hint -> assertThat(hint.getName()).isEqualTo("propertyTwo"));
}, typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
}

@Service
public class MyService {

Expand Down

0 comments on commit ec900a0

Please sign in to comment.