Search in sources :

Example 41 with CtMethod

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

the class Log4j2Plugin method registerConfigurator.

@OnClassLoadEvent(classNameRegexp = "org.apache.logging.log4j.core.LoggerContext")
public static void registerConfigurator(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException {
    // fallback to the old version (<2.3) of Log4j2
    CtMethod m = ctClass.getDeclaredMethod("setConfiguration", new CtClass[] { classPool.get("org.apache.logging.log4j.core.config.Configuration") });
    m.insertAfter(PluginManagerInvoker.buildInitializePlugin(Log4j2Plugin.class));
    m.insertAfter(PluginManagerInvoker.buildCallPluginMethod(Log4j2Plugin.class, "init", "config", "java.lang.Object"));
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 42 with CtMethod

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

the class MojarraPlugin method facesConfigManagerInitialized.

@OnClassLoadEvent(classNameRegexp = "com.sun.faces.config.ConfigManager")
public static void facesConfigManagerInitialized(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod init = ctClass.getDeclaredMethod("initialize");
    init.insertAfter(PluginManagerInvoker.buildInitializePlugin(MojarraPlugin.class));
    LOGGER.debug("com.sun.faces.config.ConfigManager enhanced with plugin initialization.");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 43 with CtMethod

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

the class HaCdiCommons method transformGet1.

/**
 * Transform 1 argument get method :
 *    <code>public <T> T get(Contextual<T> contextual);</code>
 *
 * @param classPool the class pool
 * @param ctClass the ct class
 * @throws CannotCompileException the cannot compile exception
 * @throws NotFoundException the not found exception
 */
public static void transformGet1(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException {
    CtMethod methGet1 = ctClass.getDeclaredMethod("get", new CtClass[] { classPool.get("javax.enterprise.context.spi.Contextual") });
    methGet1.insertAfter(getRegistrationCode());
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 44 with CtMethod

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

the class Hibernate3JPATransformers method proxyHibernatePersistence.

/**
 * Override HibernatePersistence.createContainerEntityManagerFactory() to return EntityManagerFactory proxy object.
 * {@link org.hotswap.agent.plugin.hibernate3.jpa.proxy.EntityManagerFactoryProxy} holds reference to all proxied factories
 * and on refresh command replaces internal factory with fresh instance.
 * <p/>
 * Two variants covered - createContainerEntityManagerFactory and createEntityManagerFactory.
 * <p/>
 * After the entity manager factory and it's proxy are instantiated, plugin init method is invoked.
 *
 * @param clazz the clazz
 * @throws Exception the exception
 */
@OnClassLoadEvent(classNameRegexp = "org.hibernate.ejb.HibernatePersistence")
public static void proxyHibernatePersistence(CtClass clazz) throws Exception {
    LOGGER.debug("Override org.hibernate.ejb.HibernatePersistence#createContainerEntityManagerFactory and createEntityManagerFactory to create a EntityManagerFactoryProxy proxy.");
    CtMethod oldMethod = clazz.getDeclaredMethod("createContainerEntityManagerFactory");
    oldMethod.setName("_createContainerEntityManagerFactory" + clazz.getSimpleName());
    CtMethod newMethod = CtNewMethod.make("public javax.persistence.EntityManagerFactory createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo info, java.util.Map properties) {" + "  return " + Hibernate3JPAHelper.class.getName() + ".createContainerEntityManagerFactoryProxy(" + "      info, properties, _createContainerEntityManagerFactory" + clazz.getSimpleName() + "(info, properties)); " + "}", clazz);
    clazz.addMethod(newMethod);
    oldMethod = clazz.getDeclaredMethod("createEntityManagerFactory");
    oldMethod.setName("_createEntityManagerFactory" + clazz.getSimpleName());
    newMethod = CtNewMethod.make("public javax.persistence.EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, java.util.Map properties) {" + "  return " + Hibernate3JPAHelper.class.getName() + ".createEntityManagerFactoryProxy(" + "      persistenceUnitName, properties, _createEntityManagerFactory" + clazz.getSimpleName() + "(persistenceUnitName, properties)); " + "}", clazz);
    clazz.addMethod(newMethod);
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 45 with CtMethod

use of org.hotswap.agent.javassist.CtMethod 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)

Aggregations

CtMethod (org.hotswap.agent.javassist.CtMethod)48 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)29 CtClass (org.hotswap.agent.javassist.CtClass)18 NotFoundException (org.hotswap.agent.javassist.NotFoundException)15 CtField (org.hotswap.agent.javassist.CtField)13 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)11 CtConstructor (org.hotswap.agent.javassist.CtConstructor)6 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)6 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)4 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Set (java.util.Set)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Command (org.hotswap.agent.command.Command)1 PluginManager (org.hotswap.agent.config.PluginManager)1 FieldAccess (org.hotswap.agent.javassist.expr.FieldAccess)1