From c3d533d59d6ee5c72f735c056d112b389adb1a0a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 1 Mar 2012 09:05:28 +0100 Subject: [PATCH] DATAMONGO-411 - Double check type of PersistentEntity for index creation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Spring container does not check nested generic types of the type parameter of ApplicationEvent. As T is parameterized in our case as well (PersistentEntity<…, …>) we can code an event listener against that fully parameterized type but might run into ClassCastExceptions as we might get other implementations handed into the method at runtime. We now do an instanceof check to safely invoke checkForIndexes(…) only in case we get the correct event type. --- .../core/index/MongoPersistentEntityIndexCreator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index ad773e3c0..547a74dde 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; +import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.event.MappingContextEvent; import org.springframework.data.mongodb.MongoDbFactory; @@ -76,7 +77,13 @@ public class MongoPersistentEntityIndexCreator implements */ public void onApplicationEvent( MappingContextEvent, MongoPersistentProperty> event) { - checkForIndexes(event.getPersistentEntity()); + + PersistentEntity entity = event.getPersistentEntity(); + + // Double check type as Spring infrastructure does not consider nested generics + if (entity instanceof MongoPersistentEntity) { + checkForIndexes(event.getPersistentEntity()); + } } protected void checkForIndexes(final MongoPersistentEntity entity) {