Search in sources :

Example 1 with CompositeInvoker

use of org.qi4j.api.composite.CompositeInvoker in project qi4j-sdk by Qi4j.

the class ASMTest method createClass.

@Test
public void createClass() throws Exception {
    FragmentClassLoader classLoader = new FragmentClassLoader(SomeMixin.class.getClassLoader());
    Class clazz = classLoader.loadClass(SomeMixin.class.getName() + "_Stub");
    // Class clazz = SomeMixin_Stubx.class;
    final Other other = new Other() {

        public String other() {
            return "other";
        }

        public String foo(String bar, int x) {
            return "bar:" + bar;
        }

        public void bar(double doub, boolean bool, float fl, char ch, int integer, long lg, short sh, byte b, Double doubObj, Object[] objArr, int[] intArr) {
        }

        public void multiEx(String bar) throws Exception1, Exception2 {
        }

        public long unwrapResult() {
            return 0;
        }

        public void generic(List<String> list) {
            list.add("Hello World");
        }
    };
    final Some instance = (Some) clazz.getConstructor().newInstance();
    CompositeInvoker invoker = new CompositeInvoker() {

        public Object invokeComposite(Method method, Object[] args) throws Throwable {
            Method fakeMethod = null;
            try {
                fakeMethod = instance.getClass().getMethod("_" + method.getName(), method.getParameterTypes());
                try {
                    return fakeMethod.invoke(instance, args);
                } catch (InvocationTargetException e) {
                    throw e.getCause();
                }
            } catch (NoSuchMethodException e) {
                try {
                    return method.invoke(other, args);
                } catch (InvocationTargetException ex) {
                    throw ex.getCause();
                }
            }
        }
    };
    clazz.getField("_instance").set(instance, invoker);
    System.out.println(instance.some());
    System.out.println(instance.testConcern());
}
Also used : List(java.util.List) Method(java.lang.reflect.Method) FragmentClassLoader(org.qi4j.runtime.composite.FragmentClassLoader) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompositeInvoker(org.qi4j.api.composite.CompositeInvoker) Test(org.junit.Test) QI256Test(org.qi4j.runtime.composite.QI256Test)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Test (org.junit.Test)1 CompositeInvoker (org.qi4j.api.composite.CompositeInvoker)1 FragmentClassLoader (org.qi4j.runtime.composite.FragmentClassLoader)1 QI256Test (org.qi4j.runtime.composite.QI256Test)1