Created
January 15, 2011 21:50
-
-
Save charroch/781300 to your computer and use it in GitHub Desktop.
Mixin solutions...
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
package novoda.mixin; | |
public class TestMixin extends Mixable implements WithTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
TestMixin mixin = new TestMixin(); | |
mixin.sayHello("test"); | |
} | |
@Override | |
public void sayHello(String what) { | |
super.call(what); | |
} | |
} | |
--- | |
package novoda.mixin; | |
import java.lang.reflect.Method; | |
public abstract class Mixable { | |
private Method method; | |
@SuppressWarnings({ | |
"rawtypes", "unchecked" | |
}) | |
public void call(Object... parameters) { | |
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2]; | |
for (Class klass : this.getClass().getInterfaces()) { | |
try { | |
klass.getMethod(stackTraceElement.getMethodName(), String.class); | |
Class c = getClass().getClassLoader().loadClass(klass.getCanonicalName() + "Impl"); | |
Method m = c.getMethod(stackTraceElement.getMethodName(), String.class); | |
Object obj = c.newInstance(); | |
m.invoke(obj, parameters); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
--- | |
package novoda.mixin; | |
public interface WithTest { | |
public void sayHello(String what); | |
} | |
---- | |
package novoda.mixin; | |
public class WithTestImpl implements WithTest { | |
public WithTestImpl() { | |
System.out.println("hello world with test"); | |
} | |
@Override | |
public void sayHello(String what) { | |
System.out.println("From with mixin"); | |
System.out.println(what); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment