001: /**************************************************************************/
002: /* N I C E */
003: /* A high-level object-oriented research language */
004: /* (c) Daniel Bonniot 2004 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.util;
012:
013: /**
014: Implementations of features that were introduced in recent JDKs
015: (1.4 and later).
016:
017: We don't want to call them directly, as this would break compatibility with
018: JDK 1.3, but we implement replacements here. If we decide to raise the
019: requirements, we can simply delete these methods and replace them with the
020: JDK implementation.
021:
022: @author Daniel Bonniot (bonniot@users.sf.net)
023: */
024:
025: public class JDK {
026: /* JDK 1.4 */
027:
028: /** Replacement for String.replaceAll(String,String) */
029: public static String replaceAll(String source, String what,
030: String with) {
031: int index = source.indexOf(what);
032:
033: if (index == -1)
034: return source;
035:
036: int len = what.length();
037: int last = 0;
038:
039: StringBuffer res = new StringBuffer(source.length() * 2);
040:
041: while (index != -1) {
042: res.append(source.substring(last, index));
043: res.append(with);
044: last = index + len;
045: index = source.indexOf(what, last);
046: }
047: res.append(source.substring(last));
048:
049: return res.toString();
050: }
051:
052: /** Replacement for ClassLoader.setDefaultAssertionStatus(boolean) */
053: public static void setDefaultAssertionStatus(
054: java.lang.ClassLoader loader, boolean st)
055: throws java.lang.reflect.InvocationTargetException {
056: try {
057: // JDK 1.4+
058: java.lang.ClassLoader.class.getDeclaredMethod(
059: "setDefaultAssertionStatus",
060: new Class[] { Boolean.TYPE }).invoke(loader,
061: new Object[] { new Boolean(st) });
062: } catch (NoSuchMethodException e) {
063: // Under earlier versions, Nice-generated assertions are controlled
064: // by the 'assertions' property.
065: java.lang.System.setProperty("assertions", new Boolean(st)
066: .toString());
067: } catch (IllegalAccessException e) {
068: throw new Error(
069: "ClassLoader.setDefaultAssertionStatus must be public");
070: }
071: }
072:
073: /** Replacement for Throwable.getStackTrace()
074: @return null if that feature is not available.
075: */
076: public static Object[] getStackTrace(Throwable t)
077: throws java.lang.reflect.InvocationTargetException {
078: try {
079: return (Object[]) Throwable.class.getMethod(
080: "getStackTrace", null).invoke(t, null);
081: } catch (NoSuchMethodException e) {
082: return null;
083: } catch (IllegalAccessException e) {
084: throw new Error("Throwable.getStackTrace must be public");
085: }
086: }
087:
088: private static Object call(Object o, String method)
089: throws java.lang.reflect.InvocationTargetException {
090: try {
091: return o.getClass().getDeclaredMethod(method, null).invoke(
092: o, null);
093: } catch (NoSuchMethodException e) {
094: return null;
095: } catch (IllegalAccessException e) {
096: throw new Error("" + o.getClass() + "." + method
097: + " must be public");
098: }
099: }
100:
101: public static String stackFileName(Object stackTraceElement)
102: throws java.lang.reflect.InvocationTargetException {
103: return (String) call(stackTraceElement, "getFileName");
104: }
105:
106: public static String stackMethodName(Object stackTraceElement)
107: throws java.lang.reflect.InvocationTargetException {
108: return (String) call(stackTraceElement, "getMethodName");
109: }
110:
111: public static String stackClassName(Object stackTraceElement)
112: throws java.lang.reflect.InvocationTargetException {
113: return (String) call(stackTraceElement, "getClassName");
114: }
115:
116: public static int stackLineNumber(Object stackTraceElement)
117: throws java.lang.reflect.InvocationTargetException {
118: return ((Integer) call(stackTraceElement, "getLineNumber"))
119: .intValue();
120: }
121:
122: }
|