Search in sources :

Example 16 with CtConstructor

use of org.hotswap.agent.javassist.CtConstructor in project HotswapAgent by HotswapProjects.

the class Hibernate3JPATransformers method beanMetaDataManagerRegisterVariable.

/**
 * Bean meta data manager register variable.
 *
 * @param ctClass the ct class
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.hibernate.validator.internal.metadata.BeanMetaDataManager")
public static void beanMetaDataManagerRegisterVariable(CtClass ctClass) throws CannotCompileException {
    StringBuilder src = new StringBuilder("{");
    src.append(PluginManagerInvoker.buildInitializePlugin(Hibernate3JPAPlugin.class));
    src.append(PluginManagerInvoker.buildCallPluginMethod(Hibernate3JPAPlugin.class, "registerBeanMetaDataManager", "this", "java.lang.Object"));
    src.append("}");
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(src.toString());
    }
    ctClass.addMethod(CtNewMethod.make("public void __resetCache() {" + "   this.beanMetaDataCache.clear(); " + "}", ctClass));
    LOGGER.debug("org.hibernate.validator.internal.metadata.BeanMetaDataManager - added method __resetCache().");
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 17 with CtConstructor

use of org.hotswap.agent.javassist.CtConstructor in project HotswapAgent by HotswapProjects.

the class Hibernate3Transformers method proxyHibernatePersistence.

/**
 * If org.hibernate.ejb.HibernatePersistence is loaded then we live in a JPA
 * environment. Disable the Hibernate3Plugin reload command
 *
 * @param clazz
 *            the clazz
 * @throws Exception
 *             the exception
 */
@OnClassLoadEvent(classNameRegexp = "org.hibernate.ejb.HibernatePersistence")
public static void proxyHibernatePersistence(CtClass clazz) throws Exception {
    CtConstructor con = clazz.getDeclaredConstructor(new CtClass[] {});
    LOGGER.debug("org.hibernate.ejb.HibernatePersistence.<init>");
    // 
    con.insertAfter(// 
    "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
    PluginManagerInvoker.buildInitializePlugin(Hibernate3Plugin.class, "$$cl") + // 
    PluginManagerInvoker.buildCallPluginMethod("$$cl", Hibernate3Plugin.class, "disable"));
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 18 with CtConstructor

use of org.hotswap.agent.javassist.CtConstructor in project HotswapAgent by HotswapProjects.

the class Hibernate3Transformers method proxySessionFactory.

/**
 * Patch org.hibernate.cfg.Configuration with ReInitializable features. When
 * java8+ is supprted, then make methods default in ReInitializable
 *
 * @param classLoader
 *            the class loader
 * @param classPool
 *            the class pool
 * @param clazz
 *            the clazz
 * @throws Exception
 *             the exception
 */
@OnClassLoadEvent(classNameRegexp = "org.hibernate.cfg.Configuration")
public static void proxySessionFactory(ClassLoader classLoader, ClassPool classPool, CtClass clazz) throws Exception {
    LOGGER.debug("Adding interface o.h.a.p.h.s.p.ReInitializable to org.hibernate.cfg.Configuration.");
    clazz.addInterface(classPool.get("org.hotswap.agent.plugin.hibernate3.session.proxy.ReInitializable"));
    CtField field = CtField.make("private org.hotswap.agent.plugin.hibernate3.session.proxy.OverrideConfig $$override  = new org.hotswap.agent.plugin.hibernate3.session.proxy.OverrideConfig();", clazz);
    clazz.addField(field);
    LOGGER.debug("Patching org.hibernate.cfg.Configuration#buildSessionFactory to create a SessionFactoryProxy proxy.");
    CtMethod oldMethod = clazz.getDeclaredMethod("buildSessionFactory");
    oldMethod.setName("_buildSessionFactory");
    CtMethod newMethod = // 
    CtNewMethod.make(// 
    "public org.hibernate.SessionFactory buildSessionFactory() throws org.hibernate.HibernateException {" + "  return " + // 
    SessionFactoryProxy.class.getName() + // 
    "       .getWrapper(this)" + // 
    "       .proxy(_buildSessionFactory()); " + "}", clazz);
    clazz.addMethod(newMethod);
    LOGGER.debug("Adding org.hibernate.cfg.Configuration.reInitialize() method");
    CtMethod reInitMethod = // 
    CtNewMethod.make(// 
    "public void reInitialize(){" + // 
    "  this.settingsFactory = new org.hibernate.cfg.SettingsFactory();" + // 
    "  this.reset();" + "}", clazz);
    clazz.addMethod(reInitMethod);
    LOGGER.debug("Adding org.hibernate.cfg.Configuration.getOverrideConfig() method");
    CtMethod internalPropsMethod = // 
    CtNewMethod.make(// 
    "public org.hotswap.agent.plugin.hibernate3.session.proxy.OverrideConfig getOverrideConfig(){" + // 
    "  return $$override;" + "}", clazz);
    clazz.addMethod(internalPropsMethod);
    CtConstructor con = clazz.getDeclaredConstructor(new CtClass[] {});
    LOGGER.debug("Patching org.hibernate.cfg.Configuration.<init>");
    // 
    con.insertAfter(// 
    "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
    PluginManagerInvoker.buildInitializePlugin(Hibernate3Plugin.class, "$$cl") + // 
    "java.lang.String $$version = org.hibernate.Version.getVersionString();" + // 
    PluginManagerInvoker.buildCallPluginMethod("$$cl", Hibernate3Plugin.class, "setVersion", "$$version", "java.lang.String"));
    ProxyUtil.addMethod(classLoader, classPool, clazz, "void", "hotSwap", null);
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "setProperty", new String[] { "java.lang.String", "java.lang.String" });
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "configure", new String[] { "java.lang.String" });
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "configure", new String[] { "java.net.URL" });
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "configure", new String[] { "java.io.File" });
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "configure", new String[] { "org.w3c.dom.Document" });
    ProxyUtil.addMethod(classLoader, classPool, clazz, "org.hibernate.cfg.Configuration", "configure", null);
    LOGGER.info("Hibernate3Plugin, patched org.hibernate.cfg.Configuration");
}
Also used : SessionFactoryProxy(org.hotswap.agent.plugin.hibernate3.session.proxy.SessionFactoryProxy) CtField(org.hotswap.agent.javassist.CtField) CtMethod(org.hotswap.agent.javassist.CtMethod) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 19 with CtConstructor

