Skip to content

Commit 1fd30ae

Browse files
committed
Refactored @convert SPI and created implementation for JSF
1 parent 64b9496 commit 1fd30ae

11 files changed

Lines changed: 232 additions & 156 deletions

File tree

‎config-annotations/src/main/java/org/ocpsoft/rewrite/annotation/Convert.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@
2828
@Retention(RetentionPolicy.RUNTIME)
2929
public @interface Convert
3030
{
31-
Class<?> with();
31+
Class<?> with() default Object.class;
32+
33+
String id() default "";
3234
}

‎config-annotations/src/main/java/org/ocpsoft/rewrite/annotation/handler/ConvertHandler.java‎

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,31 @@ public void process(FieldContext context, Convert annotation, HandlerChain chain
6767
"Found Binding which is not a BindingBuilder but: " + binding.getClass().getSimpleName());
6868
BindingBuilder bindingBuilder = (BindingBuilder) binding;
6969

70-
// add the converter
71-
Class<?> converterType = annotation.with();
72-
LazyConverterAdapter converter = new LazyConverterAdapter(converterType);
70+
Converter<?> converter = null;
71+
72+
// identify converter by the type of the converter
73+
if (annotation.with() != Object.class) {
74+
converter = LazyConverterAdapter.forConverterType(annotation.with());
75+
}
76+
77+
// identify converter by some kind of unique id
78+
else if (annotation.id().length() > 0) {
79+
converter = LazyConverterAdapter.forConverterId(annotation.id());
80+
}
81+
82+
// default: identify converter by the target type
83+
else {
84+
converter = LazyConverterAdapter.forTargetType(field.getType());
85+
}
86+
7387
bindingBuilder.convertedBy(converter);
7488

75-
// some logging
7689
if (log.isTraceEnabled()) {
77-
log.trace("Attached converter adapter for [{}] to field [{}] of class [{}]", new Object[] {
78-
converterType.getSimpleName(), field.getName(), field.getDeclaringClass().getName()
90+
log.trace("Attached converter to field [{}] of class [{}]: ", new Object[] {
91+
field.getName(), field.getDeclaringClass().getName(), converter
7992
});
8093
}
94+
8195
}
8296

8397
// continue with the chain
@@ -92,11 +106,30 @@ public void process(FieldContext context, Convert annotation, HandlerChain chain
92106
private static class LazyConverterAdapter implements Converter<Object>
93107
{
94108

95-
private final Class<?> converterClass;
109+
private final Class<?> targetType;
110+
private final String converterId;
111+
private final Class<?> converterType;
112+
113+
private LazyConverterAdapter(Class<?> targetType, String converterId, Class<?> converterType)
114+
{
115+
this.targetType = targetType;
116+
this.converterId = converterId;
117+
this.converterType = converterType;
118+
}
119+
120+
public static LazyConverterAdapter forConverterType(Class<?> converterType)
121+
{
122+
return new LazyConverterAdapter(null, null, converterType);
123+
}
124+
125+
public static LazyConverterAdapter forConverterId(String id)
126+
{
127+
return new LazyConverterAdapter(null, id, null);
128+
}
96129

97-
public LazyConverterAdapter(Class<?> clazz)
130+
public static LazyConverterAdapter forTargetType(Class<?> targetType)
98131
{
99-
this.converterClass = clazz;
132+
return new LazyConverterAdapter(targetType, null, null);
100133
}
101134

102135
@Override
@@ -110,15 +143,48 @@ public Object convert(Rewrite event, EvaluationContext context, Object value)
110143
Iterator<ConverterProvider> providers = ServiceLoader.load(ConverterProvider.class).iterator();
111144
while (providers.hasNext()) {
112145
ConverterProvider provider = providers.next();
113-
converter = provider.getByType(converterClass);
146+
147+
if (targetType != null) {
148+
converter = provider.getByTargetType(targetType);
149+
}
150+
else if (converterType != null) {
151+
converter = provider.getByConverterType(converterType);
152+
}
153+
else {
154+
converter = provider.getByConverterId(converterId);
155+
}
156+
114157
if (converter != null) {
115158
break;
116159
}
160+
117161
}
118-
Assert.notNull(converter, "Got no result from any ConverterProvider for type: " + converterClass.getName());
162+
Assert.notNull(converter, "Got no converter from any ConverterProvider for: " + this.toString());
119163

120164
return converter.convert(event, context, value);
121165

122166
}
167+
168+
@Override
169+
public String toString()
170+
{
171+
StringBuilder b = new StringBuilder();
172+
b.append(this.getClass().getSimpleName());
173+
b.append(" for ");
174+
if (targetType != null) {
175+
b.append(" target type ");
176+
b.append(targetType.getName());
177+
}
178+
else if (converterType != null) {
179+
b.append(" converter type ");
180+
b.append(converterType.getName());
181+
}
182+
else {
183+
b.append(" id ");
184+
b.append(converterId);
185+
}
186+
return b.toString();
187+
}
188+
123189
}
124190
}

