Initial revision

From-SVN: r102074
This commit is contained in:
Tom Tromey 2005-07-16 00:30:23 +00:00
parent 6f4434b39b
commit f911ba985a
4557 changed files with 1000262 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/* ArrayHelper.java -- Helper methods for handling array operations
Copyright (C) 1998, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.lang;
/**
* ArrayHelper helps you do things with arrays.
*
* @author John Keiser
*/
public class ArrayHelper
{
/**
* Counterpart to java.util.Collection.contains.
*
* @param array the array to search
* @param searchFor the object to locate
* @return true if some array element <code>equals(searchFor)</code>
*/
public static boolean contains(Object[] array, Object searchFor)
{
return indexOf(array, searchFor) != -1;
}
/**
* Counterpart to java.util.Collection.indexOf.
*
* @param array the array to search
* @param searchFor the object to locate
* @return the index of the first equal object, or -1
*/
public static int indexOf(Object[] array, Object searchFor)
{
for (int i = 0; i < array.length; i++)
{
if(array[i].equals(searchFor))
{
return i;
}
}
return -1;
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,177 @@
/* ClassHelper.java -- Utility methods to augment java.lang.Class
Copyright (C) 1998, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.lang;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* ClassHelper has various methods that ought to have been in Class.
*
* @author John Keiser
* @author Eric Blake (ebb9@email.byu.edu)
*/
public class ClassHelper
{
/**
* Strip the package part from the class name.
*
* @param clazz the class to get the truncated name from
* @return the truncated class name
*/
public static String getTruncatedClassName(Class clazz)
{
return getTruncatedName(clazz.getName());
}
/**
* Strip the package part from the class name, or the class part from
* the method or field name.
*
* @param name the name to truncate
* @return the truncated name
*/
public static String getTruncatedName(String name)
{
int lastInd = name.lastIndexOf('.');
if (lastInd == -1)
return name;
return name.substring(lastInd + 1);
}
/** Cache of methods found in getAllMethods(). */
private static Map allMethods = new HashMap();
/**
* Get all the methods, public, private and otherwise, from the class,
* getting them from the most recent class to find them. This may not
* be quite the correct approach, as this includes methods that are not
* inherited or accessible from clazz, so beware.
*
* @param clazz the class to start at
* @return all methods declared or inherited in clazz
*/
public static Method[] getAllMethods(Class clazz)
{
Method[] retval = (Method[]) allMethods.get(clazz);
if (retval == null)
{
Set methods = new HashSet();
Class c = clazz;
while (c != null)
{
Method[] currentMethods = c.getDeclaredMethods();
loop:
for (int i = 0; i < currentMethods.length; i++)
{
Method current = currentMethods[i];
int size = methods.size();
Iterator iter = methods.iterator();
while (--size >= 0)
{
Method override = (Method) iter.next();
if (current.getName().equals(override.getName())
&& Arrays.equals(current.getParameterTypes(),
override.getParameterTypes())
&& current.getReturnType() == override.getReturnType())
continue loop;
}
methods.add(current);
}
c = c.getSuperclass();
}
retval = new Method[methods.size()];
methods.toArray(retval);
allMethods.put(clazz, retval);
}
return retval;
}
/** Cache of fields found in getAllFields(). */
private static Map allFields = new HashMap();
/**
* Get all the fields, public, private and otherwise, from the class,
* getting them from the most recent class to find them. This may not
* be quite the correct approach, as this includes fields that are not
* inherited or accessible from clazz, so beware.
*
* @param clazz the class to start at
* @return all fields declared or inherited in clazz
*/
public static Field[] getAllFields(Class clazz)
{
Field[] retval = (Field[]) allFields.get(clazz);
if (retval == null)
{
Set fields = new HashSet();
Class c = clazz;
while (c != null)
{
Field[] currentFields = c.getDeclaredFields();
loop:
for (int i = 0; i < currentFields.length; i++)
{
Field current = currentFields[i];
int size = fields.size();
Iterator iter = fields.iterator();
while (--size >= 0)
{
Field override = (Field) iter.next();
if (current.getName().equals(override.getName())
&& current.getType() == override.getType())
continue loop;
}
fields.add(current);
}
c = c.getSuperclass();
}
retval = new Field[fields.size()];
fields.toArray(retval);
allFields.put(clazz, retval);
}
return retval;
}
}

View file

@ -0,0 +1,83 @@
/* MainThread.java --
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.lang;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* MainThread is a Thread which uses the main() method of some class.
*
* @author John Keiser
* @author Tom Tromey (tromey@redhat.com)
*/
public class MainThread
{
// Private data.
String[] args;
Method mainMethod;
public MainThread(String classname, String[] args)
throws ClassNotFoundException, NoSuchMethodException
{
Class found = Class.forName(classname, true,
ClassLoader.getSystemClassLoader());
Class[] argTypes = new Class[1];
argTypes[0] = args.getClass();
mainMethod = found.getMethod("main", argTypes);
this.args = args;
}
public void run()
{
try
{
mainMethod.invoke(null,args);
}
catch(IllegalAccessException e)
{
// Ignore.
}
catch(InvocationTargetException e)
{
// Ignore.
}
}
}

View file

@ -0,0 +1,46 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!-- package.html - describes classes in gnu.java.lang package.
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. -->
<html>
<head><title>GNU Classpath - gnu.java.lang</title></head>
<body>
<p></p>
</body>
</html>

View file

@ -0,0 +1,288 @@
/* TypeSignature.java -- Class used to compute type signatures
Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.lang.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
/**
* This class provides static methods that can be used to compute
* type-signatures of <code>Class</code>s or <code>Member</code>s.
* More specific methods are also provided for computing the
* type-signature of <code>Constructor</code>s and
* <code>Method</code>s. Methods are also provided to go in the
* reverse direction.
*
* @author Eric Blake (ebb9@email.byu.edu)
*/
public class TypeSignature
{
/**
* Returns a <code>String</code> representing the type-encoding of a class.
* The .class file format has different encodings for classes, depending
* on whether it must be disambiguated from primitive types or not; hence
* the descriptor parameter to choose between them. If you are planning
* on decoding primitive types along with classes, then descriptor should
* be true for correct results. Type-encodings are computed as follows:
*
* <pre>
* boolean -> "Z"
* byte -> "B"
* char -> "C"
* double -> "D"
* float -> "F"
* int -> "I"
* long -> "J"
* short -> "S"
* void -> "V"
* arrays -> "[" + descriptor format of component type
* object -> class format: fully qualified name with '.' replaced by '/'
* descriptor format: "L" + class format + ";"
* </pre>
*
* @param type the class name to encode
* @param descriptor true to return objects in descriptor format
* @return the class name, as it appears in bytecode constant pools
* @see #getClassForEncoding(String)
*/
public static String getEncodingOfClass(String type, boolean descriptor)
{
if (! descriptor || type.charAt(0) == '[')
return type.replace('.', '/');
if (type.equals("boolean"))
return "Z";
if (type.equals("byte"))
return "B";
if (type.equals("short"))
return "S";
if (type.equals("char"))
return "C";
if (type.equals("int"))
return "I";
if (type.equals("long"))
return "J";
if (type.equals("float"))
return "F";
if (type.equals("double"))
return "D";
if (type.equals("void"))
return "V";
return 'L' + type.replace('.', '/') + ';';
}
/**
* Gets the descriptor encoding for a class.
*
* @param clazz the class to encode
* @param descriptor true to return objects in descriptor format
* @return the class name, as it appears in bytecode constant pools
* @see #getEncodingOfClass(String, boolean)
*/
public static String getEncodingOfClass(Class clazz, boolean descriptor)
{
return getEncodingOfClass(clazz.getName(), descriptor);
}
/**
* Gets the descriptor encoding for a class.
*
* @param clazz the class to encode
* @return the class name, as it appears in bytecode constant pools
* @see #getEncodingOfClass(String, boolean)
*/
public static String getEncodingOfClass(Class clazz)
{
return getEncodingOfClass(clazz.getName(), true);
}
/**
* This function is the inverse of <code>getEncodingOfClass</code>. This
* accepts both object and descriptor formats, but must know which style
* of string is being passed in (usually, descriptor should be true). In
* descriptor format, "I" is treated as int.class, in object format, it
* is treated as a class named I in the unnamed package. This method is
* strictly equivalent to {@link #getClassForEncoding(java.lang.String, boolean, java.lang.ClassLoader)}
* with a class loader equal to <code>null</code>. In that case, it
* uses the default class loader on the calling stack.
*
* @param type_code the class name to decode
* @param descriptor if the string is in descriptor format
* @return the corresponding Class object
* @throws ClassNotFoundException if the class cannot be located
* @see #getEncodingOfClass(Class, boolean)
*/
public static Class getClassForEncoding(String type_code, boolean descriptor)
throws ClassNotFoundException
{
return getClassForEncoding(type_code, descriptor, null);
}
/**
* This function is the inverse of <code>getEncodingOfClass</code>. This
* accepts both object and descriptor formats, but must know which style
* of string is being passed in (usually, descriptor should be true). In
* descriptor format, "I" is treated as int.class, in object format, it
* is treated as a class named I in the unnamed package.
*
* @param type_code The class name to decode.
* @param descriptor If the string is in descriptor format.
* @param loader The class loader when resolving generic object name. If
* <code>loader</code> is null then it uses the default class loader on the
* calling stack.
* @return the corresponding Class object.
* @throws ClassNotFoundException if the class cannot be located.
* @see #getEncodingOfClass(Class, boolean)
* @see #getClassForEncoding(String, boolean)
*/
public static Class getClassForEncoding(String type_code, boolean descriptor,
ClassLoader loader)
throws ClassNotFoundException
{
if (descriptor)
{
switch (type_code.charAt(0))
{
case 'B':
return byte.class;
case 'C':
return char.class;
case 'D':
return double.class;
case 'F':
return float.class;
case 'I':
return int.class;
case 'J':
return long.class;
case 'S':
return short.class;
case 'V':
return void.class;
case 'Z':
return boolean.class;
default:
throw new ClassNotFoundException("Invalid class name: "
+ type_code);
case 'L':
type_code = type_code.substring(1, type_code.length() - 1);
// Fallthrough.
case '[':
}
}
return Class.forName(type_code.replace('/', '.'), true, loader);
}
/**
* Gets the Class object for a type name.
*
* @param type_code the class name to decode
* @return the corresponding Class object
* @throws ClassNotFoundException if the class cannot be located
* @see #getClassForEncoding(String, boolean)
*/
public static Class getClassForEncoding(String type_code)
throws ClassNotFoundException
{
return getClassForEncoding(type_code, true);
}
/**
* Returns a <code>String</code> representing the type-encoding of a
* method. The type-encoding of a method is:
*
* "(" + parameter type descriptors + ")" + return type descriptor
*
* XXX This could be faster if it were implemented natively.
*
* @param m the method to encode
* @return the encoding
*/
public static String getEncodingOfMethod(Method m)
{
Class[] paramTypes = m.getParameterTypes();
StringBuffer buf = new StringBuffer().append('(');
for (int i = 0; i < paramTypes.length; i++)
buf.append(getEncodingOfClass(paramTypes[i].getName(), true));
buf.append(')').append(getEncodingOfClass(m.getReturnType().getName(),
true));
return buf.toString();
}
/**
* Returns a <code>String</code> representing the type-encoding of a
* constructor. The type-encoding of a method is:
*
* "(" + parameter type descriptors + ")V"
*
* XXX This could be faster if it were implemented natively.
*
* @param c the constructor to encode
* @return the encoding
*/
public static String getEncodingOfConstructor(Constructor c)
{
Class[] paramTypes = c.getParameterTypes();
StringBuffer buf = new StringBuffer().append('(');
for (int i = 0; i < paramTypes.length; i++)
buf.append(getEncodingOfClass(paramTypes[i].getName(), true));
buf.append(")V");
return buf.toString();
}
/**
* Returns a <code>String</code> representing the type-encoding of a
* class member. This appropriately handles Constructors, Methods, and
* Fields.
*
* @param mem the member to encode
* @return the encoding
*/
public static String getEncodingOfMember(Member mem)
{
if (mem instanceof Constructor)
return getEncodingOfConstructor((Constructor) mem);
if (mem instanceof Method)
return getEncodingOfMethod((Method) mem);
else // Field
return getEncodingOfClass(((Field) mem).getType().getName(), true);
}
} // class TypeSignature

View file

@ -0,0 +1,46 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!-- package.html - describes classes in gnu.java.lang.reflect package.
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. -->
<html>
<head><title>GNU Classpath - gnu.java.lang.reflect</title></head>
<body>
<p></p>
</body>
</html>