[kaffe] Patch: Merge of Classpath glue code for assertion framework

Benja Fallenstein b.fallenstein at gmx.de
Sun May 11 09:55:01 PDT 2003


Hi,

today I had a problem with a dependency using log4j, because log4j would 
try to use Class.desiredAssertionStatus() -- a 1.4 method that is part 
of the assertion framework. Since the library I depended on initialized 
log4j statically, my test suite wouldn't even start. To fix the problem, 
I merged the corresponding GNU Classpath code into Kaffe-- not actually 
implementing the whole assertion framework though, just the methods in 
Class and ClassLoader :-)

I was hoping this could be useful for Kaffe?

- Benja
-------------- next part --------------
? XOE_1.0
? XOE_1.0.tgz
? kev.alt
? libraries/clib/awt/qt/.deps
? libraries/clib/awt/qt/.libs
? libraries/clib/awt/qt/Makefile
? libraries/clib/awt/qt/cbd.lo
? libraries/clib/awt/qt/clr.lo
? libraries/clib/awt/qt/evt.lo
? libraries/clib/awt/qt/evt.moc.cc
? libraries/clib/awt/qt/evt.moc.lo
? libraries/clib/awt/qt/fnt.lo
? libraries/clib/awt/qt/gra.lo
? libraries/clib/awt/qt/img.lo
? libraries/clib/awt/qt/libawt.la
? libraries/clib/awt/qt/tlk.lo
? libraries/clib/awt/qt/wnd.lo
? libraries/extensions/sound/Makefile
? libraries/extensions/sound/common/.deps
? libraries/extensions/sound/common/.libs
? libraries/extensions/sound/common/common.lo
? libraries/extensions/sound/common/libtritonuscommon.la
? replace/.deps
? replace/.libs
? replace/Makefile
? replace/getifaddrs.lo
? replace/libreplace.la
? test/regression/serializedFoos.bin
Index: libraries/javalib/java/lang/Class.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/lang/Class.java,v
retrieving revision 1.39
diff -u -u -r1.39 Class.java
--- libraries/javalib/java/lang/Class.java	24 Jan 2003 00:07:32 -0000	1.39
+++ libraries/javalib/java/lang/Class.java	11 May 2003 16:45:22 -0000
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 1997, 1998
  *      Transvirtual Technologies, Inc.  All rights reserved.
+ * Portions Copyright (C) 1998, 2002 Free Software Foundation
  *
  * See the file "license.terms" for information on usage and redistribution
  * of this file.
@@ -491,5 +492,81 @@
 		}
 		return null;
 	}
+}
+
+/**
+ * Returns the desired assertion status of this class, if it were to be
+ * initialized at this moment. The class assertion status, if set, is
+ * returned; the backup is the default package status; then if there is
+ * a class loader, that default is returned; and finally the system default
+ * is returned. This method seldom needs calling in user code, but exists
+ * for compilers to implement the assert statement. Note that there is no
+ * guarantee that the result of this method matches the class's actual
+ * assertion status.
+ *
+ * @return the desired assertion status
+ * @see ClassLoader#setClassAssertionStatus(String, boolean)
+ * @see ClassLoader#setPackageAssertionStatus(String, boolean)
+ * @see ClassLoader#setDefaultAssertionStatus(boolean)
+ * @since 1.4
+ */
+public boolean desiredAssertionStatus() {
+	ClassLoader c = getClassLoader();
+	Object status;
+	if (c == null)
+		return ClassLoader.systemDefaultAssertionStatus;
+	if (c.classAssertionStatus != null) {
+		synchronized (c) {
+			status = c.classAssertionStatus.get(getName());
+			if (status != null)
+				return status.equals(Boolean.TRUE);
+		}
+	} else {
+		status = ClassLoader.systemClassAssertionStatus.get(getName());
+		if (status != null)
+			return status.equals(Boolean.TRUE);
+	}
+	if (c.packageAssertionStatus != null) {
+		synchronized (c) {
+			String name = getPackagePortion(getName());
+			if ("".equals(name))
+				status = c.packageAssertionStatus.get(null);
+			else {
+				do {
+					status = c.packageAssertionStatus.get(name);
+					name = getPackagePortion(name);
+				} while (! "".equals(name) && status == null);
+			}
+			if (status != null)
+				return status.equals(Boolean.TRUE);
+		}
+	} else {
+		String name = getPackagePortion(getName());
+		if ("".equals(name))
+			status = ClassLoader.systemPackageAssertionStatus.get(null);
+		else {
+			do {
+				status = ClassLoader.systemPackageAssertionStatus.get(name);
+				name = getPackagePortion(name);
+			} while (! "".equals(name) && status == null);
+		}
+		if (status != null)
+			return status.equals(Boolean.TRUE);
+	}
+	return c.defaultAssertionStatus;
+}
+
+/**
+ * Strip the last portion of the name (after the last dot).
+ *
+ * @param name the name to get package of
+ * @return the package name, or "" if no package
+ */
+private static String getPackagePortion(String name)
+{
+	int lastInd = name.lastIndexOf('.');
+	if (lastInd == -1)
+		return "";
+	return name.substring(0, lastInd);
 }
 }