‎config-annotations/src/main/java/org/ocpsoft/rewrite/annotation/spi/ConverterProvider.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,26 @@
1818
import org.ocpsoft.rewrite.bind.Converter;
1919

2020
/**
21-
* SPI for providing integration with other conversion frameworks. Implementations should be able to build an Rewrite
22-
* {@link Converter} from any kind of 3rd party class.
21+
* SPI for providing integration with other conversion frameworks.
2322
*
2423
* @author Christian Kaltepoth
2524
*/
2625
public interface ConverterProvider
2726
{
2827

2928
/**
30-
* Create a Rewrite {@link Converter} from the given converter class. Which types are supported is up to the
31-
* implementation class. A JSF implementation would for example support JSF converters.
29+
* Create a {@link Converter} by some unique ID.
3230
*/
33-
Converter<?> getByType(Class<?> converterClass);
31+
Converter<?> getByConverterId(String id);
32+
33+
/**
34+
* Create a {@link Converter} by a third party converter class.
35+
*/
36+
Converter<?> getByConverterType(Class<?> converterType);
37+
38+
/**
39+
* Create a {@link Converter} by a target type.
40+
*/
41+
Converter<?> getByTargetType(Class<?> targetType);
42+
3443
}

‎config-annotations/src/main/java/org/ocpsoft/rewrite/annotation/spi/DefaultConverterProvider.java‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@
1818
import org.ocpsoft.rewrite.bind.Converter;
1919

2020
/**
21-
* Default implementation of {@link ConverterProvider} that builds {@link Converter} instances by creating an instance
22-
* of the supplied class using the default constructor.
23-
*
24-
* @author Christian Kaltepoth
21+
* Default implementation of {@link ConverterProvider} which allows to use Rewrite {@link Converter} implementations by
22+
* their type.
2523
*/
2624
public class DefaultConverterProvider implements ConverterProvider
2725
{
2826

2927
@Override
30-
public Converter<?> getByType(Class<?> converterClass)
28+
public Converter<?> getByConverterType(Class<?> converterType)
3129
{
32-
33-
if (Converter.class.isAssignableFrom(converterClass)) {
30+
if (Converter.class.isAssignableFrom(converterType)) {
3431

3532
try {
36-
return (Converter<?>) converterClass.newInstance();
33+
return (Converter<?>) converterType.newInstance();
3734
}
3835
catch (InstantiationException e) {
3936
throw new IllegalArgumentException(e);
@@ -48,4 +45,18 @@ public Converter<?> getByType(Class<?> converterClass)
4845

4946
}
5047

48+
@Override
49+
public Converter<?> getByTargetType(Class<?> targetType)
50+
{
51+
// unsupported
52+
return null;
53+
}
54+
55+
@Override
56+
public Converter<?> getByConverterId(String id)
57+
{
58+
// unsupported
59+
return null;
60+
}
61+
5162
}

‎config-annotations/src/test/java/org/ocpsoft/rewrite/annotation/spi/DefaultConverterProviderTest.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ public class DefaultConverterProviderTest
1717
@Test
1818
public void testCreateRewriteConverter()
1919
{
20-
Converter<?> converter = new DefaultConverterProvider().getByType(TestConverter.class);
20+
Converter<?> converter = new DefaultConverterProvider().getByConverterType(TestConverter.class);
2121
assertNotNull(converter);
2222
assertTrue(converter instanceof TestConverter);
2323
}
2424

2525
@Test
2626
public void testUnsupportedType()
2727
{
28-
Converter<?> converter = new DefaultConverterProvider().getByType(BigDecimal.class);
28+
Converter<?> converter = new DefaultConverterProvider().getByConverterType(BigDecimal.class);
29+
assertNull(converter);
30+
}
31+
32+
@Test
33+
public void testSomeIdentifier()
34+
{
35+
Converter<?> converter = new DefaultConverterProvider().getByConverterId("something");
36+
assertNull(converter);
37+
}
38+
39+
@Test
40+
public void testByTargetType()
41+
{
42+
Converter<?> converter = new DefaultConverterProvider().getByTargetType(Integer.class);
2943
assertNull(converter);
3044
}
3145

‎integration-faces-annotations/src/main/java/org/ocpsoft/rewrite/faces/annotation/JSFConverter.java‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎integration-faces-annotations/src/main/java/org/ocpsoft/rewrite/faces/annotation/handler/JSFConverterHandler.java‎

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)