use of org.hotswap.agent.javassist.CtConstructor in project HotswapAgent by HotswapProjects.

the class ProxyUtil method ensureProxyable.

/**
 * Ensure proxyable.
 *
 * @param clazz
 *            the clazz
 * @throws CannotCompileException
 *             the cannot compile exception
 */
public static void ensureProxyable(CtClass clazz) throws CannotCompileException {
    int flags = clazz.getClassFile().getAccessFlags();
    flags = AccessFlag.setPublic(flags);
    flags = AccessFlag.clear(flags, AccessFlag.FINAL);
    clazz.getClassFile().setAccessFlags(flags);
    try {
        CtConstructor ct = clazz.getDeclaredConstructor(new CtClass[] {});
        if (Modifier.isPrivate(ct.getModifiers())) {
            ct.setModifiers(AccessFlag.setProtected(ct.getModifiers()));
        }
    } catch (NotFoundException ex) {
        CtConstructor c = CtNewConstructor.make("protected " + clazz.getSimpleName() + "() {}", clazz);
        clazz.addConstructor(c);
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

Example 20 with CtConstructor

use of org.hotswap.agent.javassist.CtConstructor in project HotswapAgent by HotswapProjects.

the class ELResolverPlugin method beanELResolverRegisterVariable.

/**
 * Hook on BeanELResolver class and for each instance:
 * - ensure plugin is initialized
 * - register instances using registerBeanELResolver() method
 */
@OnClassLoadEvent(classNameRegexp = "javax.el.BeanELResolver")
public static void beanELResolverRegisterVariable(CtClass ctClass) throws CannotCompileException {
    String initPlugin = PluginManagerInvoker.buildInitializePlugin(ELResolverPlugin.class);
    String registerThis = PluginManagerInvoker.buildCallPluginMethod(ELResolverPlugin.class, "registerBeanELResolver", "this", "java.lang.Object");
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(initPlugin);
        constructor.insertAfter(registerThis);
    }
    boolean found = false;
    if (checkJuelEL(ctClass)) {
        found = true;
        LOGGER.debug("JuelEL - javax.el.BeanELResolver - method added " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader). ");
    } else if (checkApacheEL(ctClass)) {
        found = true;
        LOGGER.debug("ApacheEL - javax.el.BeanELResolver - method added " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader). ");
    } else if (checkJBoss_3_0_EL(ctClass)) {
        found = true;
        LOGGER.debug("JBossEL 3.0 - javax.el.BeanELResolver - method added " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader). ");
    }
    if (!found) {
        LOGGER.warning("Unable to add javax.el.BeanELResolver." + PURGE_CLASS_CACHE_METHOD_NAME + "() method. Purging will not be available.");
    }
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

CtConstructor (org.hotswap.agent.javassist.CtConstructor)21 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)16 CtClass (org.hotswap.agent.javassist.CtClass)8 CtMethod (org.hotswap.agent.javassist.CtMethod)6 NotFoundException (org.hotswap.agent.javassist.NotFoundException)5 WeldPlugin (org.hotswap.agent.plugin.weld.WeldPlugin)4 CtField (org.hotswap.agent.javassist.CtField)3 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)2 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Command (org.hotswap.agent.command.Command)1 PluginManager (org.hotswap.agent.config.PluginManager)1 CtBehavior (org.hotswap.agent.javassist.CtBehavior)1 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)1 FieldAccess (org.hotswap.agent.javassist.expr.FieldAccess)1 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)1 DeltaSpikePlugin (org.hotswap.agent.plugin.deltaspike.DeltaSpikePlugin)1