diff options
author | John Keiser <shalom@gnu.org> | 1998-08-11 23:44:35 +0000 |
---|---|---|
committer | John Keiser <shalom@gnu.org> | 1998-08-11 23:44:35 +0000 |
commit | 10b9a27f252f52f02449080163a447ea66081321 (patch) | |
tree | d1520ba172410970f545bda96fbb69ee0c79596a /java | |
parent | 1733937f148868957658b837cff9559cd7a6a16e (diff) | |
download | classpath-10b9a27f252f52f02449080163a447ea66081321.tar.gz |
Added Java implementation for basic system classes.
Diffstat (limited to 'java')
-rw-r--r-- | java/lang/Class.java | 416 | ||||
-rw-r--r-- | java/lang/ClassLoader.java | 155 | ||||
-rw-r--r-- | java/lang/Object.java | 235 | ||||
-rw-r--r-- | java/lang/Runtime.java | 224 | ||||
-rw-r--r-- | java/lang/System.java | 280 |
5 files changed, 1310 insertions, 0 deletions
diff --git a/java/lang/Class.java b/java/lang/Class.java new file mode 100644 index 000000000..468a8e23f --- /dev/null +++ b/java/lang/Class.java @@ -0,0 +1,416 @@ +/* + * java.lang.Class: part of the Java Class Libraries project. + * Copyright (C) 1998 John Keiser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +import java.lang.reflect.*; +import gnu.java.lang.*; + +/** + ** A Class represents a Java type. There will never be + ** multiple Class objects with identical names and + ** ClassLoaders.<P> + ** + ** Arrays with identical type and number of dimensions + ** share the same class (and null "system" ClassLoader, + ** incidentally). The name of an array class is + ** <CODE>[<type name></CODE> ... for example, + ** String[]'s class is <CODE>[java.lang.String</CODE>. + ** boolean, byte, short, char, int, long, float and double + ** have the "type name" of Z,B,S,C,I,J,F,D for the + ** purposes of array classes. If it's a multidimensioned + ** array, the same principle applies: + ** <CODE>int[][][]</CODE> == <CODE>[[[I</CODE>.<P> + ** + ** As of 1.1, this class represents primitive types as + ** well. You can get to those by looking at + ** java.lang.Integer.TYPE, java.lang.Boolean.TYPE, etc. + ** + ** @author John Keiser + ** @version 1.1.0, Aug 6 1998 + ** @since JDK1.0 + **/ + +public class Class { + Class superclass; + String name; + Object[] signers; + + private Class(Class superclass, String name) { + this.superclass = superclass; + this.name = name.replace('/','.'); + } + + /** Return the human-readable form of this Object. For + ** class, that means "interface " or "class " plus the + ** classname. + ** @return the human-readable form of this Object. + ** @since JDK1.0 + **/ + public String toString() { + return (isInterface() ? "interface " : "class ") + getName(); + } + + /** Get the name of this class, separated by dots for + ** package separators. + ** @return the name of this class. + ** @since JDK1.0 + **/ + public String getName() { + return name; + } + + /** Get whether this class is an interface or not. Array + ** types are not interfaces. + ** @return whether this class is an interface or not. + ** @since JDK1.0 + **/ + public boolean isInterface() { + return !java.lang.Object.class.isAssignableFrom(this); + } + + /** Get the direct superclass of this class. If this is + ** an interface, it will get the direct superinterface. + ** @return the direct superclass of this class. + ** @since JDK1.0 + **/ + public Class getSuperclass() { + return superclass; + } + + /** Get the interfaces this class <EM>directly</EM> + ** implements, in the order that they were declared. + ** This method may return an empty array, but will + ** never return null. + ** @return the interfaces this class directly implements. + ** @since JDK1.0 + **/ + public native Class[] getInterfaces(); + + /** Get a new instance of this class by calling the + ** no-argument constructor. + ** @return a new instance of this class. + ** @exception InstantiationException if there is not a + ** no-arg constructor for this class, or if + ** an exception occurred during instantiation. + ** @exception IllegalAccessException if you are not + ** allowed to access the no-arg constructor of + ** this Class for whatever reason. + ** @since JDK1.0 + **/ + public native Object newInstance() throws InstantiationException, IllegalAccessException; + + /** Get the ClassLoader that loaded this class. If it was + ** loaded by the system classloader, this method will + ** return null. + ** @return the ClassLoader that loaded this class. + ** @since JDK1.0 + **/ + public native ClassLoader getClassLoader(); + + /** Use the system classloader to load and link a class. + ** @param name the name of the class to find. + ** @return the Class object representing the class. + ** @exception ClassNotFoundException if the class was not + ** found by the system classloader. + ** @since JDK1.0 + **/ + public static native Class forName(String name) throws ClassNotFoundException; + + /** Discover whether an Object is an instance of this + ** Class. Think of it as almost like + ** <CODE>o instanceof (this class)</CODE>. + ** @param o the Object to check + ** @return whether o is an instance of this class. + ** @since JDK1.1 + **/ + public native boolean isInstance(Object o); + + /** Discover whether an instance of the Class parameter + ** would be an instance of this Class as well. Think of + ** doing <CODE>isInstance(c.newInstance())</CODE> or even + ** <CODE>c instanceof (this class)</CODE>. + ** @param c the class to check + ** @return whether an instance of c would be an instance + ** of this class as well. + ** @since JDK1.1 + **/ + public native boolean isAssignableFrom(Class c); + + /** Return whether this class is an array type. + ** @return whether this class is an array type. + ** @since JDK1.1 + **/ + public boolean isArray() { + return name.charAt(0) == '['; + } + + /** Return whether this class is a primitive type. A + ** primitive type class is a class representing a kind of + ** "placeholder" for the various primitive types. You + ** can access the various primitive type classes through + ** java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc. + ** @return whether this class is a primitive type. + ** @since JDK1.1 + **/ + public boolean isPrimitive() { + return this == java.lang.Boolean.TYPE + || this == java.lang.Byte.TYPE + || this == java.lang.Short.TYPE + || this == java.lang.Character.TYPE + || this == java.lang.Integer.TYPE + || this == java.lang.Long.TYPE + || this == java.lang.Float.TYPE + || this == java.lang.Double.TYPE + || this == java.lang.Void.TYPE; + } + + /** If this is an array, get the Class representing the + ** type of array. Examples: [[java.lang.String would + ** return [java.lang.String, and calling getComponentType + ** on that would give java.lang.String. If this is not + ** an array, returns null. + ** @return the array type of this class, or null. + ** @since JDK1.1 + **/ + public Class getComponentType() { + if(isArray()) { + try { + return Class.forName(name.substring(1)); + } catch(ClassNotFoundException e) { + return null; + } + } else { + return null; + } + } + + /** Get the signers of this class. + ** @return the signers of this class. + ** @since JDK1.1 + **/ + public Object[] getSigners() { + return signers; + } + + /** Set the signers of this class. + ** @param signers the signers of this class. + ** @since JDK1.1 + **/ + public void setSigners(Object[] signers) { + this.signers = signers; + } + + /** Get a resource URL using this class's package using + ** the getClassLoader().getResource() method. If this + ** class was loaded using the system classloader, + ** ClassLoader.getSystemResource() is used instead.<P> + ** + ** If the name you supply is absolute (it starts with a + ** <CODE>/</CODE>), then it is passed on to getResource() + ** as is. If it is relative, the package name is + ** prepended, with <CODE>.</CODE>'s replaced with + ** <CODE>/</CODE> slashes.<P> + ** + ** The URL returned is system- and classloader- + ** dependent, and could change across implementations. + ** @param name the name of the resource, generally a + ** path. + ** @return the URL to the resource. + **/ + public java.net.URL getResource(String name) { + if(name.length() > 0 && name.charAt(0) != '/') { + name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name; + } + ClassLoader c = getClassLoader(); + if(c == null) { + return ClassLoader.getSystemResource(name); + } else { + return c.getResource(name); + } + } + + /** Get a resource using this class's package using the + ** getClassLoader().getResource() method. If this class + ** was loaded using the system classloader, + ** ClassLoader.getSystemResource() is used instead.<P> + ** + ** If the name you supply is absolute (it starts with a + ** <CODE>/</CODE>), then it is passed on to getResource() + ** as is. If it is relative, the package name is + ** prepended, with <CODE>.</CODE>'s replaced with + ** <CODE>/</CODE> slashes.<P> + ** + ** The URL returned is system- and classloader- + ** dependent, and could change across implementations. + ** @param name the name of the resource, generally a + ** path. + ** @return An InputStream with the contents of the + ** resource in it. + **/ + public java.io.InputStream getResourceAsStream(String name) { + if(name.length() > 0 && name.charAt(0) != '/') { + name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name; + } + ClassLoader c = getClassLoader(); + if(c == null) { + return ClassLoader.getSystemResourceAsStream(name); + } else { + return c.getResourceAsStream(name); + } + } + + /** Get the modifiers of this class. These can be checked + ** against using java.lang.reflect.Modifier. + ** @return the modifiers of this class. + ** @see java.lang.reflect.Modifer + ** @since JDK1.1 + **/ + public native int getModifiers(); + + /** If this is an inner class, return the class that + ** declared it. If not, return null. + ** @return the declaring class of this class. + ** @since JDK1.1 + **/ + public native Class getDeclaringClass(); + + /** Get all the public inner classes, declared in this + ** class or inherited from superclasses, that are + ** members of this class. + ** @return all public inner classes in this class. + **/ + public native Class[] getClasses(); + + /** Get all the inner classes declared in this class. + ** @return all inner classes declared in this class. + ** @exception SecurityException if you do not have access + ** to non-public inner classes of this class. + **/ + public native Class[] getDeclaredClasses() throws SecurityException; + + /** Get a public constructor from this class. + ** @param args the argument types for the constructor. + ** @return the constructor. + ** @exception NoSuchMethodException if the constructor does + ** not exist. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Constructor getConstructor(Class[] args) throws NoSuchMethodException, SecurityException; + + /** Get a constructor declared in this class. + ** @param args the argument types for the constructor. + ** @return the constructor. + ** @exception NoSuchMethodException if the constructor does + ** not exist in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Constructor getDeclaredConstructor(Class[] args) throws NoSuchMethodException, SecurityException; + + /** Get all public constructors from this class. + ** @return all public constructors in this class. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Constructor[] getConstructors() throws SecurityException; + + /** Get all constructors declared in this class. + ** @return all constructors declared in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Constructor[] getDeclaredConstructors() throws SecurityException; + + + /** Get a public method from this class. + ** @param name the name of the method. + ** @param args the argument types for the method. + ** @return the method. + ** @exception NoSuchMethodException if the method does + ** not exist. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Method getMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException; + + /** Get a method declared in this class. + ** @param name the name of the method. + ** @param args the argument types for the method. + ** @return the method. + ** @exception NoSuchMethodException if the method does + ** not exist in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Method getDeclaredMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException; + + /** Get all public methods from this class. + ** @return all public methods in this class. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Method[] getMethods() throws SecurityException; + + /** Get all methods declared in this class. + ** @return all methods declared in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Method[] getDeclaredMethods() throws SecurityException; + + + /** Get a public field from this class. + ** @param name the name of the field. + ** @return the field. + ** @exception NoSuchFieldException if the field does + ** not exist. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Field getField(String name) throws NoSuchFieldException, SecurityException; + + /** Get a field declared in this class. + ** @param name the name of the field. + ** @return the field. + ** @exception NoSuchFieldException if the field does + ** not exist in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException; + + /** Get all public fields from this class. + ** @return all public fields in this class. + ** @exception SecurityException if you do not have access to public + ** members of this class. + **/ + public native Field[] getFields() throws SecurityException; + + /** Get all fields declared in this class. + ** @return all fieilds declared in this class. + ** @exception SecurityException if you do not have access to + ** non-public members of this class. + **/ + public native Field[] getDeclaredFields() throws SecurityException; + +} + diff --git a/java/lang/ClassLoader.java b/java/lang/ClassLoader.java new file mode 100644 index 000000000..f36d33a9d --- /dev/null +++ b/java/lang/ClassLoader.java @@ -0,0 +1,155 @@ +/* + * java.lang.ClassLoader: part of the Java Class Libraries project. + * Copyright (C) 1998 John Keiser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +import java.lang.reflect.*; +import gnu.java.lang.*; +import java.io.*; +import java.net.*; + +/** + ** The ClassLoader is a way of customizing the way Java + ** gets its classes and loads them into memory. The + ** verifier and other standard Java things still run, but + ** the ClassLoader is allowed great flexibility in + ** determining where to get the classfiles and when to + ** load and resolve them. + ** + ** @author John Keiser + ** @version 1.1.0, Aug 6 1998 + ** @since JDK1.0 + **/ + +public abstract class ClassLoader { + /** Create a new ClassLoader. + ** @exception SecurityException if you do not have permission + ** to create a ClassLoader. + **/ + public ClassLoader() throws SecurityException { + try { + System.getSecurityManager().checkCreateClassLoader(); + } catch(NullPointerException e) { + } + } + + /** Load a class using this ClassLoader. + ** @param name the name of the class relative to this ClassLoader. + ** @exception ClassNotFoundException if the class cannot be found to + ** be loaded. + ** @return the loaded class. + **/ + public native Class loadClass(String name) throws ClassNotFoundException; + + /** Load a class using this ClassLoader, possibly resolving it as well + ** using resolveClass(). + ** @param name the name of the class relative to this ClassLoader. + ** @param resolve whether or not to resolve the class. + ** @exception ClassNotFoundException if the class cannot be found to + ** be loaded. + ** @return the loaded class. + **/ + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + Class c = loadClass(name); + if(resolve) { + resolveClass(c); + } + return c; + } + + /** Get the URL to a resource using this classloader. + ** @param name the name of the resource relative to this + ** classloader. + ** @return the URL to the resource. + **/ + public URL getResource(String name) { + return ClassLoader.getSystemResource(name); + } + + /** Get a resource using this classloader. + ** @param name the name of the resource relative to this + ** classloader. + ** @return the resource. + **/ + public InputStream getResourceAsStream(String name) { + return ClassLoader.getSystemResourceAsStream(name); + } + + + /** Helper to define a class using a string of bytes. + ** @param data the data representing the classfile, in classfile format. + ** @param offset the offset into the data where the classfile starts. + ** @param len the length of the classfile data in the array. + ** @return the class that was defined. + ** @deprecated use defineClass(String,...) instead. + **/ + protected final native Class defineClass(byte[] data, int offset, int len) throws ClassFormatError; + + /** Helper to define a class using a string of bytes. + ** @param name the name to give the class. + ** @param data the data representing the classfile, in classfile format. + ** @param offset the offset into the data where the classfile starts. + ** @param len the length of the classfile data in the array. + ** @return the class that was defined. + ** @exception ClassFormatError if the byte array is not in proper classfile format. + **/ + protected final native Class defineClass(String name, byte[] data, int offset, int len) throws ClassFormatError; + + /** Helper to resolve all references to other classes from this class. + ** @param c the class to resolve. + **/ + protected final native void resolveClass(Class c); + + /** Helper to find a Class using the system classloader. + ** @param name the name of the class to find. + ** @return the found class + ** @exception ClassNotFoundException if the class cannot be found. + **/ + protected final native Class findSystemClass(String name) throws ClassNotFoundException; + + /** Helper to set the signers of a class. + ** @param c the Class to set signers of + ** @param signers the signers to set + **/ + protected final void setSigners(Class c, Object[] signers) { + c.setSigners(signers); + } + + /** Helper to find an already-loaded class in this ClassLoader. + ** @param name the name of the class to find. + ** @return the found Class, or null if it is not found. + **/ + protected final native Class findLoadedClass(String name); + + /** Get the URL to a resource using the system classloader. + ** @param name the name of the resource relative to the + ** system classloader. + ** @return the URL to the resource. + **/ + public static final native URL getSystemResource(String name); + + /** Get a resource using the system classloader. + ** @param name the name of the resource relative to the + ** system classloader. + ** @return the resource. + **/ + public static final native InputStream getSystemResourceAsStream(String name); +} + diff --git a/java/lang/Object.java b/java/lang/Object.java new file mode 100644 index 000000000..1eab5cb5f --- /dev/null +++ b/java/lang/Object.java @@ -0,0 +1,235 @@ +/* + * java.lang.Object: part of the Java Class Libraries project. + * Copyright (C) 1998 John Keiser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +/** + ** Object is the ultimate superclass of every class + ** (excepting interfaces). When you define a class that + ** does not extend any other class, it implicitly extends + ** java.lang.Object. + ** + ** It provides general-purpose methods that every single + ** Object, regardless of race, sex or creed, implements. + ** + ** @author John Keiser + ** @version 1.1.0, Aug 6 1998 + **/ + +public class Object { + /** Determine whether this Object is semantically equal + ** to another Object.<P> + ** + ** There are some fairly strict requirements on this + ** method which subclasses must follow:<P> + ** + ** <UL> + ** <LI>It must be transitive. If a.equals(b) and + ** b.equals(c) then a.equals(c) should be true + ** as well.</LI> + ** <LI>It must be symmetric. If a.equals(b) then + ** b.equals(a) must be true as well. If !a.equals(b) + ** then b.equals(a) must be false.</LI> + ** <LI>It must be reflexive. a.equals(a) must always be + ** true.</LI> + ** <LI>a.equals(null) must be false.</LI> + ** </UL> + ** + ** The Object implementation of equals returns this == o. + ** + ** @param o the Object to compare to. + ** @return whether this Object is semantically equal to + ** another. + **/ + public boolean equals(Object o) { + return this == o; + } + + /** Get a value that represents this Object, as uniquely as possible within + ** the confines of an int. This method is called on Objects.<P> + ** + ** The Object implementation returns System.identityHashCode(this); + ** @return the hash code for this Object. + **/ + public int hashCode() { + return System.identityHashCode(this); + } + + /** Convert this Object to a human-readable String. + ** There are no limits placed on how long this String + ** should be or what it should contain. We suggest you + ** make it as intuitive as possible to be able to place + ** it into System.out.println().<P> + ** + ** The Object implementation of toString() returns + ** <CODE>getClass().getName() + "@" + Integer.toHexString(hashCode())</CODE>. + ** + ** @return the String representing this Object. + **/ + public String toString() { + return getClass().getName() + "@" + Integer.toHexString(hashCode()); + } + + /** Called on every object at some point after the Object + ** is determined unreachable and before it is destroyed. + ** You would think that this means it eventually is + ** called on every Object, but this is not necessarily + ** the case. If execution terminates abnormally, garbage + ** collection does not always happen. Thus you cannot + ** rely on this method to always work.<P> + ** + ** finalize() will be called by a Thread that has no + ** locks on any Objects. Why this is important, I have + ** no idea, but Sun says it's so, so it's so.<P> + ** + ** If an Exception is thrown from finalize(), it will be + ** patently ignored and the Object will still be + ** destroyed.<P> + ** + ** The Object implementation of finalize() does nothing. + **/ + protected void finalize() { + } + + /** This method may be called to create a new copy of the + ** Object. However, there are *no* requirements at all + ** placed on this method, just suggestions. The ==, + ** equals() and instanceof comparisons may even return + ** false when comparing the original with the clone!<P> + ** + ** If the Object you call clone() on does not implement + ** Cloneable (which is a placeholder interface), then + ** a CloneNotSupportedException is thrown.<P> + ** + ** Object's implementation of clone allocates space for + ** the new Object using the correct class, and then fills + ** in all of the new field values with the old field + ** values. Thus, it is a shallow copy. + ** + ** @exception CloneNotSupportedException + ** @return a copy of the Object. + **/ + public Object clone() throws CloneNotSupportedException { + if(this instanceof Cloneable) { + return nativeClone(); + } else { + throw new CloneNotSupportedException(); + } + } + + /** Returns the class of this Object as a Class object. + ** @return the class of this Object. + ** @see java.lang.Class + **/ + public native Class getClass(); + + /** Wakes up one of the threads that is waiting on this + ** Object's monitor. Only the owner of a lock on the + ** Object may call this method.<P> + ** + ** The Thread to wake up is chosen arbitrarily.<P> + ** + ** If the Thread waiting on this Object is waiting + ** because it wants to obtain the lock, then the notify() + ** call will in essence do nothing, since the lock will + ** still be owned by the Thread that called notify(). + ** + ** @exception IllegalMonitorStateException if this Thread + ** does not own the lock on the Object. + **/ + public final native void notify() throws IllegalMonitorStateException; + + /** Wakes up all of the threads waiting on this Object's + ** monitor. Only the owner of the lock on this Object + ** may call this method.<P> + ** + ** If the Threads waiting on this Object are waiting + ** because they want to obtain the lock, then the + ** notifyAll() call will in essence do nothing, since the + ** lock will still be owned by the Thread that called + ** notifyAll(). + ** + ** @exception IllegalMonitorStateException if this Thread + ** does not own the lock on the Object. + **/ + public final native void notifyAll() throws IllegalMonitorStateException; + + /** Waits indefinitely for notify() or notifyAll() to be + ** called on the Object in question. Implementation is + ** identical to wait(0). Most sane implementations just + ** call wait(0). + ** + ** @exception IllegalMonitorStateException if this Thread + ** does not own a lock on this Object. + ** @exception InterruptedException if some other Thread + ** interrupts this Thread. + **/ + public final void wait() throws IllegalMonitorStateException, InterruptedException { + wait(0); + } + + /** Waits a specified amount of time (or indefinitely if + ** the time specified is 0) for someone to call notify() + ** or notifyAll() on this Object, waking up this Thread.<P> + ** + ** The Thread that calls wait() loses all locks it has + ** when this method is called. They are restored when + ** the method completes (even if it completes + ** abnormally).<P> + ** + ** If another Thread interrupts this Thread, the method + ** will terminate with an InterruptedException.<P> + ** + ** The Thread that calls wait() must have a lock on this + ** Object.<P> + ** + ** The waiting period is actually only *roughly* the + ** amount of time you requested. It cannot be exact + ** because of the overhead of the call itself. + ** + ** @param ms the number of milliseconds to wait (1000 + ** milliseconds = 1 second). + ** @exception IllegalMonitorStateException if this Thread + ** does not own a lock on this Object. + ** @exception InterruptedException if some other Thread + ** interrupts this Thread. + **/ + public final native void wait(long ms) throws IllegalMonitorStateException, InterruptedException; + + /** Waits a specified amount of time for notify() or + ** notifyAll() to be called on this Object. This call + ** behaves almost identically to wait(int ms), except it + ** throws nanoseconds into the pot. It's fairly useless, + ** though; if we can only roughly estimate the number of + ** milliseconds to wait, how do you think we can exactly + ** deal with nanoseconds? + ** @param ms the number of milliseconds to wait (1,000 + ** milliseconds = 1 second). + ** @param ns the number of nanoseconds to wait over and + ** above ms (1,000,000 nanoseconds = 1 second). + ** @exception IllegalMonitorStateException if this Thread + ** does not own a lock on this Object. + ** @exception InterruptedException if some other Thread + ** interrupts this Thread. + **/ + public final native void wait(long ms, int ns) throws IllegalMonitorStateException, InterruptedException; + + private native Object nativeClone(); +} diff --git a/java/lang/Runtime.java b/java/lang/Runtime.java new file mode 100644 index 000000000..54205932a --- /dev/null +++ b/java/lang/Runtime.java @@ -0,0 +1,224 @@ +/* + * java.lang.Runtime: part of the Java Class Libraries project. + * Copyright (C) 1998 John Keiser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +import java.util.*; +import java.io.*; + +/** + ** Runtime represents the Virtual Machine. + ** + ** @author John Keiser + ** @version 1.1.0, Aug 8 1998 + **/ + +public class Runtime { + private static Runtime currentRuntime = new Runtime(); + + /* This class is only instantiable by the VM */ + private Runtime() { + } + + /** Get the current Runtime object for this JVM. + ** @return the current Runtime object + **/ + public static Runtime getRuntime() { + return currentRuntime; + } + + /** Exit the Java runtime. This method will either throw + ** a SecurityException or it will never return. + ** @param status the status to exit with + ** @exception SecurityException if + ** System.getSecurityManager().checkExit(status) + ** fails. + **/ + public void exit(int status) { + try { + System.getSecurityManager().checkExit(status); + } catch(NullPointerException e) { + } + nativeExit(status); + } + + /** Run the garbage collector. + ** This method is more of a suggestion than anything. + ** All this method guarantees is that the garbage + ** collector will have "done its best" by the time + ** it returns. + **/ + public native void gc(); + + /** Run finalization on all Objects that are waiting to be + ** finalized. Again, a suggestion, though a stronger + ** one. + **/ + public native void runFinalization(); + + /** Tell the VM to run the finalize() method on every + ** single Object before it exits. Note that the JVM may + ** still exit abnormally and not perform this, so you + ** still don't have a guarantee. This value defaults to + ** <CODE>false</CODE>. + ** @param finalizeOnExit whether to finalize all Objects + ** before the JVM exits + **/ + public native void runFinalizersOnExit(boolean finalizeOnExit); + + /** Load a native library using the system-dependent + ** filename. + ** @exception SecurityException if + ** System.getSecurityManager().checkLink(filename) + ** fails. + ** @exception UnsatisfiedLinkError if the library is not + ** found. + **/ + public void load(String filename) { + try { + System.getSecurityManager().checkLink(filename); + } catch(NullPointerException e) { + } + nativeLoad(filename); + } + + /** Load a native library using a system-independent "short + ** name" for the library. It will be transformed to a + ** correct filename in a system-dependent manner (for + ** example, in Windows, "mylib" will be turned into + ** "mylib.dll") and then passed to load(filename). + ** @exception SecurityException if + ** System.getSecurityManager().checkLink(filename) + ** fails. + ** @exception UnsatisfiedLinkError if the library is not + ** found. + **/ + public void loadLibrary(String libname) { + load(getLibFilename(libname)); + } + + /** Create a new subprocess with the specified command + ** line. Calls exec(cmdline, null). + ** @param cmdline the command to call + ** @exception SecurityException if you cannot call this + ** command + **/ + public Process exec(String cmdline) { + return exec(cmdline, null); + } + + /** Create a new subprocess with the specified command + ** line and environment. Parses the command line into + ** pieces using StringTokenizer and then calls exec(cmd,env) + ** @param cmdline the command to call + ** @exception SecurityException if you cannot call this + ** command + **/ + public Process exec(String cmdline, String[] env) { + StringTokenizer t = new StringTokenizer(cmdline); + Vector v = new Vector(); + while(t.hasMoreTokens()) { + v.addElement(t.nextElement()); + } + String[] cmd = new String[v.size()]; + v.copyInto(cmd); + return exec(cmd, env); + } + + /** Create a new subprocess with the specified command + ** line. Calls exec(cmd,null). + ** @param cmd the command line, already separated + ** @exception SecurityException if you cannot call this + ** command. + **/ + public Process exec(String[] cmd) { + return exec(cmd,null); + } + + /** Create a new subprocess with the specified command + ** line. Calls exec(cmd,null). + ** @param cmd the command line, already separated + ** @exception SecurityException if you cannot call this + ** command (checks using + ** <CODE>System.getSecuritymanager().checkExec(cmd[0])</CODE>. + **/ + public Process exec(String[] cmd, String[] env) { + try { + System.getSecurityManager().checkExec(cmd[0]); + } catch(NullPointerException e) { + } + return nativeExec(cmd,env); + } + + /** Find out how much memory is still free for allocating + ** Objects on the heap. + ** @return the amount of free memory for more Objects. + **/ + public native long freeMemory(); + + /** Find out how much memory total is available on the + ** heap for allocating Objects. + ** @return the total amount of memory for Objects. + **/ + public native long totalMemory(); + + /** Tell the VM to trace every bytecode instruction that + ** executes (print out a trace of it). No guarantees + ** are made as to where it will be printed, and the VM is + ** allowed to ignore this request. + ** @param on whether to turn instruction tracing on + **/ + public native void traceInstructions(boolean on); + + /** Tell the VM to trace every method call that executes + ** (print out a trace of it). No guarantees are made as + ** to where it will be printed, and the VM is allowed to + ** ignore this request. + ** @param on whether to turn method tracing on + **/ + public native void traceMethodCalls(boolean on); + + /** Return a localized version of this InputStream, + ** meaning all characters are localized before they come + ** out the other end. + ** @XXX I must confess I have absolutely no idea how to + ** do this, and the thing is deprecated now anyway, + ** so I await Mr. Localization to work on it. + **/ + public InputStream getLocalizedInputStream(InputStream in) { + return in; + } + + /** Return a localized version of this InputStream, + ** meaning all characters are localized before they come + ** out the other end. + ** @XXX I must confess I have absolutely no idea how to + ** do this, and the thing is deprecated now anyway, + ** so I await Mr. Localization to work on it. + **/ + public OutputStream getLocalizedOutputStream(OutputStream out) { + return out; + } + + private native void nativeExit(int status); + private native void nativeLoad(String filename); + private native Process nativeExec(String[] cmd, String[] env); + private native String getLibFilename(String libname); +} diff --git a/java/lang/System.java b/java/lang/System.java new file mode 100644 index 000000000..9542915d8 --- /dev/null +++ b/java/lang/System.java @@ -0,0 +1,280 @@ +/* + * java.lang.System: part of the Java Class Libraries project. + * Copyright (C) 1998 John Keiser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +import java.io.*; +import java.util.*; + +/** + ** System represents system-wide resources; things that + ** represent the general environment. As such, all + ** methods are static. + ** + ** @author John Keiser + ** @version 1.1.0, Aug 8 1998 + **/ + +public class System { + /* This class is uninstantiable. */ + private System() { } + + private static SecurityManager securityManager; + private static Properties properties; + + /** The standard InputStream. This is assigned at + ** startup and starts its life perfectly valid.<P> + ** This corresponds to the C stdin and C++ cin + ** variables, which typically input from the keyboard, + ** but may be used to pipe input from other processes + ** or files. That should all be transparent to you, + ** however. + **/ + public static final InputStream in = new FileInputStream(FileDescriptor.in); + + /** The standard output PrintStream. This is assigned at + ** startup and starts its life perfectly valid.<P> + ** This corresponds to the C stdout and C++ cout + ** variables, which typically output to the screen, + ** but may be used to pipe output to other processes + ** or files. That should all be transparent to you, + ** however. + **/ + public static final PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out)); + + /** The standard error PrintStream. This is assigned at + ** startup and starts its life perfectly valid.<P> + ** This corresponds to the C stdout and C++ cout + ** variables, which typically output to the screen, + ** but may be used to pipe output to other processes + ** or files. That should all be transparent to you, + ** however. + **/ + public static final PrintStream err = new PrintStream(new FileOutputStream(FileDescriptor.err)); + + /** Set in to a new InputStream. + ** @param in the new InputStream. + ***/ + public static native void setIn(InputStream in); + + /** Set out to a new PrintStream. + ** @param out the new PrintStream. + ***/ + public static native void setOut(PrintStream out); + + /** Set err to a new PrintStream. + ** @param err the new PrintStream. + ***/ + public static native void setErr(PrintStream err); + + /** Get the current SecurityManager. + ** If the SecurityManager has not been set yet, then this + ** method returns null. + ** @return the current SecurityManager, or null. + **/ + public static SecurityManager getSecurityManager() { + return securityManager; + } + + /** Set the current SecurityManager. + ** This may only be done once. If you try to re-set the + ** current SecurityManager, then you will get a + ** SecurityException. If you use null and there is no + ** SecurityManager set, then the state will not + ** change.<P> + ** <STRONG>Spec Note:</STRONG> Don't ask me, I didn't + ** write it. It looks pretty vulnerable; whoever gets to + ** the gate first gets to set the policy. + ** @param securityManager the new SecurityManager. + ** @exception SecurityException if the SecurityManger is + ** already set. + **/ + public static void setSecurityManager(SecurityManager securityManager) { + if(securityManager != null) { + throw new SecurityException("Security Manager already set"); + } + System.securityManager = securityManager; + } + + /** Get the current time, measured in the number of + ** milliseconds from the beginning of Jan. 1, 1970. + ** This is gathered from the system clock, with any + ** attendant incorrectness (it may be timezone + ** dependent). + ** @return the current time. + **/ + public static native long currentTimeMillis(); + + /** Copy one array onto another from + ** <CODE>src[srcStart] ... src[srcStart+len]</CODE> to + ** <CODE>dest[destStart] ... dest[destStart+len]</CODE> + ** @param src the array to copy elements from + ** @param srcStart the starting position to copy elements + ** from in the src array + ** @param dest the array to copy elements to + ** @param destStart the starting position to copy + ** elements from in the src array + ** @param len the number of elements to copy + ** @exception ArrayStoreException if src or dest is not + ** an array, or if one is a primitive type + ** and the other is a reference type or a + ** different primitive type. The array will + ** not be modified if any of these is the + ** case. If there is an element in src that + ** is not assignable to dest's type, this will + ** be thrown and all elements up to but not + ** including that element will have been + ** modified. + ** @exception ArrayIndexOutOfBoundsException if len is + ** negative, or if the start or end copy + ** position in either array is out of bounds. + ** The array will not be modified if this + ** exception is thrown. + **/ + public static native void arraycopy(Object src, int srcStart, Object dest, int destStart, int len); + + /** Get a hash code computed by the VM for the Object. + ** This hash code will be the same as Object's hashCode() + ** method. It is usually some convolution of the pointer + ** to the Object internal to the VM. It follows standard + ** hash code rules, in that it will remain the same for a + ** given Object for the lifetime of that Object. + ** @param o the Object to get the hash code for + ** @return the VM-dependent hash code for this Object + **/ + public static native int identityHashCode(Object o); + + /** Get all the system properties at once. + ** @XXX list the standard system properties + ** @return the system properties + ** @exception SecurityException if thrown by + ** getSecurityManager().checkPropertiesAccess() + **/ + public static Properties getProperties() { + try { + getSecurityManager().checkPropertiesAccess(); + } catch(NullPointerException e) { + } + return properties; + } + + /** Set all the system properties at once. + ** @param properties the new set of system properties. + ** @exception SecurityException if thrown by + ** getSecurityManager().checkPropertiesAccess() + **/ + public static void setProperties(Properties properties) { + try { + getSecurityManager().checkPropertiesAccess(); + } catch(NullPointerException e) { + } + System.properties = properties; + } + + /** Get a single system property by name. + ** @param name the name of the system property to get + ** @return the property, or null if not found. + ** @exception SecurityException if thrown by + ** getSecurityManager().checkPropertyAccess(name) + **/ + public static String getProperty(String name) { + try { + getSecurityManager().checkPropertyAccess(name); + } catch(NullPointerException e) { + } + return properties.getProperty(name); + } + + /** Get a single property by name, with a possible default + ** value returned if not found. + ** @param name the name of the system property to set + ** @param def the default value to use if the + ** property does not exist. + ** @return the property, or default if not found. + ** @exception SecurityException if thrown by + ** getSecurityManager().checkPropertyAccess(name) + **/ + public static String getProperty(String name, String def) { + try { + getSecurityManager().checkPropertyAccess(name); + } catch(NullPointerException e) { + } + return properties.getProperty(name,def); + } + + /** Get a single property by name. Calls getProperty(name). + ** @deprecated use getProperty(name). + ** @see #getProperty(java.lang.String) + **/ + public static String getenv(String name) { + return getProperty(name); + } + + /** Helper method to exit the Java runtime using + ** <CODE>Runtime.getRuntime().exit()</CODE>. + ** @see java.lang.Runtime#exit(int) + **/ + public static void exit(int status) { + Runtime.getRuntime().exit(status); + } + + /** Helper method to run the garbage collector using + ** <CODE>Runtime.getRuntime().gc()</CODE>. + ** @see java.lang.Runtime#gc() + **/ + public static void gc() { + Runtime.getRuntime().gc(); + } + + /** Helper method to run finalization using + ** <CODE>Runtime.getRuntime().runFinalization()</CODE>. + ** @see java.lang.Runtime#runFinalization() + **/ + public static void runFinalization() { + Runtime.getRuntime().runFinalization(); + } + + /** Tell the Runtime whether to run finalization before + ** exiting the JVM. Just uses + ** <CODE>Runtime.getRuntime().runFinalizersOnExit()</CODE>. + ** @see java.lang.Runtime#runFinalizersOnExit() + **/ + public static void runFinalizersOnExit(boolean finalizeOnExit) { + Runtime.getRuntime().runFinalizersOnExit(finalizeOnExit); + } + + /** Helper method to load a library using its explicit + ** system-dependent filename. This just calls + ** <CODE>Runtime.getRuntime().load(filename)</CODE>. + ** @see java.lang.Runtime#load(java.lang.String) + **/ + public static void load(String filename) { + Runtime.getRuntime().load(filename); + } + + /** Helper method to load a library using just a + ** short identifier for the name. This just calls + ** <CODE>Runtime.getRuntime().loadLibrary(libname)</CODE>. + ** @see java.lang.Runtime#loadLibrary(java.lang.String) + **/ + public static void loadLibrary(String libname) { + Runtime.getRuntime().loadLibrary(libname); + } +} |