Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8168623

Byte array that is presented to a Java agent does not preserve parameter names and modifiers.

XMLWordPrintable

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      When implementing a Java agent, the "transform" method of the java.lang.instrument.Instrumentation interface provides a byte array containing the Java classes original byte code. Upon retransforming such a class, the parameter names are not preserved.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Implement a Java agent and add the following transformer:

      class NoOpTransformer implements java.lang.instrument.Transformer {
          @Override
          public byte[] transform(ClassLoader loader,
                                  String className,
                                  Class<?> classBeingRedefined,
                                  ProtectionDomain protectionDomain,
                                  byte[] classfileBuffer) throws IllegalClassFormatException {
          return classFileBuffer.clone(); // No-op transformation, not null to make the supplied byte code effective
        }
      }

      Add the following class, compiled with the -parameters flag:

      class Foo {
        void bar(String x) {}
      }

      Run the following code within premain:

      public static void premain(String arg, Instrumentation inst) throwsException {
        System.out.println(Foo.class.getDeclaredMethod("bar", String.class).getParameters[0].getName)); // x
        inst.addClassFileTransformer(new NoOpTransformer(), true);
        inst.retransformClasses(Foo.class);
        System.out.println(Foo.class.getDeclaredMethod("bar", String.class).getParameters[0].getName)); // arg0
      }

      The first println gives "x" as this is the preserved parameter name. The second println gives "arg0" as the parameter name is not included in the byte array that is presented to the class file transformer.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The parameter names should be preserved in the class file.
      ACTUAL -
      The parameter names are not preserved in the class file.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      See the steps for reproduction which contains code snippets. The code should be bundled as a jar file with a manifest pointing to the starting point as a Agent-Premain class. It can then be added to any Java program.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Reading the class file from the file system (via the class loader) instead. This way, the work of other agents is however not retained.

            amenkov Alex Menkov
            webbuggrp Webbug Group
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: