emul/src/main/java/java/lang/reflect/Method.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Dec 2012 08:59:47 +0100
branchlauncher
changeset 355 eea0065bcc1a
parent 354 002b7c3d5157
child 391 8cddb5d3e18f
permissions -rw-r--r--
Support for reflection on primitive types. All tests finish in the browser.
     1 /*
     2  * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang.reflect;
    27 
    28 import java.lang.annotation.Annotation;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 import org.apidesign.bck2brwsr.emul.AnnotationImpl;
    31 
    32 /**
    33  * A {@code Method} provides information about, and access to, a single method
    34  * on a class or interface.  The reflected method may be a class method
    35  * or an instance method (including an abstract method).
    36  *
    37  * <p>A {@code Method} permits widening conversions to occur when matching the
    38  * actual parameters to invoke with the underlying method's formal
    39  * parameters, but it throws an {@code IllegalArgumentException} if a
    40  * narrowing conversion would occur.
    41  *
    42  * @see Member
    43  * @see java.lang.Class
    44  * @see java.lang.Class#getMethods()
    45  * @see java.lang.Class#getMethod(String, Class[])
    46  * @see java.lang.Class#getDeclaredMethods()
    47  * @see java.lang.Class#getDeclaredMethod(String, Class[])
    48  *
    49  * @author Kenneth Russell
    50  * @author Nakul Saraiya
    51  */
    52 public final
    53     class Method extends AccessibleObject implements GenericDeclaration,
    54                                                      Member {
    55     private final Class<?> clazz;
    56     private final String name;
    57     private final Object data;
    58     private final String sig;
    59     private int modifiers;
    60 
    61    // Generics infrastructure
    62 
    63     private String getGenericSignature() {return null;}
    64 
    65     /**
    66      * Package-private constructor used by ReflectAccess to enable
    67      * instantiation of these objects in Java code from the java.lang
    68      * package via sun.reflect.LangReflectAccess.
    69      */
    70     Method(Class<?> declaringClass, String name, Object data, String sig)
    71     {
    72         this.clazz = declaringClass;
    73         this.name = name;
    74         this.data = data;
    75         this.sig = sig;
    76     }
    77 
    78     /**
    79      * Package-private routine (exposed to java.lang.Class via
    80      * ReflectAccess) which returns a copy of this Method. The copy's
    81      * "root" field points to this Method.
    82      */
    83     Method copy() {
    84         return this;
    85     }
    86 
    87     /**
    88      * Returns the {@code Class} object representing the class or interface
    89      * that declares the method represented by this {@code Method} object.
    90      */
    91     public Class<?> getDeclaringClass() {
    92         return clazz;
    93     }
    94 
    95     /**
    96      * Returns the name of the method represented by this {@code Method}
    97      * object, as a {@code String}.
    98      */
    99     public String getName() {
   100         return name;
   101     }
   102 
   103     /**
   104      * Returns the Java language modifiers for the method represented
   105      * by this {@code Method} object, as an integer. The {@code Modifier} class should
   106      * be used to decode the modifiers.
   107      *
   108      * @see Modifier
   109      */
   110     public int getModifiers() {
   111         return modifiers;
   112     }
   113 
   114     /**
   115      * Returns an array of {@code TypeVariable} objects that represent the
   116      * type variables declared by the generic declaration represented by this
   117      * {@code GenericDeclaration} object, in declaration order.  Returns an
   118      * array of length 0 if the underlying generic declaration declares no type
   119      * variables.
   120      *
   121      * @return an array of {@code TypeVariable} objects that represent
   122      *     the type variables declared by this generic declaration
   123      * @throws GenericSignatureFormatError if the generic
   124      *     signature of this generic declaration does not conform to
   125      *     the format specified in
   126      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   127      * @since 1.5
   128      */
   129     public TypeVariable<Method>[] getTypeParameters() {
   130         throw new UnsupportedOperationException();
   131     }
   132 
   133     /**
   134      * Returns a {@code Class} object that represents the formal return type
   135      * of the method represented by this {@code Method} object.
   136      *
   137      * @return the return type for the method this object represents
   138      */
   139     public Class<?> getReturnType() {
   140         switch (sig.charAt(0)) {
   141             case 'I': return Integer.TYPE;
   142             case 'J': return Long.TYPE;
   143             case 'D': return Double.TYPE;
   144             case 'F': return Float.TYPE;
   145             case 'B': return Byte.TYPE;
   146             case 'Z': return Boolean.TYPE;
   147             case 'S': return Short.TYPE;
   148 //            case 'V': return Void.TYPE;
   149             case 'L': try {
   150                 int up = sig.indexOf("_2");
   151                 String type = sig.substring(1, up);
   152                 return Class.forName(type);
   153             } catch (ClassNotFoundException ex) {
   154                 // should not happen
   155             }
   156         }
   157         throw new UnsupportedOperationException(sig);
   158     }
   159 
   160     /**
   161      * Returns a {@code Type} object that represents the formal return
   162      * type of the method represented by this {@code Method} object.
   163      *
   164      * <p>If the return type is a parameterized type,
   165      * the {@code Type} object returned must accurately reflect
   166      * the actual type parameters used in the source code.
   167      *
   168      * <p>If the return type is a type variable or a parameterized type, it
   169      * is created. Otherwise, it is resolved.
   170      *
   171      * @return  a {@code Type} object that represents the formal return
   172      *     type of the underlying  method
   173      * @throws GenericSignatureFormatError
   174      *     if the generic method signature does not conform to the format
   175      *     specified in
   176      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   177      * @throws TypeNotPresentException if the underlying method's
   178      *     return type refers to a non-existent type declaration
   179      * @throws MalformedParameterizedTypeException if the
   180      *     underlying method's return typed refers to a parameterized
   181      *     type that cannot be instantiated for any reason
   182      * @since 1.5
   183      */
   184     public Type getGenericReturnType() {
   185         throw new UnsupportedOperationException();
   186     }
   187 
   188 
   189     /**
   190      * Returns an array of {@code Class} objects that represent the formal
   191      * parameter types, in declaration order, of the method
   192      * represented by this {@code Method} object.  Returns an array of length
   193      * 0 if the underlying method takes no parameters.
   194      *
   195      * @return the parameter types for the method this object
   196      * represents
   197      */
   198     public Class<?>[] getParameterTypes() {
   199         throw new UnsupportedOperationException();
   200         //return (Class<?>[]) parameterTypes.clone();
   201     }
   202 
   203     /**
   204      * Returns an array of {@code Type} objects that represent the formal
   205      * parameter types, in declaration order, of the method represented by
   206      * this {@code Method} object. Returns an array of length 0 if the
   207      * underlying method takes no parameters.
   208      *
   209      * <p>If a formal parameter type is a parameterized type,
   210      * the {@code Type} object returned for it must accurately reflect
   211      * the actual type parameters used in the source code.
   212      *
   213      * <p>If a formal parameter type is a type variable or a parameterized
   214      * type, it is created. Otherwise, it is resolved.
   215      *
   216      * @return an array of Types that represent the formal
   217      *     parameter types of the underlying method, in declaration order
   218      * @throws GenericSignatureFormatError
   219      *     if the generic method signature does not conform to the format
   220      *     specified in
   221      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   222      * @throws TypeNotPresentException if any of the parameter
   223      *     types of the underlying method refers to a non-existent type
   224      *     declaration
   225      * @throws MalformedParameterizedTypeException if any of
   226      *     the underlying method's parameter types refer to a parameterized
   227      *     type that cannot be instantiated for any reason
   228      * @since 1.5
   229      */
   230     public Type[] getGenericParameterTypes() {
   231         throw new UnsupportedOperationException();
   232     }
   233 
   234 
   235     /**
   236      * Returns an array of {@code Class} objects that represent
   237      * the types of the exceptions declared to be thrown
   238      * by the underlying method
   239      * represented by this {@code Method} object.  Returns an array of length
   240      * 0 if the method declares no exceptions in its {@code throws} clause.
   241      *
   242      * @return the exception types declared as being thrown by the
   243      * method this object represents
   244      */
   245     public Class<?>[] getExceptionTypes() {
   246         throw new UnsupportedOperationException();
   247         //return (Class<?>[]) exceptionTypes.clone();
   248     }
   249 
   250     /**
   251      * Returns an array of {@code Type} objects that represent the
   252      * exceptions declared to be thrown by this {@code Method} object.
   253      * Returns an array of length 0 if the underlying method declares
   254      * no exceptions in its {@code throws} clause.
   255      *
   256      * <p>If an exception type is a type variable or a parameterized
   257      * type, it is created. Otherwise, it is resolved.
   258      *
   259      * @return an array of Types that represent the exception types
   260      *     thrown by the underlying method
   261      * @throws GenericSignatureFormatError
   262      *     if the generic method signature does not conform to the format
   263      *     specified in
   264      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   265      * @throws TypeNotPresentException if the underlying method's
   266      *     {@code throws} clause refers to a non-existent type declaration
   267      * @throws MalformedParameterizedTypeException if
   268      *     the underlying method's {@code throws} clause refers to a
   269      *     parameterized type that cannot be instantiated for any reason
   270      * @since 1.5
   271      */
   272       public Type[] getGenericExceptionTypes() {
   273         throw new UnsupportedOperationException();
   274       }
   275 
   276     /**
   277      * Compares this {@code Method} against the specified object.  Returns
   278      * true if the objects are the same.  Two {@code Methods} are the same if
   279      * they were declared by the same class and have the same name
   280      * and formal parameter types and return type.
   281      */
   282     public boolean equals(Object obj) {
   283         if (obj != null && obj instanceof Method) {
   284             Method other = (Method)obj;
   285             return data == other.data;
   286         }
   287         return false;
   288     }
   289 
   290     /**
   291      * Returns a hashcode for this {@code Method}.  The hashcode is computed
   292      * as the exclusive-or of the hashcodes for the underlying
   293      * method's declaring class name and the method's name.
   294      */
   295     public int hashCode() {
   296         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
   297     }
   298 
   299     /**
   300      * Returns a string describing this {@code Method}.  The string is
   301      * formatted as the method access modifiers, if any, followed by
   302      * the method return type, followed by a space, followed by the
   303      * class declaring the method, followed by a period, followed by
   304      * the method name, followed by a parenthesized, comma-separated
   305      * list of the method's formal parameter types. If the method
   306      * throws checked exceptions, the parameter list is followed by a
   307      * space, followed by the word throws followed by a
   308      * comma-separated list of the thrown exception types.
   309      * For example:
   310      * <pre>
   311      *    public boolean java.lang.Object.equals(java.lang.Object)
   312      * </pre>
   313      *
   314      * <p>The access modifiers are placed in canonical order as
   315      * specified by "The Java Language Specification".  This is
   316      * {@code public}, {@code protected} or {@code private} first,
   317      * and then other modifiers in the following order:
   318      * {@code abstract}, {@code static}, {@code final},
   319      * {@code synchronized}, {@code native}, {@code strictfp}.
   320      */
   321     public String toString() {
   322         try {
   323             StringBuilder sb = new StringBuilder();
   324             int mod = getModifiers() & Modifier.methodModifiers();
   325             if (mod != 0) {
   326                 sb.append(Modifier.toString(mod)).append(' ');
   327             }
   328             sb.append(Field.getTypeName(getReturnType())).append(' ');
   329             sb.append(Field.getTypeName(getDeclaringClass())).append('.');
   330             sb.append(getName()).append('(');
   331             /*
   332             Class<?>[] params = parameterTypes; // avoid clone
   333             for (int j = 0; j < params.length; j++) {
   334                 sb.append(Field.getTypeName(params[j]));
   335                 if (j < (params.length - 1))
   336                     sb.append(',');
   337             }
   338             sb.append(')');
   339             Class<?>[] exceptions = exceptionTypes; // avoid clone
   340             if (exceptions.length > 0) {
   341                 sb.append(" throws ");
   342                 for (int k = 0; k < exceptions.length; k++) {
   343                     sb.append(exceptions[k].getName());
   344                     if (k < (exceptions.length - 1))
   345                         sb.append(',');
   346                 }
   347             }
   348             */
   349             return sb.toString();
   350         } catch (Exception e) {
   351             return "<" + e + ">";
   352         }
   353     }
   354 
   355     /**
   356      * Returns a string describing this {@code Method}, including
   357      * type parameters.  The string is formatted as the method access
   358      * modifiers, if any, followed by an angle-bracketed
   359      * comma-separated list of the method's type parameters, if any,
   360      * followed by the method's generic return type, followed by a
   361      * space, followed by the class declaring the method, followed by
   362      * a period, followed by the method name, followed by a
   363      * parenthesized, comma-separated list of the method's generic
   364      * formal parameter types.
   365      *
   366      * If this method was declared to take a variable number of
   367      * arguments, instead of denoting the last parameter as
   368      * "<tt><i>Type</i>[]</tt>", it is denoted as
   369      * "<tt><i>Type</i>...</tt>".
   370      *
   371      * A space is used to separate access modifiers from one another
   372      * and from the type parameters or return type.  If there are no
   373      * type parameters, the type parameter list is elided; if the type
   374      * parameter list is present, a space separates the list from the
   375      * class name.  If the method is declared to throw exceptions, the
   376      * parameter list is followed by a space, followed by the word
   377      * throws followed by a comma-separated list of the generic thrown
   378      * exception types.  If there are no type parameters, the type
   379      * parameter list is elided.
   380      *
   381      * <p>The access modifiers are placed in canonical order as
   382      * specified by "The Java Language Specification".  This is
   383      * {@code public}, {@code protected} or {@code private} first,
   384      * and then other modifiers in the following order:
   385      * {@code abstract}, {@code static}, {@code final},
   386      * {@code synchronized}, {@code native}, {@code strictfp}.
   387      *
   388      * @return a string describing this {@code Method},
   389      * include type parameters
   390      *
   391      * @since 1.5
   392      */
   393     public String toGenericString() {
   394         try {
   395             StringBuilder sb = new StringBuilder();
   396             int mod = getModifiers() & Modifier.methodModifiers();
   397             if (mod != 0) {
   398                 sb.append(Modifier.toString(mod)).append(' ');
   399             }
   400             TypeVariable<?>[] typeparms = getTypeParameters();
   401             if (typeparms.length > 0) {
   402                 boolean first = true;
   403                 sb.append('<');
   404                 for(TypeVariable<?> typeparm: typeparms) {
   405                     if (!first)
   406                         sb.append(',');
   407                     // Class objects can't occur here; no need to test
   408                     // and call Class.getName().
   409                     sb.append(typeparm.toString());
   410                     first = false;
   411                 }
   412                 sb.append("> ");
   413             }
   414 
   415             Type genRetType = getGenericReturnType();
   416             sb.append( ((genRetType instanceof Class<?>)?
   417                         Field.getTypeName((Class<?>)genRetType):genRetType.toString()))
   418                     .append(' ');
   419 
   420             sb.append(Field.getTypeName(getDeclaringClass())).append('.');
   421             sb.append(getName()).append('(');
   422             Type[] params = getGenericParameterTypes();
   423             for (int j = 0; j < params.length; j++) {
   424                 String param = (params[j] instanceof Class)?
   425                     Field.getTypeName((Class)params[j]):
   426                     (params[j].toString());
   427                 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
   428                     param = param.replaceFirst("\\[\\]$", "...");
   429                 sb.append(param);
   430                 if (j < (params.length - 1))
   431                     sb.append(',');
   432             }
   433             sb.append(')');
   434             Type[] exceptions = getGenericExceptionTypes();
   435             if (exceptions.length > 0) {
   436                 sb.append(" throws ");
   437                 for (int k = 0; k < exceptions.length; k++) {
   438                     sb.append((exceptions[k] instanceof Class)?
   439                               ((Class)exceptions[k]).getName():
   440                               exceptions[k].toString());
   441                     if (k < (exceptions.length - 1))
   442                         sb.append(',');
   443                 }
   444             }
   445             return sb.toString();
   446         } catch (Exception e) {
   447             return "<" + e + ">";
   448         }
   449     }
   450 
   451     /**
   452      * Invokes the underlying method represented by this {@code Method}
   453      * object, on the specified object with the specified parameters.
   454      * Individual parameters are automatically unwrapped to match
   455      * primitive formal parameters, and both primitive and reference
   456      * parameters are subject to method invocation conversions as
   457      * necessary.
   458      *
   459      * <p>If the underlying method is static, then the specified {@code obj}
   460      * argument is ignored. It may be null.
   461      *
   462      * <p>If the number of formal parameters required by the underlying method is
   463      * 0, the supplied {@code args} array may be of length 0 or null.
   464      *
   465      * <p>If the underlying method is an instance method, it is invoked
   466      * using dynamic method lookup as documented in The Java Language
   467      * Specification, Second Edition, section 15.12.4.4; in particular,
   468      * overriding based on the runtime type of the target object will occur.
   469      *
   470      * <p>If the underlying method is static, the class that declared
   471      * the method is initialized if it has not already been initialized.
   472      *
   473      * <p>If the method completes normally, the value it returns is
   474      * returned to the caller of invoke; if the value has a primitive
   475      * type, it is first appropriately wrapped in an object. However,
   476      * if the value has the type of an array of a primitive type, the
   477      * elements of the array are <i>not</i> wrapped in objects; in
   478      * other words, an array of primitive type is returned.  If the
   479      * underlying method return type is void, the invocation returns
   480      * null.
   481      *
   482      * @param obj  the object the underlying method is invoked from
   483      * @param args the arguments used for the method call
   484      * @return the result of dispatching the method represented by
   485      * this object on {@code obj} with parameters
   486      * {@code args}
   487      *
   488      * @exception IllegalAccessException    if this {@code Method} object
   489      *              is enforcing Java language access control and the underlying
   490      *              method is inaccessible.
   491      * @exception IllegalArgumentException  if the method is an
   492      *              instance method and the specified object argument
   493      *              is not an instance of the class or interface
   494      *              declaring the underlying method (or of a subclass
   495      *              or implementor thereof); if the number of actual
   496      *              and formal parameters differ; if an unwrapping
   497      *              conversion for primitive arguments fails; or if,
   498      *              after possible unwrapping, a parameter value
   499      *              cannot be converted to the corresponding formal
   500      *              parameter type by a method invocation conversion.
   501      * @exception InvocationTargetException if the underlying method
   502      *              throws an exception.
   503      * @exception NullPointerException      if the specified object is null
   504      *              and the method is an instance method.
   505      * @exception ExceptionInInitializerError if the initialization
   506      * provoked by this method fails.
   507      */
   508     public Object invoke(Object obj, Object... args)
   509         throws IllegalAccessException, IllegalArgumentException,
   510            InvocationTargetException
   511     {
   512         Object res = invoke0(this, obj, args);
   513         if (getReturnType().isPrimitive()) {
   514             res = fromPrimitive(getReturnType(), res);
   515         }
   516         return res;
   517     }
   518     
   519     @JavaScriptBody(args = { "method", "self", "args" }, body =
   520           "if (args.length > 0) throw 'unsupported now';"
   521         + "return method.fld_data(self);"
   522     )
   523     private static native Object invoke0(Method m, Object self, Object[] args);
   524 
   525     private static Object fromPrimitive(Class<?> type, Object o) {
   526         if (type == Integer.TYPE) {
   527             return fromRaw(Integer.class, "valueOf__Ljava_lang_Integer_2I", o);
   528         }
   529         if (type == Long.TYPE) {
   530             return fromRaw(Long.class, "valueOf__Ljava_lang_Long_2J", o);
   531         }
   532         if (type == Double.TYPE) {
   533             return fromRaw(Double.class, "valueOf__Ljava_lang_Double_2D", o);
   534         }
   535         if (type == Float.TYPE) {
   536             return fromRaw(Float.class, "valueOf__Ljava_lang_Float_2F", o);
   537         }
   538         if (type == Byte.TYPE) {
   539             return fromRaw(Byte.class, "valueOf__Ljava_lang_Byte_2B", o);
   540         }
   541         if (type == Boolean.TYPE) {
   542             return fromRaw(Boolean.class, "valueOf__Ljava_lang_Boolean_2Z", o);
   543         }
   544         if (type == Short.TYPE) {
   545             return fromRaw(Short.class, "valueOf__Ljava_lang_Short_2S", o);
   546         }
   547 //            case 'V': return Void.TYPE;
   548         throw new IllegalStateException("Can't convert " + o);
   549     }
   550     
   551     @JavaScriptBody(args = { "cls", "m", "o" }, 
   552         body = "return cls.cnstr(false)[m](o);"
   553     )
   554     private static native Integer fromRaw(Class<?> cls, String m, Object o);
   555     
   556     /**
   557      * Returns {@code true} if this method is a bridge
   558      * method; returns {@code false} otherwise.
   559      *
   560      * @return true if and only if this method is a bridge
   561      * method as defined by the Java Language Specification.
   562      * @since 1.5
   563      */
   564     public boolean isBridge() {
   565         return (getModifiers() & Modifier.BRIDGE) != 0;
   566     }
   567 
   568     /**
   569      * Returns {@code true} if this method was declared to take
   570      * a variable number of arguments; returns {@code false}
   571      * otherwise.
   572      *
   573      * @return {@code true} if an only if this method was declared to
   574      * take a variable number of arguments.
   575      * @since 1.5
   576      */
   577     public boolean isVarArgs() {
   578         return (getModifiers() & Modifier.VARARGS) != 0;
   579     }
   580 
   581     /**
   582      * Returns {@code true} if this method is a synthetic
   583      * method; returns {@code false} otherwise.
   584      *
   585      * @return true if and only if this method is a synthetic
   586      * method as defined by the Java Language Specification.
   587      * @since 1.5
   588      */
   589     public boolean isSynthetic() {
   590         return Modifier.isSynthetic(getModifiers());
   591     }
   592 
   593     @JavaScriptBody(args = { "self", "ac" }, 
   594         body = 
   595           "if (self.fld_data.anno) {"
   596         + "  return self.fld_data.anno['L' + ac.jvmName + ';'];"
   597         + "} else return null;"
   598     )
   599     private Object getAnnotationData(Class<?> annotationClass) {
   600         throw new UnsupportedOperationException();
   601     }
   602     
   603     /**
   604      * @throws NullPointerException {@inheritDoc}
   605      * @since 1.5
   606      */
   607     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
   608         Object data = getAnnotationData(annotationClass);
   609         return data == null ? null : AnnotationImpl.create(annotationClass, data);
   610     }
   611 
   612     /**
   613      * @since 1.5
   614      */
   615     public Annotation[] getDeclaredAnnotations()  {
   616         throw new UnsupportedOperationException();
   617     }
   618 
   619     /**
   620      * Returns the default value for the annotation member represented by
   621      * this {@code Method} instance.  If the member is of a primitive type,
   622      * an instance of the corresponding wrapper type is returned. Returns
   623      * null if no default is associated with the member, or if the method
   624      * instance does not represent a declared member of an annotation type.
   625      *
   626      * @return the default value for the annotation member represented
   627      *     by this {@code Method} instance.
   628      * @throws TypeNotPresentException if the annotation is of type
   629      *     {@link Class} and no definition can be found for the
   630      *     default class value.
   631      * @since  1.5
   632      */
   633     public Object getDefaultValue() {
   634         throw new UnsupportedOperationException();
   635     }
   636 
   637     /**
   638      * Returns an array of arrays that represent the annotations on the formal
   639      * parameters, in declaration order, of the method represented by
   640      * this {@code Method} object. (Returns an array of length zero if the
   641      * underlying method is parameterless.  If the method has one or more
   642      * parameters, a nested array of length zero is returned for each parameter
   643      * with no annotations.) The annotation objects contained in the returned
   644      * arrays are serializable.  The caller of this method is free to modify
   645      * the returned arrays; it will have no effect on the arrays returned to
   646      * other callers.
   647      *
   648      * @return an array of arrays that represent the annotations on the formal
   649      *    parameters, in declaration order, of the method represented by this
   650      *    Method object
   651      * @since 1.5
   652      */
   653     public Annotation[][] getParameterAnnotations() {
   654         throw new UnsupportedOperationException();
   655     }
   656     
   657     //
   658     // bck2brwsr implementation
   659     //
   660 
   661     @JavaScriptBody(args = { "clazz", "prefix" },
   662         body = ""
   663         + "var c = clazz.cnstr.prototype;"
   664         + "var arr = new Array();\n"
   665         + "for (m in c) {\n"
   666         + "  if (m.indexOf(prefix) === 0) {\n"
   667         + "     arr.push(m);\n"
   668         + "     arr.push(c[m]);\n"
   669         + "  }"
   670         + "}\n"
   671         + "return arr;"
   672     )
   673     private static native Object[] findMethodData(
   674         Class<?> clazz, String prefix
   675     );
   676 
   677     // XXX should not be public
   678     public static Method findMethod(
   679         Class<?> clazz, String name, Class<?>... parameterTypes
   680     ) {
   681         Object[] data = findMethodData(clazz, name + "__");
   682         if (data.length == 0) {
   683             return null;
   684         }
   685         String sig = ((String)data[0]).substring(name.length() + 2);
   686         return new Method(clazz, name, data[1], sig);
   687     }
   688     
   689     public static Method[] findMethods(Class<?> clazz) {
   690         Object[] namesAndData = findMethodData(clazz, "");
   691         int cnt = 0;
   692         for (int i = 0; i < namesAndData.length; i += 2) {
   693             String sig = (String) namesAndData[i];
   694             Object data = namesAndData[i + 1];
   695             int middle = sig.indexOf("__");
   696             if (middle == -1) {
   697                 continue;
   698             }
   699             String name = sig.substring(0, middle);
   700             sig = sig.substring(middle + 2);
   701             namesAndData[cnt++] = new Method(clazz, name, data, sig);
   702         }
   703         Method[] arr = new Method[cnt];
   704         for (int i = 0; i < cnt; i++) {
   705             arr[i] = (Method)namesAndData[i];
   706         }
   707         return arr;
   708     }
   709 }