Search in sources :

Example 1 with FragmentClassLoader

use of org.qi4j.runtime.composite.FragmentClassLoader 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)

Example 2 with FragmentClassLoader

use of org.qi4j.runtime.composite.FragmentClassLoader in project qi4j-sdk by Qi4j.

the class AssemblyHelper method getModifierClassLoader.

private FragmentClassLoader getModifierClassLoader(ClassLoader classLoader) {
    FragmentClassLoader cl = modifierClassLoaders.get(classLoader);
    if (cl == null) {
        cl = new FragmentClassLoader(classLoader);
        modifierClassLoaders.put(classLoader, cl);
    }
    return cl;
}
Also used : FragmentClassLoader(org.qi4j.runtime.composite.FragmentClassLoader)

Example 3 with FragmentClassLoader

use of org.qi4j.runtime.composite.FragmentClassLoader in project qi4j-sdk by Qi4j.

the class AssemblyHelper method instantiationClass.

private Class instantiationClass(Class fragmentClass) {
    Class instantiationClass = fragmentClass;
    if (!InvocationHandler.class.isAssignableFrom(fragmentClass)) {
        instantiationClass = instantiationClasses.get(fragmentClass);
        if (instantiationClass == null) {
            try {
                FragmentClassLoader fragmentLoader = getModifierClassLoader(fragmentClass.getClassLoader());
                instantiationClass = fragmentLoader.loadFragmentClass(fragmentClass);
                instantiationClasses.put(fragmentClass, instantiationClass);
            } catch (ClassNotFoundException e) {
                throw new ConstructionException("Could not generate mixin subclass " + fragmentClass.getName(), e);
            } catch (VerifyError e) {
                throw new ConstructionException("Could not generate mixin subclass " + fragmentClass.getName(), e);
            }
        }
    }
    return instantiationClass;
}
Also used : ConstructionException(org.qi4j.api.common.ConstructionException) InvocationHandler(java.lang.reflect.InvocationHandler) FragmentClassLoader(org.qi4j.runtime.composite.FragmentClassLoader)

Aggregations

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