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());
}
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;
}
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;
}
Aggregations