Skip to content

Commit

Permalink
Add ResolvableTypeProvider
Browse files Browse the repository at this point in the history
Provide a mean to detect the actual ResolvableType based on a instance as
a counter measure to type erasure.

Upgrade the event infrastructure to detect if the event (or the payload)
implements such interface. When this is the case, the return value of
`getResolvableType` is used to validate its generic type against the
method signature of the listener.

Issue: SPR-13069
  • Loading branch information
snicoll committed Jun 3, 2015
1 parent 08c032d commit 0aa4e20
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.context;

import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.util.Assert;

/**
Expand All @@ -28,7 +30,7 @@
* @param <T> the payload type of the event
*/
@SuppressWarnings("serial")
public class PayloadApplicationEvent<T> extends ApplicationEvent {
public class PayloadApplicationEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {

private final T payload;

Expand All @@ -44,11 +46,24 @@ public PayloadApplicationEvent(Object source, T payload) {
this.payload = payload;
}

@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), getPayloadResolvableType());
}

protected ResolvableType getPayloadResolvableType() {
if (this.payload instanceof ResolvableTypeProvider) {
return ((ResolvableTypeProvider) this.payload).getResolvableType();
}
else {
return ResolvableType.forClass(this.payload.getClass());
}
}

/**
* Return the payload of the event.
*/
public T getPayload() {
public final T getPayload() {

This comment has been minimized.

Copy link
@philwebb

philwebb Jun 8, 2015

Collaborator

Does this need to be final?

This comment has been minimized.

Copy link
@snicoll

snicoll Jun 8, 2015

Author Owner

maybe not but then I shouldn't call this.payload there either then.

return this.payload;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ public void processEvent(ApplicationEvent event) {
protected Object[] resolveArguments(ApplicationEvent event) {
if (!ApplicationEvent.class.isAssignableFrom(this.declaredEventType.getRawClass())
&& event instanceof PayloadApplicationEvent) {
@SuppressWarnings("rawtypes")
Object payload = ((PayloadApplicationEvent) event).getPayload();
if (this.declaredEventType.isAssignableFrom(ResolvableType.forClass(payload.getClass()))) {
return new Object[] {payload};
PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
ResolvableType payloadType = payloadEvent.getResolvableType()
.as(PayloadApplicationEvent.class).getGeneric(0);
if (this.declaredEventType.isAssignableFrom(payloadType)) {
return new Object[] {payloadEvent.getPayload()};
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.util.ErrorHandler;

/**
Expand Down Expand Up @@ -139,7 +140,12 @@ public void run() {
}

private ResolvableType resolveDefaultEventType(ApplicationEvent event) {
return ResolvableType.forType(event.getClass());
if (event instanceof ResolvableTypeProvider) {
return ((ResolvableTypeProvider) event).getResolvableType();
}
else {
return ResolvableType.forType(event.getClass());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;

/**
* @author Stephane Nicoll
Expand Down Expand Up @@ -53,6 +54,23 @@ public T getPayload() {

}

protected static class SmartGenericTestEvent<T>
extends GenericTestEvent<T> implements ResolvableTypeProvider {

private final ResolvableType resolvableType;

public SmartGenericTestEvent(Object source, T payload) {
super(source, payload);
this.resolvableType = ResolvableType.forClassWithGenerics(
getClass(), payload.getClass());
}

@Override
public ResolvableType getResolvableType() {
return this.resolvableType;
}
}

protected static class StringEvent extends GenericTestEvent<String> {

public StringEvent(Object source, String payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void multicastGenericEventWrongType() {
getGenericApplicationEventType("longEvent"));
}

@Test // Unfortunate - this should work as well
@Test
public void multicastGenericEventWildcardSubType() {
multicastEvent(false, StringEventListener.class, createGenericTestEvent("test"),
getGenericApplicationEventType("wildcardEvent"));
Expand All @@ -91,6 +91,16 @@ public void multicastConcreteWrongTypeGenericListener() {
multicastEvent(false, StringEventListener.class, new LongEvent(this, 123L), null);
}

@Test
public void multicastSmartGenericTypeGenericListener() {
multicastEvent(true, StringEventListener.class, new SmartGenericTestEvent<>(this, "test"), null);
}

@Test
public void multicastSmartGenericWrongTypeGenericListener() {
multicastEvent(false, StringEventListener.class, new SmartGenericTestEvent<>(this, 123L), null);
}

private void multicastEvent(boolean match, Class<?> listenerType,
ApplicationEvent event, ResolvableType eventType) {
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.ApplicationEvent;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.core.annotation.Order;
import org.springframework.util.ReflectionUtils;

Expand Down Expand Up @@ -155,6 +156,42 @@ public void invokeListener() {
verify(this.sampleEvents, times(1)).handleGenericString(event);
}

@Test
public void invokeListenerWithGenericEvent() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
"handleGenericString", GenericTestEvent.class);
GenericTestEvent<String> event = new SmartGenericTestEvent<>(this, "test");
invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleGenericString(event);
}

@Test
public void invokeListenerWithGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
"handleGenericStringPayload", EntityWrapper.class);
EntityWrapper<String> payload = new EntityWrapper<>("test");
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload);
}

@Test
public void invokeListenerWithWrongGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
"handleGenericStringPayload", EntityWrapper.class);
EntityWrapper<Integer> payload = new EntityWrapper<>(123);
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(0)).handleGenericStringPayload(any());
}

@Test
public void invokeListenerWithAnyGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
"handleGenericAnyPayload", EntityWrapper.class);
EntityWrapper<String> payload = new EntityWrapper<>("test");
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload);
}

@Test
public void invokeListenerRuntimeException() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
Expand Down Expand Up @@ -284,6 +321,16 @@ public void handleGenericString(GenericTestEvent<String> event) {
public void handleString(String payload) {
}

@EventListener
public void handleGenericStringPayload(EntityWrapper<String> event) {

}

@EventListener
public void handleGenericAnyPayload(EntityWrapper<?> event) {

}

@EventListener
public void tooManyParameters(String event, String whatIsThis) {
}
Expand Down Expand Up @@ -313,6 +360,19 @@ interface SimpleService {

}

private static class EntityWrapper<T> implements ResolvableTypeProvider {
private final T entity;

public EntityWrapper(T entity) {
this.entity = entity;
}

@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), this.entity.getClass());
}
}

static class InvalidProxyTestBean implements SimpleService {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void genericListenerStrictTypeAndResolvableType() {
supportsEventType(true, StringEventListener.class, eventType);
}

@Test // or if the event provides its precise type
public void genericListenerStrictTypeAndResolvableTypeProvider() {
ResolvableType eventType = new SmartGenericTestEvent<>(this, "foo").getResolvableType();
supportsEventType(true, StringEventListener.class, eventType);
}

@Test // Demonstrates it works if we actually use the subtype
public void genericListenerStrictTypeEventSubType() {
StringEvent stringEvent = new StringEvent(this, "test");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2002-2015 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
*
* 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.springframework.core;

/**
* Any object can implement this interface to provide its actual{@link ResolvableType}.
* <p>
* Such information is very useful when figuring out if the instance matches a generic
* signature as Java does not convey the signature at runtime.
* <p>
* Users of this interface should be careful in complex hierarchy scenarios, especially
* when the generic type signature of the class changes in sub-classes.
*
* @author Stephane Nicoll
* @since 4.2
*/
public interface ResolvableTypeProvider {

/**
* Return the {@link ResolvableType} describing this instance.
*/
ResolvableType getResolvableType();

}

0 comments on commit 0aa4e20

Please sign in to comment.