View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: ThreadUtils.java 1354 2007-09-28 15:35:10Z vlads $
21   */
22  package org.microemu.util;
23  
24  import java.lang.reflect.Constructor;
25  import java.lang.reflect.Method;
26  import java.util.Timer;
27  
28  public class ThreadUtils {
29  
30  	private static boolean java13 = false;
31  	
32  	private static boolean java14 = false;
33  	
34  	/**
35       * Creates a new timer whose associated thread has the specified name in Java 1.5.
36  	 * 
37  	 * @param name the name of the associated thread
38       *
39  	 */
40  	public static Timer createTimer(String name) {
41  		try {
42  			Constructor c = Timer.class.getConstructor(new Class[] { String.class });
43  			return (Timer)c.newInstance(new Object[]{name});
44  		} catch (Throwable e) {
45  			// In cany case create new Timer
46  			return new Timer();
47  		}
48  	}
49  
50  	public static String getCallLocation(String fqn) {
51  		if (!java13) {
52  			try {
53  				StackTraceElement[] ste = new Throwable().getStackTrace();
54  				for (int i = 0; i < ste.length - 1; i++) {
55  					if (fqn.equals(ste[i].getClassName())) {
56  						StackTraceElement callLocation = ste[i + 1];
57  						String nextClassName = callLocation.getClassName();
58  						if (nextClassName.equals(fqn)) {
59  							continue;
60  						}
61  						return callLocation.toString();
62  					}
63  				}
64  			} catch (Throwable e) {
65  				java13 = true;
66  			}
67  		}
68  		return null;
69  	}
70  	
71  	public static String getTreadStackTrace(Thread t) {
72  		if (java14) {
73  			return "";
74  		}
75  		try {
76  			// Java 1.5 thread.getStackTrace();
77  			Method m = t.getClass().getMethod("getStackTrace", null);
78  			
79  			StackTraceElement[] trace = (StackTraceElement[])m.invoke(t, null);
80  			StringBuffer b = new StringBuffer();  
81  			for (int i=0; i < trace.length; i++) {
82  			    b.append("\n\tat ").append(trace[i]);
83  			}
84  			return b.toString();
85  		} catch (Throwable e) {
86  			java14 = true;
87  			return "";
88  		}
89  	}
90  }