annotate scripts/miscellaneous/methods.m @ 26953:6e01e5be8de3

methods.m: adapt method list post-processing to Java constructors (bug #55858) * methods.m: Add regular expression to delete package prefix and argument list from Java constructors. Add regular expression to delete package prefix from Java constructors when called with "-full".
author Mike Miller <mtmiller@octave.org>
date Wed, 20 Mar 2019 11:24:07 -0700
parents 3cbc3c741f81
children 7f86c097f29e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
1 ## Copyright (C) 2012-2019 Rik Wehbring
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
2 ##
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
4 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
5 ## Octave is free software: you can redistribute it and/or modify it
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
7 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22497
diff changeset
8 ## (at your option) any later version.
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
9 ##
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22497
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22497
diff changeset
13 ## GNU General Public License for more details.
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
14 ##
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
17 ## <https://www.gnu.org/licenses/>.
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
18
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20539
diff changeset
20 ## @deftypefn {} {} methods (@var{obj})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20539
diff changeset
21 ## @deftypefnx {} {} methods ("@var{classname}")
26767
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
22 ## @deftypefnx {} {} methods (@dots{}, "-full")
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20539
diff changeset
23 ## @deftypefnx {} {@var{mtds} =} methods (@dots{})
22167
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
24 ## List the names of the public methods for the object @var{obj} or the
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
25 ## named class @var{classname}.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
26 ##
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
27 ## @var{obj} may be an Octave class object or a Java object.
22167
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
28 ## @var{classname} may be the name of an Octave class or a Java class.
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
29 ##
26767
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
30 ## If the optional argument @qcode{"-full"} is given then Octave returns
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
31 ## full method signatures which include output type, name of method,
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
32 ## and the number and type of inputs.
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
33 ##
22167
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
34 ## When called with no output arguments, @code{methods} prints the list of
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
35 ## method names to the screen. Otherwise, the output argument @var{mtds}
6c94c7bd55e7 doc: Reword methods doc string for clarity
Mike Miller <mtmiller@octave.org>
parents: 21634
diff changeset
36 ## contains the list in a cell array of strings.
26767
ebf5ef76f56e methods.m: Add documentation for "-full" option.
Rik <rik@octave.org>
parents: 26766
diff changeset
37 ## @seealso{ismethod, properties, fieldnames}
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
38 ## @end deftypefn
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
39
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
40 function mtds = methods (obj, opt)
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
41
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
42 if (nargin < 1 || nargin > 2)
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
43 print_usage ();
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
44 endif
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
45
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
46 showsigs = (nargin > 1 && ischar (opt) && strcmp (opt, "-full"));
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
47 havesigs = false;
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
48
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
49 if (isobject (obj))
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
50 ## Call internal C++ function for Octave objects
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
51 mtds_list = __methods__ (obj);
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
52 elseif (ischar (obj))
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
53 ## Could be a classname for an Octave class or Java class.
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
54 ## Try Octave class first.
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
55 mtds_list = __methods__ (obj);
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
56 if (isempty (mtds_list))
15790
921912c92102 Deprecate java_invoke, replace with javaMethod.
Rik <rik@octave.org>
parents: 15785
diff changeset
57 mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
58 mtds_list = ostrsplit (mtds_str, ';');
26768
3cbc3c741f81 methods.m: Return a column vector for Matlab compatibility.
Rik <rik@octave.org>
parents: 26767
diff changeset
59 mtds_list = mtds_list(:); # return a column vector for compatibility
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
60 havesigs = true;
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
61 endif
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
62 elseif (isjava (obj))
20539
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
63 ## FIXME: Function prototype accepts java obj, but doesn't work if obj
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
64 ## is e.g., java.lang.String. Convert obj to classname then.
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
65 try
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
66 mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
67 catch
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
68 obj = class (obj);
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
69 mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
5d31cd795c21 methods.m: try Java class names if getMethods fails on Java objects (bug #46010)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 20158
diff changeset
70 end_try_catch
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
71 mtds_list = strsplit (mtds_str, ';');
26768
3cbc3c741f81 methods.m: Return a column vector for Matlab compatibility.
Rik <rik@octave.org>
parents: 26767
diff changeset
72 mtds_list = mtds_list(:); # return a column vector for compatibility
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
73 havesigs = true;
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
74 else
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
75 error ("methods: invalid input argument");
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
76 endif
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
77
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
78 if (havesigs && ! showsigs)
26766
c1d5a0a799b2 methods.m: Fix bug when function signature has spaces.
Rik <rik@octave.org>
parents: 26761
diff changeset
79 mtds_list = regexprep (mtds_list, '^(?:[^(]+) (\w+) ?\(.*$', '$1');
26953
6e01e5be8de3 methods.m: adapt method list post-processing to Java constructors (bug #55858)
Mike Miller <mtmiller@octave.org>
parents: 26768
diff changeset
80 mtds_list = regexprep (mtds_list, '^(?:[\.\w]+\.)(\w+) ?\(.*$', '$1');
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
81 mtds_list = unique (mtds_list);
26953
6e01e5be8de3 methods.m: adapt method list post-processing to Java constructors (bug #55858)
Mike Miller <mtmiller@octave.org>
parents: 26768
diff changeset
82 else
6e01e5be8de3 methods.m: adapt method list post-processing to Java constructors (bug #55858)
Mike Miller <mtmiller@octave.org>
parents: 26768
diff changeset
83 mtds_list = regexprep (mtds_list, '^(?:[\.\w]+\.)(\w+ ?\(.*)$', '$1');
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
84 endif
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
85
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
86 if (nargout == 0)
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
87 classname = ifelse (ischar (obj), obj, class (obj));
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
88 printf ("Methods for class %s:\n", classname);
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
89 disp (list_in_columns (mtds_list));
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
90 else
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
91 mtds = mtds_list;
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
92 endif
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
93
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
94 endfunction
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
95
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
96
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
97 ## test Octave classname
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
98 %!test
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
99 %! mtds = methods ("ftp");
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
100 %! assert (mtds{1}, "ascii");
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
101
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
102 ## test Java classname
23405
32ec90068be5 use %!testif runtime tests to properly skip java tests
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
103 %!testif HAVE_JAVA; usejava ("jvm")
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
104 %! mtds = methods ("java.lang.Double", "-full");
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
105 %! search = strfind (mtds, "java.lang.Double valueOf");
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
106 %! assert (! isempty ([search{:}]));
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
107
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
108 ## test Java classname
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
109 %!testif HAVE_JAVA; usejava ("jvm")
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
110 %! mtds = methods ("java.lang.Double");
26761
8554becefe1b handle -full argument for methods function (bug #55487)
John W. Eaton <jwe@octave.org>
parents: 26724
diff changeset
111 %! search = strfind (mtds, "valueOf");
15785
42cff4396de4 Add methods.m which extends methods() to work on Java objects.
Rik <rik@octave.org>
parents:
diff changeset
112 %! assert (! isempty ([search{:}]));
26724
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
113
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
114 ## classdef
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
115 %!assert (methods ("inputParser"),
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
116 %! {"addOptional"; "addParamValue"; "addParameter";
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
117 %! "addRequired"; "addSwitch"; "add_missing"; "delete";
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
118 %! "disp"; "error"; "is_argname"; "parse"; "validate_arg";
84355b7b9521 allow methods function to work for classdef objects (bug #42510)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
119 %! "validate_name"});