1 /*
2  * Copyright (c) 2013, 2018, 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 //package nsk.jvmti.RedefineClasses.StressRedefine;
24 package nsk.jvmti.RedefineClasses;
25 
26 import java.util.Random;
27 
28 
29 public class GenerateSourceHelper {
30 
31     static final String STATIC_METHOD_NAME = "staticMethod";
32     static final String NONSTATIC_METHOD_NAME = "regularMethod";
33     static final String CLASS_NAME = "MyClass";
34 
35     private static Random random;
36 
setRandom(Random random)37     public static void setRandom(Random random) {
38         GenerateSourceHelper.random = random;
39     }
40 
41     /**
42      * We can vary only constant pool and method bodies from call to call
43      *
44      */
generateSource()45     static CharSequence generateSource() {
46         return "public class " + CLASS_NAME + " { " +
47                         "public static String s1 = \"s1s" + random.nextInt() + "dfsdf\"; " +
48                                         "public int i = 1345345345; \n" +
49                                         "public static double dd = 1e-4; \n" +
50                                         "public String s2 = \"s2" + random.nextInt() + "sdfsdf\"; \n" +
51                                         "public static String static_s2 = \"s2" + random.nextInt() + "sdfsdf\"; \n" +
52                                         "protected String sprotected1 = \"asdfsdf" + random.nextInt() + "sdf\"; \n" +
53                                         "protected double d = -.12345; \n" +
54                                         "public String methodJustPadding() {return s1 + s2 + d; } \n" +
55                                         "public static String " + STATIC_METHOD_NAME + "(double d, int i, Object o) {\n" +
56                                                         "String ret_0 = \"little computation \" + (4 * dd  + d + i + o.hashCode() + s1.length()); \n" +
57                                                         "String ret_1 = \"in_static_method call_random=" + random.nextInt() + "\"; \n" +
58                                                         "String ret =  s1 + static_s2 + d + i + o; \n" +
59                                                         //"System.out.println(\"ret stat is : \" + ret);" +
60                                                         "return ret; " +
61                                         "} \n" +
62                                         "public String methodInTheMiddle() {return s1 + s2; } \n" +
63                                         "public String " + NONSTATIC_METHOD_NAME + "(double d, int i, Object o) {\n" +
64                                                         "String ret_0 = \"little computation \" + (2 * dd + 5 * d  + i + o.hashCode() + s2.length()); \n" +
65                                                         "String ret_1 = \"in_nonstatic_method call_random=" + random.nextInt() + "\"; " +
66                                                         "String ret = ret_0 + ret_1 +  s1 + s2 + i + o; \n" +
67                                                         //"System.out.println(\"ret nonstat is : \" + ret);" +
68                                                         "return ret;" +
69                                         "} \n" +
70                                         "public String methodFinalInClass() {return s1 + s2 + i; } \n" +
71                         "}";
72     }
73 
74 
75 }
76