forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
package org.springframework.context; | ||
|
||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.ResolvableTypeProvider; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
|
@@ -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; | ||
|
||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
snicoll
Author
Owner
|
||
return this.payload; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
spring-core/src/main/java/org/springframework/core/ResolvableTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
Does this need to be final?