/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Utility method to insert callback at the beginning of the body. * * @param callback The callback * * @see CtBehavior#insertBefore(String) */ public static void insertBefore(CtBehavior behavior, Callback callback) throws CannotCompileException { behavior.insertBefore(callback.toString()); }
/** * Utility method to insert callback at the beginning of the body. * * @param callback The callback * * @see CtBehavior#insertBefore(String) */ public static void insertBefore(CtBehavior behavior, Callback callback) throws CannotCompileException { behavior.insertBefore(callback.toString()); }
body += " }"; ctBehavior.insertBefore(body);
field.setModifiers(Modifier.PUBLIC | Modifier.STATIC); cc.addField(field, CtField.Initializer.byNew(type)); insertBefore(fname + ".enter();", false); String src = fname + ".exit();"; insertAfter(src, true);
field.setModifiers(Modifier.PUBLIC | Modifier.STATIC); cc.addField(field, CtField.Initializer.byNew(type)); insertBefore(fname + ".enter();", false); String src = fname + ".exit();"; insertAfter(src, true);
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
/** * Inserts bytecode at the beginning of the body. * * <p>If this object represents a constructor, * the bytecode is inserted before * a constructor in the super class or this class is called. * Therefore, the inserted bytecode is subject to constraints described * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). * For example, it cannot access instance fields or methods although * it may assign a value to an instance field directly declared in this * class. Accessing static fields and methods is allowed. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>. * * @param src the source code representing the inserted bytecode. * It must be a single statement or block. * @see CtConstructor#insertBeforeBody(String) */ public void insertBefore(String src) throws CannotCompileException { insertBefore(src, true); }
private void injectAllocationCallPoint(CtBehavior ctBehavior, short callPointId) throws CannotCompileException { LOGGER.info("Injecting call point to " + ctBehavior.getLongName() + " ..."); ctBehavior.insertBefore(InstrumentationBasedAllocationPathManager.class.getName() + ".pushThreadLocalAllocationCallPoint(" + "(short) " + callPointId + ");"); ctBehavior.insertAfter(InstrumentationBasedAllocationPathManager.class.getName() + ".popThreadLocalAllocationCallPoint(" + "(short) " + callPointId + ");", true); }
protected static void insertAssertedVerificationCodeBefore( @Nonnull CtBehavior method, @Nonnull String verificationCode ) throws CannotCompileException { method.insertBefore( wrapInAssertion(method.getDeclaringClass(), verificationCode ) ); }
public static void insertBefore(CtBehavior ctBehavior, String statement) { try { ctBehavior.insertBefore(statement); } catch (CannotCompileException e) { throw new CheckedExceptionSuppressorException(e.getMessage(), e); } }
/** * process a single method - this means add entry/exit logging if requested. * It is only called for methods with a body. * * @param method * method to work on * @throws NotFoundException * @throws CannotCompileException */ private void doMethod(CtBehavior method) throws NotFoundException, CannotCompileException { String signature = JavassistHelper.getSignature(method); String returnValue = JavassistHelper.returnValue(method); if (addEntryExit) { String messagePattern = "if ({}.{}()) {}.{}(\">> {}\");"; Object[] arg1 = new Object[] { loggerName, levelEnabled, loggerName, level, signature }; String before = MessageFormatter.arrayFormat(messagePattern, arg1).getMessage(); // System.out.println(before); method.insertBefore(before); String messagePattern2 = "if ({}.{}()) {}.{}(\"<< {}{}\");"; Object[] arg2 = new Object[] { loggerName, levelEnabled, loggerName, level, signature, returnValue }; String after = MessageFormatter.arrayFormat(messagePattern2, arg2).getMessage(); // System.out.println(after); method.insertAfter(after); } } }
protected void handle(CtBehavior behavior) { try { if (debug) System.err.println("instrumenting " + behavior.getClass().getName() + "." + behavior.getName()); if (behavior.isEmpty()) return; behavior.addLocalVariable("memoryBefore", CtClass.longType); behavior.insertBefore("memoryBefore = fiji.MemoryProfiler.get();"); behavior.insertAfter("fiji.MemoryProfiler.report(memoryBefore);"); } catch (CannotCompileException e) { if (!e.getMessage().equals("no method body")) e.printStackTrace(); } }
@Override void createBody(CtBehavior behavior) { try { behavior.insertBefore(getBody()); } catch (CannotCompileException e) { throw new org.jboss.reflect.spi.CannotCompileException(e.getMessage()); } }
private void instrument(CtBehavior ctBehavior) throws CannotCompileException { final String longName = ctBehavior.getLongName(); final Integer id = ProfilerManager.register(longName); ctBehavior.insertBefore("ProfilerManager.methodEnter(" + id + ");"); final ProfilerDefinition.Sleeper sleeper = sleepers == null ? null : sleepers.get(longName); if (sleeper != null && (sleeper.min != null || sleeper.max != null)) { final int min = sleeper.min == null ? sleeper.max : sleeper.min; final int max = sleeper.max == null ? sleeper.min : sleeper.max; final String call = min == max ? "ProfilerManager.sleep(" + min + ");" : "ProfilerManager.randomSleep(" + min + "," + max + ");"; ctBehavior.insertAfter(call); } ctBehavior.insertAfter("ProfilerManager.methodExit(" + id + ");"); }
@Override public void processAnnotatedArgument(T annotation, ArgumentAnnotationContext context) throws CannotCompileException, NotFoundException { if (!isActive(context)) return; try { context.getArgumentBehavior().insertBefore( ENSURE_FULLY_QUALIFIED_NAME + createArgumentCheck(annotation, context) + ";"); } catch (CannotCompileException e) { logError(context, context.getArgumentBehavior(), e); throw e; } }