001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.aopc;
008:
009: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
010:
011: import java.io.File;
012: import java.lang.reflect.Array;
013: import java.lang.reflect.Method;
014: import java.net.MalformedURLException;
015: import java.net.URL;
016: import java.net.URLClassLoader;
017:
018: /**
019: * Test helper for AspectContainer, emulates a ClassLoader hierarchy sys/sub/ sys/sub/a sys/sub/b
020: *
021: * FIXME - rewrite with ASM
022: *
023: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
024: */
025: public class ClassCreator {
026: /**
027: * ClassLoader.defineClass(name, bytes, from, to)
028: */
029: private static Method CLASSLOADER_DEFINECLASS_METHOD;
030:
031: static {
032: try {
033: Object b = Array.newInstance(byte.class, 1);
034: CLASSLOADER_DEFINECLASS_METHOD = ClassLoader.class
035: .getDeclaredMethod("defineClass", new Class[] {
036: String.class, b.getClass(), int.class,
037: int.class });
038: CLASSLOADER_DEFINECLASS_METHOD.setAccessible(true);
039: } catch (Throwable t) {
040: t.printStackTrace();
041: }
042: }
043:
044: public static Object createInstance(String name,
045: Class classPrototype, ClassLoader loader) {
046: try {
047: return createClass(name, classPrototype, loader)
048: .newInstance();
049: } catch (Throwable t) {
050: throw new WrappedRuntimeException(t);
051: }
052: }
053:
054: public static Class createClass(String name, Class classPrototype,
055: ClassLoader loader) {
056: return classPrototype;
057: // try {
058: // ClassPool cp = new ClassPool(null);
059: // cp.appendClassPath(new LoaderClassPath(loader));
060: // CtClass prototype = cp.get(classPrototype.getName());
061: // prototype.setName(name);
062: // return define(prototype.toBytecode(), name, loader);
063: // } catch (Throwable throwable) {
064: // throw new WrappedRuntimeException(throwable);
065: // }
066: }
067:
068: public static void main(String[] a) throws Throwable {
069: ClassLoader myCL = new URLClassLoader(
070: new URL[] { getPathFor(Callable.class
071: .getResource("META-INF/aop.xml")) },
072: ClassLoader.getSystemClassLoader());
073: ClassLoader mySubCLA = new URLClassLoader(
074: new URL[] { getPathFor(Callable.class
075: .getResource("a/META-INF/aop.xml")) }, myCL);
076: Callable ca = (Callable) (createClass("test.aopc.a.Callee",
077: CallablePrototype.class, mySubCLA)).newInstance();
078: ca.methodAround();
079: ca.debug();
080: ClassLoader mySubCLB = new URLClassLoader(new URL[] {}, myCL);
081: Callable cb = (Callable) (createClass("test.aopc.b.Callee",
082: CallablePrototype.class, mySubCLB)).newInstance();
083: cb.methodAround();
084: cb.debug();
085: }
086:
087: public static URL getPathFor(URL definition) {
088: try {
089: System.out.println(definition);
090: System.out.println(definition.getFile());
091: File f = new File(definition.getFile());
092: if (!f.exists()) {
093: System.err.println("<WARN> could not find " + f);
094: }
095: String path = new File(f.getParent()).getParent();
096: File testExists = new File(path);
097: if (!testExists.isDirectory()) {
098: System.err.println("<WARN> could not find " + path);
099: }
100: return new File(path).toURL();
101: } catch (MalformedURLException e) {
102: throw new WrappedRuntimeException(e);
103: }
104: }
105:
106: /**
107: * Helper to define a Class within a specific ClassLoader
108: *
109: * @param b
110: * @param name
111: * @param loader
112: * @return @throws Throwable
113: */
114: public static Class define(byte[] b, String name, ClassLoader loader)
115: throws Throwable {
116: Object k = CLASSLOADER_DEFINECLASS_METHOD.invoke(loader,
117: new Object[] { name, b, new Integer(0),
118: new Integer(b.length) });
119: return (Class) k;
120: }
121: }
|