Index: libraries/javalib/java/lang/ClassLoader.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/lang/ClassLoader.java,v
retrieving revision 1.27
diff -u -u -r1.27 ClassLoader.java
--- libraries/javalib/java/lang/ClassLoader.java	4 Sep 2002 16:08:13 -0000	1.27
+++ libraries/javalib/java/lang/ClassLoader.java	11 May 2003 16:45:23 -0000
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 1997, 1998
  *      Transvirtual Technologies, Inc.  All rights reserved.
+ * Portions Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
  *
  * See the file "license.terms" for information on usage and redistribution
  * of this file.
@@ -16,7 +17,9 @@
 import java.security.ProtectionDomain;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.Vector;
@@ -52,6 +55,54 @@
  */
 private final Hashtable protectionDomains = new Hashtable();
 
+/**
+ * The command-line state of the default assertion status overrides.
+ */
+// Package visible for use by Class.
+// XXX should be set from command line
+static final boolean systemDefaultAssertionStatus = true;
+
+/**
+ * The desired assertion status of classes loaded by this loader, if not
+ * overridden by package or class instructions.
+ */
+// Package visible for use by Class.
+boolean defaultAssertionStatus = systemDefaultAssertionStatus;
+
+/**
+ * The command-line state of the package assertion status overrides. This
+ * map is never modified, so it does not need to be synchronized.
+ */
+// Package visible for use by Class.
+// XXX should be set from command line
+static final Map systemPackageAssertionStatus = new HashMap();
+
+/**
+ * The map of package assertion status overrides, or null if no package
+ * overrides have been specified yet. The values of the map should be
+ * Boolean.TRUE or Boolean.FALSE, and the unnamed package is represented
+ * by the null key. This map must be synchronized on this instance.
+ */
+// Package visible for use by Class.
+Map packageAssertionStatus;
+
+/**
+ * The command-line state of the class assertion status overrides. This
+ * map is never modified, so it does not need to be synchronized.
+ */
+// Package visible for use by Class.
+// XXX should be set from command line
+static final Map systemClassAssertionStatus = new HashMap();
+
+/**
+ * The map of class assertion status overrides, or null if no class
+ * overrides have been specified yet. The values of the map should be
+ * Boolean.TRUE or Boolean.FALSE. This map must be synchronized on this
+ * instance.
+ */
+// Package visible for use by Class.
+Map classAssertionStatus;
+
 private ProtectionDomain defaultProtectionDomain;
 private final ClassLoader parent;
 
@@ -309,4 +360,77 @@
 private native Class defineClass0(String name, byte data[], int off, int len);
 private native void resolveClass0(Class cls);
 
+/**
+ * Set the default assertion status for classes loaded by this classloader,
+ * used unless overridden by a package or class request.
+ *
+ * @param enabled true to set the default to enabled
+ * @see #setClassAssertionStatus(String, boolean)
+ * @see #setPackageAssertionStatus(String, boolean)
+ * @see #clearAssertionStatus()
+ * @since 1.4
+ */
+public void setDefaultAssertionStatus(boolean enabled)
+{
+	defaultAssertionStatus = enabled;
+}
+  
+/**
+ * Set the default assertion status for packages, used unless overridden
+ * by a class request. This default also covers subpackages, unless they
+ * are also specified. The unnamed package should use null for the name.
+ *
+ * @param name the package (and subpackages) to affect
+ * @param enabled true to set the default to enabled
+ * @see #setDefaultAssertionStatus(String, boolean)
+ * @see #setClassAssertionStatus(String, boolean)
+ * @see #clearAssertionStatus()
+ * @since 1.4
+ */
+public synchronized void setPackageAssertionStatus(String name,
+                                                   boolean enabled)
+{
+	if (packageAssertionStatus == null)
+		packageAssertionStatus
+		  = new HashMap(systemPackageAssertionStatus);
+	packageAssertionStatus.put(name, enabled ? Boolean.TRUE : Boolean.FALSE);
+}
+  
+/**
+ * Set the default assertion status for a class. This only affects the
+ * status of top-level classes, any other string is harmless.
+ *
+ * @param name the class to affect
+ * @param enabled true to set the default to enabled
+ * @throws NullPointerException if name is null
+ * @see #setDefaultAssertionStatus(String, boolean)
+ * @see #setPackageAssertionStatus(String, boolean)
+ * @see #clearAssertionStatus()
+ * @since 1.4
+ */
+public synchronized void setClassAssertionStatus(String name,
+                                                 boolean enabled)
+{
+	if (classAssertionStatus == null)
+		classAssertionStatus = new HashMap(systemClassAssertionStatus);
+	// The toString() hack catches null, as required.
+	classAssertionStatus.put(name.toString(), enabled ? Boolean.TRUE : Boolean.FALSE);
+}
+  
+/**
+ * Resets the default assertion status of this classloader, its packages
+ * and classes, all to false. This allows overriding defaults inherited
+ * from the command line.
+ *
+ * @see #setDefaultAssertionStatus(boolean)
+ * @see #setClassAssertionStatus(String, boolean)
+ * @see #setPackageAssertionStatus(String, boolean)
+ * @since 1.4
+ */
+public synchronized void clearAssertionStatus()
+{
+	defaultAssertionStatus = false;
+	packageAssertionStatus = new HashMap();
+	classAssertionStatus = new HashMap();
+}
 }


More information about the kaffe mailing list