changeset 26957:0ffbc690b493

methods.m: always return String methods when passed a String (bug #48758) * methods.m: When passed a Java object that happens to be a java.lang.String or a subclass, don't pass it on as the name of a possible class, return the methods of that object. Add BIST test.
author Mike Miller <mtmiller@octave.org>
date Wed, 20 Mar 2019 18:00:27 -0700
parents 7f86c097f29e
children 51414d51a973
files scripts/miscellaneous/methods.m
diffstat 1 files changed, 13 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/methods.m	Wed Mar 20 13:29:14 2019 -0700
+++ b/scripts/miscellaneous/methods.m	Wed Mar 20 18:00:27 2019 -0700
@@ -60,14 +60,15 @@
       havesigs = true;
     endif
   elseif (isjava (obj))
-    ## FIXME: Function prototype accepts java obj, but doesn't work if obj
-    ##        is e.g., java.lang.String.  Convert obj to classname then.
-    try
+    ## If obj is a String or a subclass of String, then get the methods of its
+    ## class name, not the methods of the class that may be named by the
+    ## content of the string.
+    if (isa (obj, "java.lang.String"))
+      klass = class (obj);
+      mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", klass);
+    else
       mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
-    catch
-      obj = class (obj);
-      mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
-    end_try_catch
+    endif
     mtds_list = strsplit (mtds_str, ';');
     mtds_list = mtds_list(:);  # return a column vector for compatibility
     havesigs = true;
@@ -118,6 +119,11 @@
 %! search = strfind (mtds, "valueOf");
 %! assert (! isempty ([search{:}]));
 
+## Test that methods does the right thing when passed a String object
+%!testif HAVE_JAVA; usejava ("jvm") <*48758>
+%! object = javaObject ("java.lang.String", "java.lang.Integer");
+%! assert (methods (object), methods ("java.lang.String"))
+
 ## classdef
 %!assert (methods ("inputParser"),
 %!        {"addOptional"; "addParamValue"; "addParameter";