Created
December 20, 2012 15:22
-
-
Save anonymous/4345897 to your computer and use it in GitHub Desktop.
Simple use case to show how an InvocationHandler (see https://issues.apache.org/jira/browse/DELTASPIKE-113) would be useful to support mixins for CDI beans.
This file contains hidden or 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
| @InvocationHandlerBinding | |
| public @interface Composed {} | |
| /** | |
| * MyComponent will be implemented using three mixins | |
| */ | |
| @Composed | |
| public interface MyComponent implements Titled, Comparable2 {} | |
| public interface Titled { | |
| String getTitle(); | |
| } | |
| public interface Comparable2 extends Comparable { | |
| boolean isLessThan(Comparable object); | |
| boolean isGreaterThan(Comparable object); | |
| } | |
| @Decorator | |
| public abstract class TitledMixin { | |
| private String title; | |
| public String getTitle() { | |
| return title; | |
| } | |
| } | |
| @Decorator | |
| public abstract class ComparableMixin implements Comparable2 { | |
| @Inject @Delegate | |
| private Comparable comparable; | |
| public boolean isLessThan(Comparable object) { | |
| return comparable.compareTo(object) < 0; | |
| } | |
| public boolean isGreaterThan(Comparable object) { | |
| return comparable.compareTo(object) > 0; | |
| } | |
| } | |
| @Decorator | |
| public abstract class TitledComparableMixin implements Titled, Comparable { | |
| @Inject @Delegate | |
| private Titled titled; | |
| public int compareTo(Object object) { | |
| return titled.getTitle().compareTo(((Titled)object).getTitle()); | |
| } | |
| } | |
| @Composed @InvocationHandler | |
| public ComposedInvocationHandler implements InvocationHandler { | |
| public Object invoke(Object proxy, Method method, Object[] args) { | |
| LOG.warn("No mixin found for method " + method); | |
| return null; //Check, if the return type is a simple type and return the correct default value in that case. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment