/*===================================================================
 *  Overridden methods
 *  ===================================================================*/
 protected Class findClass(string name)
 // throws ClassNotFoundException
 {
     if ((
             context
             != null) && (
             context.getClassResolver
                 () != null))
     {
         return(context.getClassResolver().classForName(name, context));
     }
     return(base.findClass(name));
 }
Exemple #2
0
        public static Getter generateGetter(OgnlContext context, string code)         // throws OgnlException
        {
            string className = NAME_FACTORY.getNewClassName();

            try
            {
                ClassPool           pool   = (ClassPool)pools [(context.getClassResolver())];
                EnhancedClassLoader loader = (EnhancedClassLoader)loaders [(context.getClassResolver())];
                CtClass             newClass;
                CtClass             ognlContextClass;
                CtClass             objectClass;
                CtClass             stringClass;
                CtMethod            method;
                byte[] byteCode;
                Type   compiledClass;

                if ((pool == null) || (loader == null))
                {
                    ClassLoader classLoader = new ContextClassLoader(typeof(OgnlContext).getClassLoader(), context);

                    pool = ClassPool.getDefault();
                    pool.insertClassPath(new LoaderClassPath(classLoader));
                    pools.Add(context.getClassResolver(), pool);

                    loader = new EnhancedClassLoader(classLoader);
                    loaders.Add(context.getClassResolver(), loader);
                }

                newClass         = pool.makeClass(className);
                ognlContextClass = pool.get(typeof(OgnlContext).Name);
                objectClass      = pool.get(typeof(object).Name);
                stringClass      = pool.get(typeof(string).Name);

                newClass.addInterface(pool.get(typeof(Getter).Name));
                method = new CtMethod(objectClass, "get", new CtClass[] { ognlContextClass, objectClass, stringClass }, newClass);
                method.setBody("{" + code + "}");
                newClass.addMethod(method);
                byteCode      = newClass.toBytecode();
                compiledClass = loader.defineClass(className, byteCode);
                return((Getter)compiledClass.newInstance());
            }
            catch (Exception ex)
            {
                throw new OgnlException("Cannot create class", ex);
            }
        }