Search in sources :

Example 21 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class RadioButtonPatcher method initClass.

@InitMethod
static void initClass(CtClass c) throws CannotCompileException, NotFoundException {
    // add overrided RadioButton.setValue method
    CtMethod setValue = CtMethod.make("public void setValue(Boolean value, boolean fireEvents) { super.setValue($1, $2); " + RadioButtonManager.class.getName() + ".onRadioGroupChanged(this, $1, $2); }", c);
    c.addMethod(setValue);
    // add behavior to RadioButton.setName method
    CtMethod setName = c.getMethod("setName", "(Ljava/lang/String;)V");
    setName.insertBefore(RadioButtonManager.class.getName() + ".beforeSetName(this, $1);");
    // Add overrided RadioButton.onLoad method
    CtMethod onLoad = CtMethod.make("protected void onLoad() { super.onLoad(); " + RadioButtonManager.class.getName() + ".onLoad(this); }", c);
    c.addMethod(onLoad);
    // Add overrided RadioButton.onUnLoad method
    CtMethod onUnload = CtMethod.make("protected void onUnLoad() { super.onUnload(); " + RadioButtonManager.class.getName() + ".onUnload(this); }", c);
    c.addMethod(onUnload);
}
Also used : RadioButtonManager(com.googlecode.gwt.test.internal.utils.RadioButtonManager) CtMethod(javassist.CtMethod) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 22 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class DomEventPatcher method initClass.

@InitMethod
static void initClass(CtClass c) throws CannotCompileException, NotFoundException {
    CtMethod fireNativeEvent = c.getMethod("fireNativeEvent", "(Lcom/google/gwt/dom/client/NativeEvent;Lcom/google/gwt/event/shared/HasHandlers;Lcom/google/gwt/dom/client/Element;)V");
    // fire browser event loop first because some command or async callback may modify the DOM
    // structure + fire NativePreviewHandler
    fireNativeEvent.insertBefore(BrowserSimulatorImpl.class.getName() + ".get().fireLoopEnd(); " + DomEventPatcher.class.getName() + ".triggerNativeEvent($1, $3);");
    // fire browser event loop at the end because some command may have been scheduled or RPC call
    // made when the event was dispatched.
    fireNativeEvent.insertAfter(BrowserSimulatorImpl.class.getName() + ".get().fireLoopEnd();");
}
Also used : CtMethod(javassist.CtMethod) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 23 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class WidgetPatcher method initClass.

@InitMethod
static void initClass(CtClass c) throws CannotCompileException, NotFoundException {
    // add behavior to Widget.onAttach method
    CtMethod onAttach = c.getMethod("onAttach", "()V");
    onAttach.insertBefore(GwtFinder.class.getName() + ".onAttach(this);");
    // add behavior to RadioButton.setName method
    CtMethod onDetach = c.getMethod("onDetach", "()V");
    onDetach.insertBefore(GwtFinder.class.getName() + ".onDetach(this);");
}
Also used : CtMethod(javassist.CtMethod) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 24 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class MethodWriter method addSetter.

public static CtMethod addSetter(CtClass target, String field, String name) {
    CtField actualField = null;
    try {
        actualField = target.getField(field);
        log.debugf("Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field);
        CtMethod method = CtNewMethod.setter(name, actualField);
        target.addMethod(method);
        return method;
    } catch (CannotCompileException cce) {
        try {
            // Fall back to create a getter from delegation.
            CtMethod method = CtNewMethod.delegator(CtNewMethod.setter(name, actualField), target);
            target.addMethod(method);
            return method;
        } catch (CannotCompileException ignored) {
            String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
            throw new EnhancementException(msg, cce);
        }
    } catch (NotFoundException nfe) {
        String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
        throw new EnhancementException(msg, nfe);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 25 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class MethodWriter method write.

/* --- */
/**
	 * convenience method that builds a method from a format string. {@see String.format} for more details
	 *
	 * @throws CannotCompileException
	 */
public static CtMethod write(CtClass target, String format, Object... args) throws CannotCompileException {
    String body = String.format(format, args);
    // System.out.printf( "writing method into [%s]:%n%s%n", target.getName(), body );
    log.debugf("writing method into [%s]:%n%s", target.getName(), body);
    CtMethod method = CtNewMethod.make(body, target);
    target.addMethod(method);
    return method;
}
Also used : CtMethod(javassist.CtMethod)

Aggregations

CtMethod (javassist.CtMethod)70 CtClass (javassist.CtClass)42 CannotCompileException (javassist.CannotCompileException)20 NotFoundException (javassist.NotFoundException)20 ClassPool (javassist.ClassPool)16 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2