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 echo() calls before any redefinitions:
  44         mesg = remote.echo1("test message #1.1");
  45         System.out.println("RedefineSubclassWithTwoInterfacesApp: echo1 mesg='"
  46             + mesg + "' before any redefines");
  47         mesg = remote.echo2("test message #2.1");
  48         System.out.println("RedefineSubclassWithTwoInterfacesApp: echo2 mesg='"
  49             + mesg + "' before any redefines");
  50 
  51 
  52         // redefining RedefineSubclassWithTwoInterfacesImpl before
  53         // RedefineSubclassWithTwoInterfacesTarget fails:
  54         do_redefine("RedefineSubclassWithTwoInterfacesImpl", 1);
  55         do_redefine("RedefineSubclassWithTwoInterfacesTarget", 1);
  56 
  57         mesg = remote.echo1("test message #1.2");
  58         System.out.println("RedefineSubclassWithTwoInterfacesApp: echo1 mesg='"
  59             + mesg + "' after redefine");
  60         mesg = remote.echo2("test message #2.2");
  61         System.out.println("RedefineSubclassWithTwoInterfacesApp: echo2 mesg='"
  62             + mesg + "' after redefine");
  63     }
  64 
  65     private static void do_redefine(String className, int counter)
  66             throws Exception {
  67         File f = new File(className + "_" + counter + ".class");
  68         System.out.println("Reading test class from " + f);
  69         InputStream redefineStream = new FileInputStream(f);
  70 
  71         byte[] redefineBuffer
  72             = NamedBuffer.loadBufferFromStream(redefineStream);
  73 
  74         ClassDefinition redefineParamBlock = new ClassDefinition(
  75             Class.forName(className), redefineBuffer);
  76 
  77         RedefineSubclassWithTwoInterfacesAgent.getInstrumentation().
  78             redefineClasses(new ClassDefinition[] {redefineParamBlock});
  79     }
  80 }