4 Replies Latest reply on Feb 12, 2020 10:01 AM by Jaikiran Pai

    Is "instanceof" supported in conditions?

    Jaikiran Pai Master

      Consider this trivial Java code:

       

      public class Foo {
          public static void main(final String[] args) throws Exception {
              printMessage("Hello");
          }
      
          private static void printMessage(final String msg) {
              System.out.println(msg);
          }
      }

       

      and a simple Byteman rule:

       

      RULE instanceof test

      CLASS Foo

      METHOD printMessage

      AT ENTRY

      IF $1 instanceof java.lang.String

      DO System.out.println("Hello World from Byteman");

      ENDRULE

       

      Running Byteman against the code, results in:

       

      /tmp/byteman.btm line 5 : invalid expression
          at org.jboss.byteman.rule.Rule.(Rule.java:233)
          at org.jboss.byteman.rule.Rule.create(Rule.java:389)
          at org.jboss.byteman.agent.TransformContext.parseRule(TransformContext.java:178)
          at org.jboss.byteman.agent.TransformContext.transform(TransformContext.java:88)
          at org.jboss.byteman.agent.Transformer.transform(Transformer.java:757)
          at org.jboss.byteman.agent.Transformer.tryTransform(Transformer.java:824)
          at org.jboss.byteman.agent.Transformer.tryTransform(Transformer.java:796)
          at org.jboss.byteman.agent.Transformer.transform(Transformer.java:264)
          at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
          at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
          at java.lang.ClassLoader.defineClass1(Native Method)
          at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
          at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
          at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
          at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
          at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
          at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
          at java.security.AccessController.doPrivileged(Native Method)
          at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
          at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
          at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

      (line 5 is the line where the IF instanceof check is there).

       

      Is it intentional that instanceof isn't supported?

       

      I have a workaround to get past it for now:

       

      RULE instanceof test

      CLASS Foo

      METHOD printMessage

      AT ENTRY

      IF (java.lang.String.class).isInstance($1)

      DO System.out.println("Hello World from Byteman");

      ENDRULE