From 554fd8c5195424bdbcabf5de30fdc183aba391bd Mon Sep 17 00:00:00 2001 From: upstream source tree Date: Sun, 15 Mar 2015 20:14:05 -0400 Subject: obtained gcc-4.6.4.tar.bz2 from upstream website; verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository. --- .../java/lang/reflect/ClassSignatureParser.java | 92 +++ .../java/lang/reflect/FieldSignatureParser.java | 103 ++++ .../java/lang/reflect/GenericSignatureParser.java | 631 +++++++++++++++++++++ .../java/lang/reflect/MethodSignatureParser.java | 167 ++++++ .../classpath/gnu/java/lang/reflect/TypeImpl.java | 63 ++ .../gnu/java/lang/reflect/TypeSignature.java | 290 ++++++++++ .../classpath/gnu/java/lang/reflect/package.html | 46 ++ 7 files changed, 1392 insertions(+) create mode 100644 libjava/classpath/gnu/java/lang/reflect/ClassSignatureParser.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/FieldSignatureParser.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/GenericSignatureParser.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/MethodSignatureParser.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/TypeImpl.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/TypeSignature.java create mode 100644 libjava/classpath/gnu/java/lang/reflect/package.html (limited to 'libjava/classpath/gnu/java/lang/reflect') diff --git a/libjava/classpath/gnu/java/lang/reflect/ClassSignatureParser.java b/libjava/classpath/gnu/java/lang/reflect/ClassSignatureParser.java new file mode 100644 index 000000000..31f28385f --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/ClassSignatureParser.java @@ -0,0 +1,92 @@ +/* ClassSignatureParser.java + Copyright (C) 2005 + Free Software Foundation + +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.*; +import java.util.ArrayList; + +public class ClassSignatureParser extends GenericSignatureParser +{ + private TypeVariable[] typeParameters; + private Type superclassType; + private Type[] interfaceTypes; + + public ClassSignatureParser(Class c, String signature) + { + super(c, c.getClassLoader(), signature); + + if (peekChar() == '<') + { + typeParameters = readFormalTypeParameters(); + } + else + { + typeParameters = new TypeVariable[0]; + } + // SuperclassSignature + superclassType = readClassTypeSignature(); + ArrayList interfaces = new ArrayList(); + while (peekChar() == 'L') + { + // SuperinterfaceSignature + interfaces.add(readClassTypeSignature()); + } + interfaceTypes = new Type[interfaces.size()]; + interfaces.toArray(interfaceTypes); + end(); + } + + public TypeVariable[] getTypeParameters() + { + TypeImpl.resolve(typeParameters); + return typeParameters; + } + + public Type getSuperclassType() + { + superclassType = TypeImpl.resolve(superclassType); + return superclassType; + } + + public Type[] getInterfaceTypes() + { + TypeImpl.resolve(interfaceTypes); + return interfaceTypes; + } +} diff --git a/libjava/classpath/gnu/java/lang/reflect/FieldSignatureParser.java b/libjava/classpath/gnu/java/lang/reflect/FieldSignatureParser.java new file mode 100644 index 000000000..16622d33f --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/FieldSignatureParser.java @@ -0,0 +1,103 @@ +/* FieldSignatureParser.java + Copyright (C) 2005 + Free Software Foundation + +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.GenericSignatureFormatError; +import java.lang.reflect.Type; + +public final class FieldSignatureParser extends GenericSignatureParser +{ + private Type type; + + public FieldSignatureParser(Class container, String signature) + { + super(container, container.getClassLoader(), signature); + + switch (peekChar()) + { + case 'L': + case '[': + case 'T': + type = readFieldTypeSignature(); + break; + case 'Z': + consume('Z'); + type = boolean.class; + break; + case 'B': + consume('B'); + type = byte.class; + break; + case 'S': + consume('S'); + type = short.class; + break; + case 'C': + consume('C'); + type = char.class; + break; + case 'I': + consume('I'); + type = int.class; + break; + case 'F': + consume('F'); + type = float.class; + break; + case 'J': + consume('J'); + type = long.class; + break; + case 'D': + consume('D'); + type = double.class; + break; + default: + throw new GenericSignatureFormatError(); + } + + end(); + } + + public Type getFieldType() + { + type = TypeImpl.resolve(type); + return type; + } +} diff --git a/libjava/classpath/gnu/java/lang/reflect/GenericSignatureParser.java b/libjava/classpath/gnu/java/lang/reflect/GenericSignatureParser.java new file mode 100644 index 000000000..e413c76c7 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/GenericSignatureParser.java @@ -0,0 +1,631 @@ +/* GenericSignatureParser.java + Copyright (C) 2005 + Free Software Foundation + +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 gnu.java.lang.CPStringBuilder; + +import java.lang.reflect.Constructor; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.GenericDeclaration; +import java.lang.reflect.GenericSignatureFormatError; +import java.lang.reflect.MalformedParameterizedTypeException; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.lang.reflect.WildcardType; + +import java.util.ArrayList; +import java.util.Arrays; + +final class TypeVariableImpl extends TypeImpl implements TypeVariable +{ + private GenericDeclaration decl; + private Type[] bounds; + private String name; + + TypeVariableImpl(GenericDeclaration decl, Type[] bounds, String name) + { + this.decl = decl; + this.bounds = bounds; + this.name = name; + } + + Type resolve() + { + return this; + } + + public Type[] getBounds() + { + resolve(bounds); + return (Type[]) bounds.clone(); + } + + public GenericDeclaration getGenericDeclaration() + { + return decl; + } + + public String getName() + { + return name; + } + + public boolean equals(Object obj) + { + if (obj instanceof TypeVariableImpl) + { + TypeVariableImpl other = (TypeVariableImpl)obj; + return decl.equals(other.decl) && name.equals(other.name); + } + return false; + } + + public int hashCode() + { + return 0x5f4d5156 ^ decl.hashCode() ^ name.hashCode(); + } + + public String toString() + { + return name; + } +} + +final class ParameterizedTypeImpl extends TypeImpl implements ParameterizedType +{ + private String rawTypeName; + private ClassLoader loader; + private Class rawType; + private Type owner; + private Type[] typeArgs; + + ParameterizedTypeImpl(String rawTypeName, ClassLoader loader, Type owner, + Type[] typeArgs) + { + this.rawTypeName = rawTypeName; + this.loader = loader; + this.owner = owner; + this.typeArgs = typeArgs; + } + + Type resolve() + { + if (rawType == null) + { + try + { + rawType = Class.forName(rawTypeName, false, loader); + } + catch (ClassNotFoundException x) + { + throw new TypeNotPresentException(rawTypeName, x); + } + } + if (typeArgs == null) + { + if (owner == null) + { + return rawType; + } + typeArgs = new Type[0]; + } + resolve(typeArgs); + owner = resolve(owner); + return this; + } + + public Type[] getActualTypeArguments() + { + return (Type[]) typeArgs.clone(); + } + + public Type getRawType() + { + return rawType; + } + + public Type getOwnerType() + { + return owner; + } + + public boolean equals(Object obj) + { + if (obj instanceof ParameterizedTypeImpl) + { + ParameterizedTypeImpl other = (ParameterizedTypeImpl)obj; + return rawType.equals(other.rawType) + && ((owner == null && other.owner == null) + || owner.equals(other.owner)) + && Arrays.deepEquals(typeArgs, other.typeArgs); + } + return false; + } + + public int hashCode() + { + int h = 0x58158970 ^ rawType.hashCode(); + if (owner != null) + { + h ^= Integer.reverse(owner.hashCode()); + } + for (int i = 0; i < typeArgs.length; i++) + { + h ^= Integer.rotateLeft(typeArgs[i].hashCode(), i); + } + return h; + } + + public String toString() + { + CPStringBuilder sb = new CPStringBuilder(); + if (owner != null) + { + sb.append(owner); + sb.append('.'); + sb.append(rawType.getSimpleName()); + } + else + { + sb.append(rawTypeName); + } + if (typeArgs.length > 0) + { + sb.append('<'); + for (int i = 0; i < typeArgs.length; i++) + { + if (i > 0) + sb.append(", "); + if (typeArgs[i] instanceof Class) + { + sb.append(((Class)typeArgs[i]).getName()); + } + else + { + sb.append(typeArgs[i]); + } + } + sb.append('>'); + } + return sb.toString(); + } +} + +final class GenericArrayTypeImpl extends TypeImpl implements GenericArrayType +{ + private Type componentType; + + GenericArrayTypeImpl(Type componentType) + { + this.componentType = componentType; + } + + Type resolve() + { + componentType = resolve(componentType); + return this; + } + + public Type getGenericComponentType() + { + return componentType; + } + + public boolean equals(Object obj) + { + if (obj instanceof GenericArrayTypeImpl) + { + GenericArrayTypeImpl other = (GenericArrayTypeImpl)obj; + return componentType.equals(other.componentType); + } + return false; + } + + public int hashCode() + { + return 0x4be37a7f ^ componentType.hashCode(); + } + + public String toString() + { + return componentType + "[]"; + } +} + +final class UnresolvedTypeVariable extends TypeImpl implements Type +{ + private GenericDeclaration decl; + private String name; + + UnresolvedTypeVariable(GenericDeclaration decl, String name) + { + this.decl = decl; + this.name = name; + } + + Type resolve() + { + GenericDeclaration d = decl; + while (d != null) + { + for (TypeVariable t : d.getTypeParameters()) + { + if (t.getName().equals(name)) + { + return t; + } + } + d = getParent(d); + } + throw new MalformedParameterizedTypeException(); + } + + private static GenericDeclaration getParent(GenericDeclaration d) + { + if (d instanceof Class) + { + Method m = ((Class)d).getEnclosingMethod(); + if (m != null) + { + return m; + } + Constructor c = ((Class)d).getEnclosingConstructor(); + if (c != null) + { + return c; + } + return ((Class)d).getEnclosingClass(); + } + else if (d instanceof Method) + { + return ((Method)d).getDeclaringClass(); + } + else if (d instanceof Constructor) + { + return ((Constructor)d).getDeclaringClass(); + } + else + { + // TODO figure out what this represents + throw new Error(); + } + } +} + +final class WildcardTypeImpl extends TypeImpl implements WildcardType +{ + private Type lower; + private Type upper; + + WildcardTypeImpl(Type lower, Type upper) + { + this.lower = lower; + this.upper = upper; + } + + Type resolve() + { + upper = resolve(upper); + lower = resolve(lower); + return this; + } + + public Type[] getUpperBounds() + { + if (upper == null) + { + return new Type[0]; + } + return new Type[] { upper }; + } + + public Type[] getLowerBounds() + { + if (lower == null) + { + return new Type[0]; + } + return new Type[] { lower }; + } + + public boolean equals(Object obj) + { + if (obj instanceof WildcardTypeImpl) + { + WildcardTypeImpl other = (WildcardTypeImpl)obj; + return Arrays.deepEquals(getUpperBounds(), other.getUpperBounds()) + && Arrays.deepEquals(getLowerBounds(), other.getLowerBounds()); + } + return false; + } + + public int hashCode() + { + int h = 0x75d074fd; + if (upper != null) + { + h ^= upper.hashCode(); + } + if (lower != null) + { + h ^= lower.hashCode(); + } + return h; + } + + public String toString() + { + if (lower != null) + { + return "? super " + lower; + } + if (upper == java.lang.Object.class) + { + return "?"; + } + return "? extends " + upper; + } +} + +class GenericSignatureParser +{ + private ClassLoader loader; + private GenericDeclaration container; + private String signature; + private int pos; + + GenericSignatureParser(GenericDeclaration container, ClassLoader loader, + String signature) + { + this.container = container; + this.loader = loader; + this.signature = signature; + } + + TypeVariable[] readFormalTypeParameters() + { + consume('<'); + ArrayList params = new ArrayList(); + do + { + // TODO should we handle name clashes? + params.add(readFormalTypeParameter()); + } while (peekChar() != '>'); + consume('>'); + TypeVariable[] list = new TypeVariable[params.size()]; + params.toArray(list); + return list; + } + + private TypeVariable readFormalTypeParameter() + { + String identifier = readIdentifier(); + consume(':'); + ArrayList bounds = new ArrayList(); + if (peekChar() != ':') + { + bounds.add(readFieldTypeSignature()); + } + while (peekChar() == ':') + { + consume(':'); + bounds.add(readFieldTypeSignature()); + } + Type[] b = new Type[bounds.size()]; + bounds.toArray(b); + return new TypeVariableImpl(container, b, identifier); + } + + Type readFieldTypeSignature() + { + switch (peekChar()) + { + case 'L': + return readClassTypeSignature(); + case '[': + return readArrayTypeSignature(); + case 'T': + return readTypeVariableSignature(); + default: + throw new GenericSignatureFormatError(); + } + } + + Type readClassTypeSignature() + { + consume('L'); + String className = ""; + for (;;) + { + String part = readIdentifier(); + if (peekChar() != '/') + { + className += part; + break; + } + consume('/'); + className += part + "."; + } + Type[] typeArguments = null; + if (peekChar() == '<') + { + typeArguments = readTypeArguments(); + } + Type type = new ParameterizedTypeImpl(className, loader, null, + typeArguments); + while (peekChar() == '.') + { + consume('.'); + className += "$" + readIdentifier(); + typeArguments = null; + if (peekChar() == '<') + { + typeArguments = readTypeArguments(); + } + type = new ParameterizedTypeImpl(className, loader, type, + typeArguments); + } + consume(';'); + return type; + } + + private Type[] readTypeArguments() + { + consume('<'); + ArrayList list = new ArrayList(); + do + { + list.add(readTypeArgument()); + } while ((peekChar() != '>')); + consume('>'); + Type[] arr = new Type[list.size()]; + list.toArray(arr); + return arr; + } + + private Type readTypeArgument() + { + char c = peekChar(); + if (c == '+') + { + consume('+'); + return new WildcardTypeImpl(null, readFieldTypeSignature()); + } + else if (c == '-') + { + consume('-'); + return new WildcardTypeImpl(readFieldTypeSignature(), + java.lang.Object.class); + } + else if (c == '*') + { + consume('*'); + return new WildcardTypeImpl(null, java.lang.Object.class); + } + else + { + return readFieldTypeSignature(); + } + } + + Type readArrayTypeSignature() + { + consume('['); + switch (peekChar()) + { + case 'L': + case '[': + case 'T': + return new GenericArrayTypeImpl(readFieldTypeSignature()); + case 'Z': + consume('Z'); + return boolean[].class; + case 'B': + consume('B'); + return byte[].class; + case 'S': + consume('S'); + return short[].class; + case 'C': + consume('C'); + return char[].class; + case 'I': + consume('I'); + return int[].class; + case 'F': + consume('F'); + return float[].class; + case 'J': + consume('J'); + return long[].class; + case 'D': + consume('D'); + return double[].class; + default: + throw new GenericSignatureFormatError(); + } + } + + Type readTypeVariableSignature() + { + consume('T'); + String identifier = readIdentifier(); + consume(';'); + return new UnresolvedTypeVariable(container, identifier); + } + + private String readIdentifier() + { + int start = pos; + char c; + do + { + readChar(); + c = peekChar(); + } while (";:./<>-+*".indexOf(c) == -1); + return signature.substring(start, pos); + } + + final char peekChar() + { + if (pos == signature.length()) + return '\u0000'; + else + return signature.charAt(pos); + } + + final char readChar() + { + return signature.charAt(pos++); + } + + final void consume(char c) + { + if (readChar() != c) + throw new GenericSignatureFormatError(); + } + + final void end() + { + if (pos != signature.length()) + throw new GenericSignatureFormatError(); + } +} diff --git a/libjava/classpath/gnu/java/lang/reflect/MethodSignatureParser.java b/libjava/classpath/gnu/java/lang/reflect/MethodSignatureParser.java new file mode 100644 index 000000000..50f98e299 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/MethodSignatureParser.java @@ -0,0 +1,167 @@ +/* MethodSignatureParser.java + Copyright (C) 2005 + Free Software Foundation + +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.*; +import java.util.ArrayList; + +public class MethodSignatureParser extends GenericSignatureParser +{ + private TypeVariable[] typeParameters; + private Type[] argTypes; + private Type retType; + private Type[] throwsSigs; + + public MethodSignatureParser(Method method, String signature) + { + this(method, method.getDeclaringClass().getClassLoader(), signature); + } + + public MethodSignatureParser(Constructor method, String signature) + { + this(method, method.getDeclaringClass().getClassLoader(), signature); + } + + private MethodSignatureParser(GenericDeclaration wrapper, + ClassLoader loader, String signature) + { + super(wrapper, loader, signature); + + if (peekChar() == '<') + { + typeParameters = readFormalTypeParameters(); + } + else + { + typeParameters = new TypeVariable[0]; + } + consume('('); + ArrayList args = new ArrayList(); + while (peekChar() != ')') + { + args.add(readTypeSignature()); + } + argTypes = new Type[args.size()]; + args.toArray(argTypes); + consume(')'); + retType = readTypeSignature(); + ArrayList throwsSigs = new ArrayList(); + while (peekChar() == '^') + { + consume('^'); + if(peekChar() == 'T') + { + throwsSigs.add(readTypeVariableSignature()); + } + else + { + throwsSigs.add(readClassTypeSignature()); + } + } + this.throwsSigs = new Type[throwsSigs.size()]; + throwsSigs.toArray(this.throwsSigs); + end(); + } + + public TypeVariable[] getTypeParameters() + { + TypeImpl.resolve(typeParameters); + return typeParameters; + } + + public Type[] getGenericParameterTypes() + { + TypeImpl.resolve(argTypes); + return argTypes; + } + + public Type getGenericReturnType() + { + retType = TypeImpl.resolve(retType); + return retType; + } + + public Type[] getGenericExceptionTypes() + { + TypeImpl.resolve(throwsSigs); + return throwsSigs; + } + + private Type readTypeSignature() + { + switch (peekChar()) + { + case 'T': + return readTypeVariableSignature(); + case 'L': + return readClassTypeSignature(); + case '[': + return readArrayTypeSignature(); + case 'Z': + consume('Z'); + return boolean.class; + case 'B': + consume('B'); + return byte.class; + case 'S': + consume('S'); + return short.class; + case 'C': + consume('C'); + return char.class; + case 'I': + consume('I'); + return int.class; + case 'F': + consume('F'); + return float.class; + case 'J': + consume('J'); + return long.class; + case 'D': + consume('D'); + return double.class; + case 'V': + consume('V'); + return void.class; + default: + throw new GenericSignatureFormatError(); + } + } +} diff --git a/libjava/classpath/gnu/java/lang/reflect/TypeImpl.java b/libjava/classpath/gnu/java/lang/reflect/TypeImpl.java new file mode 100644 index 000000000..30906f629 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/TypeImpl.java @@ -0,0 +1,63 @@ +/* TypeImpl.java + Copyright (C) 2005 + Free Software Foundation + +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.Type; + +abstract class TypeImpl implements Type +{ + abstract Type resolve(); + + static void resolve(Type[] types) + { + for (int i = 0; i < types.length; i++) + { + types[i] = resolve(types[i]); + } + } + + static Type resolve(Type type) + { + if (type instanceof TypeImpl) + { + type = ((TypeImpl) type).resolve(); + } + return type; + } +} diff --git a/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java b/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java new file mode 100644 index 000000000..c0a3ab0a5 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java @@ -0,0 +1,290 @@ +/* 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 gnu.java.lang.CPStringBuilder; + +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 Classs or Members. + * More specific methods are also provided for computing the + * type-signature of Constructors and + * Methods. Methods are also provided to go in the + * reverse direction. + * + * @author Eric Blake (ebb9@email.byu.edu) + */ +public class TypeSignature +{ + /** + * Returns a String 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: + * + *
+   * 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 + ";"
+   * 
+ * + * @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 getEncodingOfClass. 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 null. 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 getEncodingOfClass. 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 + * loader 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 String 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(); + CPStringBuilder buf = new CPStringBuilder("("); + 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 String 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(); + CPStringBuilder buf = new CPStringBuilder("("); + for (int i = 0; i < paramTypes.length; i++) + buf.append(getEncodingOfClass(paramTypes[i].getName(), true)); + buf.append(")V"); + return buf.toString(); + } + + /** + * Returns a String 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 diff --git a/libjava/classpath/gnu/java/lang/reflect/package.html b/libjava/classpath/gnu/java/lang/reflect/package.html new file mode 100644 index 000000000..a837d37d2 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/package.html @@ -0,0 +1,46 @@ + + + + +GNU Classpath - gnu.java.lang.reflect + + +

+ + + -- cgit v1.2.3