Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
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
68 changes: 68 additions & 0 deletions
68
ui/impl/src/main/java/org/jboss/forge/ui/impl/converter/cdi/ConverterExtension.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,68 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.ui.impl.converter.cdi; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.AnnotatedType; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.enterprise.inject.spi.ProcessAnnotatedType; | ||
|
||
import org.jboss.forge.ui.converter.Converter; | ||
import org.jboss.forge.ui.impl.converter.ConverterRegistryImpl; | ||
|
||
/** | ||
* Registers all the converters into the {@link ConverterRegistryImpl} | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
* | ||
*/ | ||
public class ConverterExtension implements Extension | ||
{ | ||
|
||
@SuppressWarnings("unchecked") | ||
<S, T> void observesConverters(@Observes ProcessAnnotatedType<? extends Converter<S, T>> pat) throws Exception | ||
{ | ||
AnnotatedType<? extends Converter<S, T>> annotatedType = pat.getAnnotatedType(); | ||
Class<?> javaClass = annotatedType.getJavaClass(); | ||
try | ||
{ | ||
Converter<S, T> newInstance = (Converter<S, T>) javaClass.newInstance(); | ||
|
||
for (Method m : javaClass.getMethods()) | ||
{ | ||
// TODO: Ugly hack. It shoulld have a better way to discover the generic type used | ||
if ("convert".equals(m.getName())) | ||
{ | ||
// TODO: Find the best way to create this object | ||
Class<S> sourceClass = (Class<S>) m.getParameterTypes()[0]; | ||
Class<T> targetClass = (Class<T>) m.getReturnType(); | ||
ConverterRegistryImpl.INSTANCE.addConverter(sourceClass, targetClass, newInstance); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.getLogger(getClass().getSimpleName()) | ||
.log(Level.FINE, "Error while instantiating " + javaClass, e); | ||
} | ||
// Always veto the instance | ||
pat.veto(); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
ui/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
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 @@ | ||
org.jboss.forge.ui.impl.converter.cdi.ConverterExtension |
21 changes: 21 additions & 0 deletions
21
ui/impl/src/test/java/org/jboss/forge/ui/impl/converter/MyConverter.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,21 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.ui.impl.converter; | ||
|
||
import org.jboss.forge.ui.converter.Converter; | ||
|
||
public class MyConverter implements Converter<AnotherBean, SimpleBean> | ||
{ | ||
|
||
@Override | ||
public SimpleBean convert(AnotherBean source) throws Exception | ||
{ | ||
return new SimpleBean(source.getValue()); | ||
} | ||
|
||
} |