Patch: FYI: re-merge a few classes

Tom Tromey tromey@redhat.com
Thu Feb 1 20:30:00 GMT 2007


I'm checking this in on the trunk and the RH 4.1 branch.

This re-merges a few classes: Charset, Class, ClassLoader, and
Calendar.  These fixes are needed to build Eclipse's Mylar project.

There may be other unmerged bits around.  I'll try to look.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* java/util/Calendar.java: Implement Comparable<Calendar>.  Update
	comments.
	(clear): Call complete.
	(setTimeZone): Call computeTime, computeFields.
	(compareTo): New method.
	* java/nio/charset/Charset.java: Implement Comparable<Charset>.
	(availableCharsets): Genericized.
	(aliases): Likewise.
	(compareTo): Changed argument type.
	* java/lang/ClassLoader.java (loadClass): Genericized.
	(findClass): Likewise.
	(defineClass): Likewise.
	(resolveClass): Likewise.
	(findSystemClass): Likewise.
	(setSigners): Likewise.
	(findLoadedClass): Likewise.
	(getResources): Likewise.
	(findResources): Likewise.
	(getSystemResources): Likewise.
	(checkInitialized): New method.
	* java/lang/Class.java (getCanonicalName): New method.

Index: java/lang/ClassLoader.java
===================================================================
--- java/lang/ClassLoader.java	(revision 121440)
+++ java/lang/ClassLoader.java	(working copy)
@@ -1,5 +1,5 @@
 /* ClassLoader.java -- responsible for loading classes into the VM
-   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -118,7 +118,6 @@
  * @author Eric Blake (ebb9@email.byu.edu)
  * @see Class
  * @since 1.0
- * @status still missing 1.4 functionality
  */
 public abstract class ClassLoader
 {
@@ -373,7 +372,7 @@
    * @return the loaded class
    * @throws ClassNotFoundException if the class cannot be found
    */
-  public Class loadClass(String name) throws ClassNotFoundException
+  public Class<?> loadClass(String name) throws ClassNotFoundException
   {
     return loadClass(name, false);
   }
@@ -401,7 +400,7 @@
    * @return the loaded class
    * @throws ClassNotFoundException if the class cannot be found
    */
-  protected synchronized Class loadClass(String name, boolean resolve)
+  protected synchronized Class<?> loadClass(String name, boolean resolve)
     throws ClassNotFoundException
   {
     SecurityManager sm = SecurityManager.current;
@@ -490,7 +489,7 @@
    * @throws ClassNotFoundException when the class can not be found
    * @since 1.2
    */
-  protected Class findClass(String name) throws ClassNotFoundException
+  protected Class<?> findClass(String name) throws ClassNotFoundException
   {
     throw new ClassNotFoundException(name);
   }
@@ -508,7 +507,7 @@
    *         offset + len exceeds data
    * @deprecated use {@link #defineClass(String, byte[], int, int)} instead
    */
-  protected final Class defineClass(byte[] data, int offset, int len)
+  protected final Class<?> defineClass(byte[] data, int offset, int len)
     throws ClassFormatError
   {
     return defineClass(null, data, offset, len);
@@ -533,8 +532,8 @@
    * @throws SecurityException if name starts with "java."
    * @since 1.1
    */
-  protected final Class defineClass(String name, byte[] data, int offset,
-                                    int len) throws ClassFormatError
+  protected final Class<?> defineClass(String name, byte[] data, int offset,
+				       int len) throws ClassFormatError
   {
     return defineClass(name, data, offset, len, null);
   }
@@ -562,15 +561,14 @@
    *         do not match up
    * @since 1.2
    */
-  protected final synchronized Class defineClass(String name, byte[] data,
-						 int offset, int len,
-						 ProtectionDomain domain)
+  protected final synchronized Class<?> defineClass(String name, byte[] data,
+						    int offset, int len,
+						    ProtectionDomain domain)
     throws ClassFormatError
   {
+    checkInitialized();
     if (domain == null)
       domain = defaultProtectionDomain;
-    if (! initialized)
-      throw new SecurityException("attempt to define class from uninitialized class loader");
     
     Class retval = VMClassLoader.defineClass(this, name, data,
 					     offset, len, domain);
@@ -615,8 +613,9 @@
    * @throws NullPointerException if c is null
    * @throws LinkageError if linking fails
    */
-  protected final void resolveClass(Class c)
+  protected final void resolveClass(Class<?> c)
   {
+    checkInitialized();
     VMClassLoader.resolveClass(c);
   }
 
@@ -629,9 +628,10 @@
    * @return the found class
    * @throws ClassNotFoundException if the class cannot be found
    */
-  protected final Class findSystemClass(String name)
+  protected final Class<?> findSystemClass(String name)
     throws ClassNotFoundException
   {
+    checkInitialized();
     return Class.forName(name, false, systemClassLoader);
   }
 
@@ -666,8 +666,9 @@
    * @param signers the signers to set
    * @since 1.1
    */
-  protected final void setSigners(Class c, Object[] signers)
+  protected final void setSigners(Class<?> c, Object[] signers)
   {
+    checkInitialized();
     c.setSigners(signers);
   }
 
@@ -678,8 +679,9 @@
    * @return the found Class, or null if it is not found
    * @since 1.1
    */
-  protected final synchronized Class findLoadedClass(String name)
+  protected final synchronized Class<?> findLoadedClass(String name)
   {
+    checkInitialized();
     // NOTE: If the VM is keeping its own cache, it may make sense to have
     // this method be native.
     return (Class) loadedClasses.get(name);
@@ -732,15 +734,16 @@
    * @return an enumaration of all resources found
    * @throws IOException if I/O errors occur in the process
    * @since 1.2
+   * @specnote this was <code>final</code> prior to 1.5
    */
-  public final Enumeration getResources(String name) throws IOException
+  public final Enumeration<URL> getResources(String name) throws IOException
   {
-    Enumeration parentResources;
+    Enumeration<URL> parentResources;
     if (parent == null)
       parentResources = VMClassLoader.getResources(name);
     else
       parentResources = parent.getResources(name);
-    return new DoubleEnumeration(parentResources, findResources(name));
+    return new DoubleEnumeration<URL>(parentResources, findResources(name));
   }
 
   /**
@@ -760,9 +763,9 @@
    * @throws IOException if I/O errors occur in the process
    * @since 1.2
    */
-  protected Enumeration findResources(String name) throws IOException
+  protected Enumeration<URL> findResources(String name) throws IOException
   {
-    return EmptyEnumeration.getInstance();
+    return (Enumeration<URL>) EmptyEnumeration.getInstance();
   }
 
   /**
@@ -807,7 +810,8 @@
    * @throws IOException if I/O errors occur in the process
    * @since 1.2
    */
-  public static Enumeration getSystemResources(String name) throws IOException
+  public static Enumeration<URL> getSystemResources(String name)
+    throws IOException
   {
     return systemClassLoader.getResources(name);
   }
@@ -939,7 +943,7 @@
       throw new IllegalArgumentException("Package " + name
                                          + " already defined");
     Package p = new Package(name, specTitle, specVendor, specVersion,
-                            implTitle, implVendor, implVersion, sealed);
+                            implTitle, implVendor, implVersion, sealed, this);
     synchronized (definedPackages)
       {
         definedPackages.put(name, p);
@@ -1115,4 +1119,16 @@
       }
     return false;
   }
+
+  /**
+   * Before doing anything "dangerous" please call this method to make sure
+   * this class loader instance was properly constructed (and not obtained
+   * by exploiting the finalizer attack)
+   * @see #initialized
+   */
+  private void checkInitialized()
+  {
+    if (! initialized)
+      throw new SecurityException("attempt to use uninitialized class loader");
+  }
 }
Index: java/lang/Class.java
===================================================================
--- java/lang/Class.java	(revision 121440)
+++ java/lang/Class.java	(working copy)
@@ -1,5 +1,5 @@
 /* Class.java -- Representation of a Java class.
-   Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
+   Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007
    Free Software Foundation
 
 This file is part of GNU Classpath.
@@ -1291,6 +1291,59 @@
   }
 
   /**
+   * <p>
+   * Returns the canonical name of this class, as defined by section
+   * 6.7 of the Java language specification.  Each package, top-level class,
+   * top-level interface and primitive type has a canonical name.  A member
+   * class has a canonical name, if its parent class has one.  Likewise,
+   * an array type has a canonical name, if its component type does.
+   * Local or anonymous classes do not have canonical names.
+   * </p>
+   * <p>
+   * The canonical name for top-level classes, top-level interfaces and
+   * primitive types is always the same as the fully-qualified name.
+   * For array types, the canonical name is the canonical name of its
+   * component type with `[]' appended.  
+   * </p>
+   * <p>
+   * The canonical name of a member class always refers to the place where
+   * the class was defined, and is composed of the canonical name of the
+   * defining class and the simple name of the member class, joined by `.'.
+   *  For example, if a <code>Person</code> class has an inner class,
+   * <code>M</code>, then both its fully-qualified name and canonical name
+   * is <code>Person.M</code>.  A subclass, <code>Staff</code>, of
+   * <code>Person</code> refers to the same inner class by the fully-qualified
+   * name of <code>Staff.M</code>, but its canonical name is still
+   * <code>Person.M</code>.
+   * </p>
+   * <p>
+   * Where no canonical name is present, <code>null</code> is returned.
+   * </p>
+   *
+   * @return the canonical name of the class, or <code>null</code> if the
+   *         class doesn't have a canonical name.
+   * @since 1.5
+   */
+  public String getCanonicalName()
+  {
+    if (isArray())
+      {
+	String componentName = getComponentType().getCanonicalName();
+	if (componentName != null)
+	  return componentName + "[]";
+      }
+    if (isMemberClass())
+      {
+	String memberName = getDeclaringClass().getCanonicalName();
+	if (memberName != null)
+	  return memberName + "." + getSimpleName();
+      }
+    if (isLocalClass() || isAnonymousClass())
+      return null;
+    return getName();
+  }
+
+  /**
    * Returns all annotations directly defined by this class.  If there are
    * no annotations associated with this class, then a zero-length array
    * will be returned.  The returned array may be modified by the client
Index: java/nio/charset/Charset.java
===================================================================
--- java/nio/charset/Charset.java	(revision 121440)
+++ java/nio/charset/Charset.java	(working copy)
@@ -1,5 +1,5 @@
 /* Charset.java -- 
-   Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2005, 2007  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -61,14 +61,15 @@
 /**
  * @author Jesse Rosenstock
  * @since 1.4
+ * @status updated to 1.5
  */
-public abstract class Charset implements Comparable
+public abstract class Charset implements Comparable<Charset>
 {
   private CharsetEncoder cachedEncoder;
   private CharsetDecoder cachedDecoder;
  
   /**
-   * Charset providers.
+   * Extra Charset providers.
    */
   private static CharsetProvider[] providers;
   
@@ -174,7 +175,7 @@
    * Returns the Charset instance for the charset of the given name.
    * 
    * @param charsetName
-   * @return
+   * @return the Charset instance for the indicated charset
    * @throws UnsupportedCharsetException if this VM does not support
    * the charset of the given name.
    * @throws IllegalCharsetNameException if the given charset name is
@@ -221,19 +222,20 @@
     return cs;
   }
 
-  public static SortedMap availableCharsets()
+  public static SortedMap<String, Charset> availableCharsets()
   {
-    TreeMap charsets = new TreeMap(String.CASE_INSENSITIVE_ORDER);
-    for (Iterator i = provider().charsets(); i.hasNext(); )
+    TreeMap<String, Charset> charsets
+      = new TreeMap(String.CASE_INSENSITIVE_ORDER);
+    for (Iterator<Charset> i = provider().charsets(); i.hasNext(); )
       {
-	Charset cs = (Charset) i.next();
+	Charset cs = i.next();
 	charsets.put(cs.name(), cs);
       }
 
     CharsetProvider[] providers = providers2();
     for (int j = 0; j < providers.length; j++)
       {
-        for (Iterator i = providers[j].charsets(); i.hasNext(); )
+        for (Iterator<Charset> i = providers[j].charsets(); i.hasNext(); )
           {
             Charset cs = (Charset) i.next();
             charsets.put(cs.name(), cs);
@@ -295,14 +297,14 @@
     return canonicalName;
   }
 
-  public final Set aliases ()
+  public final Set<String> aliases ()
   {
     if (aliases == null)
-      return Collections.EMPTY_SET;
+      return Collections.<String>emptySet();
 
     // should we cache the aliasSet instead?
     int n = aliases.length;
-    HashSet aliasSet = new HashSet (n);
+    HashSet<String> aliasSet = new HashSet<String> (n);
     for (int i = 0; i < n; ++i)
         aliasSet.add (aliases[i]);
     return Collections.unmodifiableSet (aliasSet);
@@ -387,9 +389,9 @@
       }
   }
 
-  public final int compareTo (Object ob)
+  public final int compareTo (Charset other)
   {
-    return canonicalName.compareToIgnoreCase (((Charset) ob).canonicalName);
+    return canonicalName.compareToIgnoreCase (other.canonicalName);
   }
 
   public final int hashCode ()
Index: java/util/Calendar.java
===================================================================
--- java/util/Calendar.java	(revision 121440)
+++ java/util/Calendar.java	(working copy)
@@ -1,5 +1,6 @@
 /* Calendar.java --
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005  Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
+   Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -103,7 +104,8 @@
  * @see TimeZone
  * @see java.text.DateFormat
  */
-public abstract class Calendar implements Serializable, Cloneable
+public abstract class Calendar
+  implements Serializable, Cloneable, Comparable<Calendar>
 {
   /**
    * Constant representing the era time field.
@@ -460,6 +462,8 @@
   /**
    * Creates a calendar representing the actual time, using the default
    * time zone and locale.
+   * 
+   * @return The new calendar.
    */
   public static synchronized Calendar getInstance()
   {
@@ -469,7 +473,12 @@
   /**
    * Creates a calendar representing the actual time, using the given
    * time zone and the default locale.
-   * @param zone a time zone.
+   * 
+   * @param zone a time zone (<code>null</code> not permitted).
+   * 
+   * @return The new calendar.
+   * 
+   * @throws NullPointerException if <code>zone</code> is <code>null</code>.
    */
   public static synchronized Calendar getInstance(TimeZone zone)
   {
@@ -479,7 +488,12 @@
   /**
    * Creates a calendar representing the actual time, using the default
    * time zone and the given locale.
-   * @param locale a locale.
+   * 
+   * @param locale a locale (<code>null</code> not permitted).
+   * 
+   * @return The new calendar.
+   * 
+   * @throws NullPointerException if <code>locale</code> is <code>null</code>.
    */
   public static synchronized Calendar getInstance(Locale locale)
   {
@@ -501,8 +515,14 @@
   /**
    * Creates a calendar representing the actual time, using the given
    * time zone and locale.
-   * @param zone a time zone.
-   * @param locale a locale.
+   * 
+   * @param zone a time zone (<code>null</code> not permitted).
+   * @param locale a locale (<code>null</code> not permitted).
+   * 
+   * @return The new calendar.
+   * 
+   * @throws NullPointerException if <code>zone</code> or <code>locale</code>
+   *     is <code>null</code>.
    */
   public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
   {
@@ -600,6 +620,10 @@
   /**
    * Sets this Calendar's time to the given Date.  All time fields
    * are invalidated by this method.
+   * 
+   * @param date  the date (<code>null</code> not permitted).
+   * 
+   * @throws NullPointerException if <code>date</code> is <code>null</code>.
    */
   public final void setTime(Date date)
   {
@@ -860,6 +884,7 @@
                          1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0,
                          0, 0, zone.getRawOffset(), 0
                        };
+    complete();
     isTimeSet = false;
     areFieldsSet = false;
     isSet[field] = false;
@@ -1020,6 +1045,8 @@
   public void setTimeZone(TimeZone zone)
   {
     this.zone = zone;
+    computeTime();
+    computeFields();
   }
 
   /**
@@ -1176,6 +1203,31 @@
   }
 
   /**
+   * Compares the time of two calendar instances.
+   * @param calendar the calendar to which the time should be compared.
+   * @return 0 if the two calendars are set to the same time, 
+   * less than 0 if the time of this calendar is before that of 
+   * <code>cal</code>, or more than 0 if the time of this calendar is after
+   * that of <code>cal</code>.
+   *
+   * @param cal the calendar to compare this instance with.
+   * @throws NullPointerException if <code>cal</code> is null.
+   * @throws IllegalArgumentException if either calendar has fields set to 
+   * invalid values.
+   * @since 1.5
+   */
+  public int compareTo(Calendar cal)
+  {
+    long t1 = getTimeInMillis();
+    long t2 = cal.getTimeInMillis();
+    if(t1 == t2)
+      return 0;
+    if(t1 > t2)
+      return 1;
+    return -1;
+  }
+
+  /**
    * Return a clone of this object.
    */
   public Object clone()



More information about the Java-patches mailing list