forked from spring-projects/spring-kafka
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projectsGH-1549: Auto-Detect Type Mappings
Resolves spring-projects#1549
- Loading branch information
1 parent
82027d3
commit c5aa309
Showing
4 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
spring-kafka/src/main/java/org/springframework/kafka/annotation/TypeMapping.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,48 @@ | ||
/* | ||
* Copyright 2020 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 org.springframework.kafka.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* TODO: Move to spring-messaging? | ||
* | ||
* Classes annotated with this can be automatically populated into various type mappers | ||
* (e.g. Jackson). Several Spring projects, JMS, Kafka, RabbitMQ allow mapping a type id | ||
* to/from a class, for the purpose of decoupling concrete types used by senders and | ||
* receivers. | ||
* | ||
* @author Gary Russell | ||
* @since 2.6 | ||
* | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface TypeMapping { | ||
|
||
/** | ||
* Either a single or multiple type ids to map to/from the annotated class. | ||
* @return the type ids. | ||
*/ | ||
String[] value(); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
spring-kafka/src/main/java/org/springframework/kafka/support/TypeMappingUtils.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,87 @@ | ||
/* | ||
* Copyright 2020 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 org.springframework.kafka.support; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.core.annotation.AnnotationAttributes; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
import org.springframework.kafka.annotation.TypeMapping; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* TODO: Move to spring-messaging? | ||
* | ||
* Utilities for type mappings. | ||
* | ||
* @author Gary Russell | ||
* @since 2.6 | ||
* | ||
*/ | ||
public final class TypeMappingUtils { | ||
|
||
private TypeMappingUtils() { | ||
} | ||
|
||
/** | ||
* Find {@link TypeMapping} annotations on classes in the provided packages and return | ||
* as a map of typeid:class. | ||
* @param context the application context. | ||
* @param packagesToScan the packages to scan. | ||
* @return the mappings. | ||
*/ | ||
public static Map<String, Class<?>> findTypeMappings(ApplicationContext context, String... packagesToScan) { | ||
|
||
Map<String, Class<?>> mappings = new HashMap<>(); | ||
Set<Class<?>> candidates = new HashSet<>(); | ||
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); | ||
scanner.setEnvironment(context.getEnvironment()); | ||
scanner.setResourceLoader(context); | ||
scanner.addIncludeFilter(new AnnotationTypeFilter(TypeMapping.class)); | ||
for (String packageToScan : packagesToScan) { | ||
if (StringUtils.hasText(packageToScan)) { | ||
for (BeanDefinition candidate : scanner.findCandidateComponents(packageToScan)) { | ||
try { | ||
candidates.add(ClassUtils.forName(candidate.getBeanClassName(), context.getClassLoader())); | ||
} | ||
catch (ClassNotFoundException | LinkageError e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} | ||
} | ||
for (Class<?> candidate : candidates) { | ||
AnnotationAttributes annotationAttributes = | ||
AnnotatedElementUtils.findMergedAnnotationAttributes(candidate, TypeMapping.class, false, false); | ||
for (String value : annotationAttributes.getStringArray("value")) { | ||
if (StringUtils.hasText(value)) { | ||
mappings.put(value, candidate); | ||
} | ||
} | ||
} | ||
return mappings; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
spring-kafka/src/test/java/org/springframework/kafka/support/TypeMappingUtilsTests.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,61 @@ | ||
/* | ||
* Copyright 2020 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 org.springframework.kafka.support; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.kafka.annotation.TypeMapping; | ||
|
||
/** | ||
* TODO: Move to spring-messaging? | ||
* | ||
* @author Gary Russell | ||
* @since 2.6 | ||
* | ||
*/ | ||
public class TypeMappingUtilsTests { | ||
|
||
@Test | ||
void testDiscover() { | ||
Map<String, Class<?>> mappings = | ||
TypeMappingUtils.findTypeMappings(new GenericApplicationContext(), getClass().getPackage().getName()); | ||
assertThat(mappings).hasSize(6); // this will be 3 if we move this to s-m | ||
assertThat(mappings.get("fooA")).isEqualTo(Foo.class); | ||
assertThat(mappings.get("fooB")).isEqualTo(Foo.class); | ||
assertThat(mappings.get("barA")).isEqualTo(Bar.class); | ||
} | ||
|
||
@TypeMapping({ "fooA", "fooB" }) | ||
public static class Foo { | ||
|
||
public String foo = "foo"; | ||
|
||
} | ||
|
||
@TypeMapping("barA") | ||
public static class Bar extends Foo { | ||
|
||
public String bar = "bar"; | ||
|
||
} | ||
|
||
} |
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