1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.lang.instrument.*;
  26 
  27 public class RedefineSubclassWithTwoInterfacesApp {
  28     public static void main(String args[]) throws Exception {
  29         System.out.println("Hello from RedefineSubclassWithTwoInterfacesApp!");
  30 
  31         new RedefineSubclassWithTwoInterfacesApp().doTest();
  32 
  33         System.exit(0);
  34     }
  35 
  36     private void doTest() throws Exception {
  37         RedefineSubclassWithTwoInterfacesImpl impl
  38             = new RedefineSubclassWithTwoInterfacesImpl();
  39         RedefineSubclassWithTwoInterfacesRemote remote
  40           = new RedefineSubclassWithTwoInterfacesRemote(impl, impl);
  41         String mesg;
  42 
  43         // make an echo() call before any redefinitions:
  44         mesg = remote.echo("test message #0");
  45         System.out.println("RedefineSubclassWithTwoInterfacesApp: mesg='"
  46             + mesg + "' before any redefines");
  47 
  48 
  49         // redefining RedefineSubclassWithTwoInterfacesImpl before
  50         // RedefineSubclassWithTwoInterfacesTarget fails:
  51         do_redefine("RedefineSubclassWithTwoInterfacesImpl", 1);
  52         do_redefine("RedefineSubclassWithTwoInterfacesTarget", 1);
  53 
  54         mesg = remote.echo("test message #1");
  55         System.out.println("RedefineSubclassWithTwoInterfacesApp: mesg='"
  56             + mesg + "' after redefine #1");
  57     }
  58 
  59     private static void do_redefine(String className, int counter)
  60             throws Exception {
  61         File f = new File(className + "_" + counter + ".class");
  62         System.out.println("Reading test class from " + f);
  63         InputStream redefineStream = new FileInputStream(f);
  64 
  65         byte[] redefineBuffer
  66             = NamedBuffer.loadBufferFromStream(redefineStream);
  67 
  68         ClassDefinition redefineParamBlock = new ClassDefinition(
  69             Class.forName(className), redefineBuffer);
  70 
  71         RedefineSubclassWithTwoInterfacesAgent.getInstrumentation().
  72             redefineClasses(new ClassDefinition[] {redefineParamBlock});
  73     }
  74 }