1 /*
   2  * Copyright 2000-2001 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot;
  26 
  27 import java.lang.reflect.*;
  28 
  29 public class HelloWorld {
  30   private static String helloWorldString = "Hello, world!";
  31   private static volatile int helloWorldTrigger = 0;
  32   private static final boolean useMethodInvoke = false;
  33   private static Object lock = new Object();
  34 
  35   public static void main(String[] args) {
  36     int foo = a();
  37 
  38     System.out.println("HelloWorld exiting. a() = " + foo);
  39   }
  40 
  41   private static int a() {
  42     return 1 + b();
  43   }
  44 
  45   private static int b() {
  46     return 1 + c();
  47   }
  48 
  49   private static int c() {
  50     return 1 + d("Hi");
  51   }
  52 
  53   private static int d(String x) {
  54     System.out.println("HelloWorld.d() received \"" + x + "\" as argument");
  55     synchronized(lock) {
  56       if (useMethodInvoke) {
  57         try {
  58           Method method = HelloWorld.class.getMethod("e", null);
  59           Integer result = (Integer) method.invoke(null, new Object[0]);
  60           return result.intValue();
  61         }
  62         catch (Exception e) {
  63           throw new RuntimeException(e.toString());
  64         }
  65       } else {
  66 
  67         int i = fib(10); // 89
  68         long l = i;
  69         float f = i;
  70         double d = i;
  71         char c = (char) i;
  72         short s = (short) i;
  73         byte b = (byte) i;
  74 
  75         int ret = e();
  76 
  77         System.out.println("Tenth Fibonacci number in all formats: " +
  78                            i + ", " +
  79                            l + ", " +
  80                            f + ", " +
  81                            d + ", " +
  82                            c + ", " +
  83                            s + ", " +
  84                            b);
  85 
  86         return ret;
  87       }
  88     }
  89   }
  90 
  91   public static int e() {
  92     System.out.println("Going to sleep...");
  93 
  94     int i = 0;
  95 
  96     while (helloWorldTrigger == 0) {
  97       if (++i == 1000000) {
  98         System.gc();
  99       }
 100     }
 101 
 102     System.out.println(helloWorldString);
 103 
 104     while (helloWorldTrigger != 0) {
 105     }
 106 
 107     return i;
 108   }
 109 
 110   // Tree-recursive implementation for test
 111   public static int fib(int n) {
 112     if (n < 2) {
 113       return 1;
 114     }
 115     return fib(n - 1) + fib(n - 2);
 116   }
 117 }