I think you can get the enclosing stuff from the attributes in the
constant pool. See the javassist EnclosingMethodAttribute which
also serves for classes when the index is 0
On Tue, 2006-03-14 at 22:56 -0500, Scott Stark wrote:
> User: starksm
> Date: 06/03/14 22:56:35
>
> Modified: src/main/org/jboss/lang ClassRedirects.java
> Log:
> Add missing jdk5 Class method redirects. Several todos still exist for
> methods.
>
> Revision Changes Path
> 1.4 +252 -19 jbossretro/src/main/org/jboss/lang/ClassRedirects.java
>
> (In the diff below, changes in quantity of whitespace are not shown.)
>
> Index: ClassRedirects.java
> ===================================================================
> RCS file:
> /cvsroot/jboss/jbossretro/src/main/org/jboss/lang/ClassRedirects.java,v
> retrieving revision 1.3
> retrieving revision 1.4
> diff -u -b -r1.3 -r1.4
> --- ClassRedirects.java 15 Feb 2006 22:31:11 -0000 1.3
> +++ ClassRedirects.java 15 Mar 2006 03:56:35 -0000 1.4
> @@ -22,20 +22,29 @@
> package org.jboss.lang;
>
> import java.lang.reflect.Array;
> +import java.lang.reflect.TypeVariable;
> +import java.lang.reflect.Type;
> +import java.lang.reflect.Method;
> +import java.lang.reflect.Constructor;
> import java.util.ArrayList;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
>
> /**
> - * ClassRedirects.
> + * Implementations of java.lang.Class methods added in jdk5
> *
> * @author <a href="[EMAIL PROTECTED]">Adrian Brock</a>
> - * @version $Revision: 1.3 $
> + * @author [EMAIL PROTECTED]
> + * @version $Revision: 1.4 $
> */
> public class ClassRedirects
> {
> /** The enum modifier */
> // private static final int ENUM = 0x00004000;
> + /**
> + * The synthetic modifier
> + */
> + private static final int SYNTHETIC = 0x00001000;
>
> /**
> * Whether the class is an enum
> @@ -50,9 +59,13 @@
> // return false;
> Class superClass = clazz.getSuperclass();
> if (superClass == null)
> + {
> return false;
> + }
> if (Enum.class == clazz.getSuperclass())
> + {
> return true;
> + }
> return superClass.getName().equals("java.lang.Enum");
> }
>
> @@ -60,7 +73,6 @@
> * Get the enumeration constants
> *
> * @param <T> the enum type
> - * @param the array
> * @param enumType the enum type
> * @return the constants
> */
> @@ -82,11 +94,34 @@
> public static <T> T cast(Class<T> clazz, Object o)
> {
> if (o != null && clazz.isInstance(o) == false)
> + {
> throw new ClassCastException("Expected " + clazz.getName() + "
> got " + o.getClass().getName());
> + }
> return (T) o;
> }
>
> /**
> + * @param clazz
> + * @param thisClazz
> + * @return thisClazz object, cast to represent a subclass of
> + * the specified class object.
> + * @throws ClassCastException if thisClazz object does not
> + * represent a subclass of the specified class (here "subclass"
> includes
> + * the class itself).
> + */
> + public static <T> Class<? extends T> asSubclass(Class<T> clazz, Class
> thisClazz)
> + {
> + if (clazz.isAssignableFrom(thisClazz))
> + {
> + return (Class<? extends T>) thisClazz;
> + }
> + else
> + {
> + throw new ClassCastException("Expected " + clazz.getName() + "
> got " + thisClazz.getName());
> + }
> + }
> +
> + /**
> * Replace a character sequence
> *
> * @param string the string
> @@ -125,16 +160,214 @@
> for (char c : string.toCharArray())
> {
> if (c == '\\')
> + {
> builder.append("\\\\");
> + }
> else if (c == '$')
> + {
> builder.append("\\$");
> + }
> else
> + {
> builder.append(c);
> }
> + }
> return builder.toString();
> }
>
> /**
> + * Implementation of the jdk5 Class.getSimpleName() method.
> + * Returns the simple name of the underlying class as given in the
> + * source code. Returns an empty string if the underlying class is
> + * anonymous.
> + * <p/>
> + * <p>The simple name of an array is the simple name of the
> + * component type with "[]" appended. In particular the simple
> + * name of an array whose component type is anonymous is "[]".
> + *
> + * @param clazz the Class instance
> + * @return the simple name of the underlying class
> + */
> + public static String getSimpleName(Class clazz)
> + {
> + boolean isArray = clazz.isArray();
> + if (isArray)
> + {
> + String arrayName = getSimpleName(clazz.getComponentType())+"[]";
> + return arrayName;
> + }
> + // Remove the package name
> + String name = clazz.getName();
> + int dot = name.lastIndexOf('.');
> + if( dot >= 0 )
> + {
> + name = name.substring(dot+1);
> + // Check for inner class
> + int dollar = name.lastIndexOf('$');
> + if( dollar >= 0 )
> + {
> + name = name.substring(dollar+1);
> + // Check for anonymous $N classes (N=0-9)
> + int index = 0;
> + for(; index < name.length() &&
> Character.isDigit(name.charAt(index)); index ++)
> + ;
> + if( index > 0 )
> + name = name.substring(index);
> + }
> + }
> + return name;
> + }
> +
> + /**
> + * Returns the canonical name of the the underlying class as
> + * defined by the Java Language Specification. Returns null if
> + * the underlying class does not have a canonical name (i.e., if
> + * it is a local or anonymous class or an array whose component
> + * type does not have a canonical name).
> + *
> + * The difference between a fully qualified name and a canonical name
> can be
> + * seen in examples such as:
> + package p;
> + class O1 { class I{}}
> + class O2 extends O1{};
> + * In this example both p.O1.I and p.O2.I are fully qualified names that
> + * denote the same class, but only p.O1.I is its canonical name.
> + *
> + * @param clazz the Class instance
> + * @return the canonical name of the underlying class if it exists, and
> + * null otherwise.
> + */
> + public static String getCanonicalName(Class clazz)
> + {
> + if (clazz.isArray())
> + {
> + String name = clazz.getComponentType().getCanonicalName();
> + if( name != null )
> + name += "[]";
> + return name;
> + }
> + String name = null;
> + if( isAnonymousClass(clazz) == false )
> + {
> + // this is not correct for nested classes?
> + name = clazz.getName();
> + }
> + return name;
> + }
> +
> + /**
> + * Returns true if and only if the underlying class
> + * is an anonymous class.
> + *
> + * @param clazz the Class instance
> + * @return true if clazz is an anonymous class.
> + * @since 1.5
> + */
> + public static boolean isAnonymousClass(Class clazz)
> + {
> + String name = getSimpleName(clazz);
> + return name.length() == 0;
> + }
> +
> + /**
> + * @param clazz
> + * @return true if (clazz.getModifiers() & SYNTHETIC) != 0
> + * @todo
> + */
> + public static boolean isSynthetic(Class clazz)
> + {
> + throw new UnsupportedOperationException("isSynthetic is a TODO");
> + }
> +
> + /**
> + * @param clazz the Class instance
> + * @return true if and only if this class is a local class.
> + * @todo Returns true if and only if the underlying class
> + * is a local class.
> + * @since 1.5
> + */
> + public static boolean isLocalClass(Class clazz)
> + {
> + throw new UnsupportedOperationException("isLocalClass is a TODO");
> + }
> +
> + /**
> + * @param clazz the Class instance
> + * @return true if and only if this class is a member class.
> + * @todo Returns true if and only if the underlying class
> + * is a member class.
> + * @since 1.5
> + */
> + public static boolean isMemberClass(Class clazz)
> + {
> + throw new UnsupportedOperationException("isMemberClass is a TODO");
> + }
> +
> + /**
> + * @param clazz
> + * @return an array of TypeVariable objects that represent
> + * the type variables declared by this generic declaration
> + * @todo
> + */
> + public static <T> TypeVariable<Class<T>>[] getTypeParameters(Class
> clazz)
> + {
> + throw new UnsupportedOperationException("getTypeParameters is a
> TODO");
> + }
> +
> + /**
> + * @param clazz
> + * @return the superclass of the class represented by this object
> + * @todo
> + */
> + public static Type getGenericSuperclass(Class clazz)
> + {
> + throw new UnsupportedOperationException("getGenericSuperclass is a
> TODO");
> + }
> +
> + /**
> + * @param clazz
> + * @return an array of interfaces implemented by this class
> + * @todo
> + */
> + public static Type[] getGenericInterfaces(Class clazz)
> + {
> + throw new UnsupportedOperationException("getGenericInterfaces is a
> TODO");
> + }
> +
> + /**
> + * This method cannot be implemented as it relies on native method info.
> + *
> + * @param clazz
> + * @return null
> + */
> + public static Method getEnclosingMethod(Class clazz)
> + {
> + throw new UnsupportedOperationException("getEnclosingMethod cannot
> be implemented");
> + }
> +
> + /**
> + * This method cannot be implemented as it relies on native method info.
> + *
> + * @param clazz
> + * @return null
> + */
> + public static Constructor<?> getEnclosingConstructor(Class clazz)
> + {
> + throw new UnsupportedOperationException("getEnclosingConstructor
> cannot be implemented");
> + }
> +
> + /**
> + * This method cannot be implemented as it relies on native method info.
> + *
> + * @param clazz
> + * @return null
> + */
> + public static Class<?> getEnclosingClass(Class clazz)
> + {
> + throw new UnsupportedOperationException("getEnclosingClass cannot be
> implemented");
> + }
> +
> + /**
> * Static helper class
> */
> private ClassRedirects()
>
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting language
> that extends applications into web and mobile media. Attend the live webcast
> and join the prime developer group breaking into this new coding territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> jboss-cvs-commits mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits
--
xxxxxxxxxxxxxxxxxxxxxxxx
Adrian Brock
Chief Scientist
JBoss Inc.
xxxxxxxxxxxxxxxxxxxxxxxx
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
JBoss-Development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-development