Mercurial > hg > octave-lyh
changeset 15785:42cff4396de4
Add methods.m which extends methods() to work on Java objects.
Deprecate javamethods.m. Rename C++ methods to __methods__.
* scripts/deprecated/javamethods.m: Moved from scripts/java.
Added deprecated warning.
* scripts/java/javamethods.m: Moved to scripts/deprecated.
* scripts/general/methods.m: New m-file which accepts Java and
Octave class objects and classnames as inputs.
* libinterp/octave-value/ov-class.cc(Fmethods): Renamed methods
to __methods__ to avoid clash with methods.m
* scripts/deprecated/module.mk: Added javamethods.m to deprecated build.
* scripts/general/module.mk: Added methods.m to build.
* scripts/java/module.mk: Removed javamethods.m from build.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 13 Dec 2012 22:41:48 -0800 |
parents | 2abea2cfdace |
children | 2a2c090fdef8 |
files | libinterp/octave-value/ov-class.cc scripts/deprecated/javamethods.m scripts/deprecated/module.mk scripts/general/methods.m scripts/general/module.mk scripts/java/javamethods.m scripts/java/module.mk |
diffstat | 6 files changed, 105 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/octave-value/ov-class.cc +++ b/libinterp/octave-value/ov-class.cc @@ -2103,47 +2103,33 @@ return retval; } -DEFUN (methods, args, nargout, +DEFUN (__methods__, args, , "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} methods (@var{x})\n\ -@deftypefnx {Built-in Function} {} methods (\"classname\")\n\ -Return a cell array containing the names of the methods for the\n\ -object @var{x} or the named class.\n\ +@deftypefn {Built-in Function} {} __methods__ (@var{x})\n\ +@deftypefnx {Built-in Function} {} __methods__ (\"classname\")\n\ +Internal function.\n\ +\n\ +Implements @code{methods} for Octave class objects and classnames.\n\ +@seealso{methods}\n\ @end deftypefn") { octave_value retval; - if (args.length () == 1) - { - octave_value arg = args(0); - - std::string class_name; + // Input validation has already been done in methods.m. + octave_value arg = args(0); - if (arg.is_object ()) - class_name = arg.class_name (); - else if (arg.is_string ()) - class_name = arg.string_value (); - else - error ("methods: expecting object or class name as argument"); + std::string class_name; - if (! error_state) - { - string_vector sv = load_path::methods (class_name); - - if (nargout == 0) - { - octave_stdout << "Methods for class " << class_name << ":\n\n"; + if (arg.is_object ()) + class_name = arg.class_name (); + else if (arg.is_string ()) + class_name = arg.string_value (); - sv.list_in_columns (octave_stdout); - - octave_stdout << std::endl; - } - else - retval = Cell (sv); - } + if (! error_state) + { + string_vector sv = load_path::methods (class_name); + retval = Cell (sv); } - else - print_usage (); return retval; }
rename from scripts/java/javamethods.m rename to scripts/deprecated/javamethods.m --- a/scripts/java/javamethods.m +++ b/scripts/deprecated/javamethods.m @@ -23,10 +23,17 @@ ## Return the methods of a Java object or Java class in the form of a cell ## array of strings. If no output is requested, print the result to the ## standard output. -## @seealso{javafields, java_invoke, javaMethod, javaObject} +## @seealso{methods, javafields, java_invoke, javaMethod, javaObject} ## @end deftypefn function mtd_names = javamethods (classname) + + persistent warned = false; + if (! warned) + warned = true; + warning ("Octave:deprecated-function", + "javamethods is obsolete and will be removed from a future version of Octave, please use methods instead"); + endif if (nargin != 1) print_usage ();
--- a/scripts/deprecated/module.mk +++ b/scripts/deprecated/module.mk @@ -7,10 +7,11 @@ deprecated/cut.m \ deprecated/error_text.m \ deprecated/isstr.m \ - deprecated/javafields.m \ deprecated/java_get.m \ deprecated/java_new.m \ deprecated/java_set.m \ + deprecated/javafields.m \ + deprecated/javamethods.m \ deprecated/polyderiv.m \ deprecated/setstr.m \ deprecated/shell_cmd.m \
new file mode 100644 --- /dev/null +++ b/scripts/general/methods.m @@ -0,0 +1,76 @@ +## Copyright (C) 2012 Rik Wehbring +## +## This file is part of Octave. +## +## Octave 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 3 of the License, or (at +## your option) any later version. +## +## Octave 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Built-in Function} {} methods (@var{obj}) +## @deftypefnx {Built-in Function} {} methods ("@var{classname}") +## @deftypefnx {Built-in Function} {@var{mtds} =} methods (@dots{}) +## +## Return a cell array containing the names of the methods for the +## object @var{obj} or the named class @var{classname}. +## @var{obj} may be an Octave class object or a Java object. +## +## @seealso{fieldnames} +## @end deftypefn + +function mtds = methods (obj) + + if (nargin != 1) + print_usage (); + endif + + if (isobject (obj)) + ## Call internal C++ function for Octave objects + mtds_list = __methods__ (obj); + elseif (ischar (obj)) + ## Could be a classname for an Octave class or Java class. + ## Try Octave class first. + mtds_list = __methods__ (obj); + if (isempty (mtds_list)) + mtds_str = java_invoke ("org.octave.ClassHelper", "getMethods", obj); + mtds_list = strsplit (mtds_str, ';'); + endif + elseif (isjava (obj)) + mtds_str = java_invoke ("org.octave.ClassHelper", "getMethods", obj); + mtds_list = strsplit (mtds_str, ';'); + else + error ("methods: Invalid input argument"); + endif + + if (nargout == 0) + classname = ifelse (ischar (obj), obj, class (obj)); + printf ("Methods for class %s:\n", classname); + disp (list_in_columns (mtds_list)); + else + mtds = mtds_list; + endif + +endfunction + + +## test Octave classname +%!test +%! mtds = methods ("ftp"); +%! assert (mtds{1}, "ascii"); + +## test Java classname +%!testif HAVE_JAVA +%! mtds = methods ("java.lang.Double"); +%! search = strfind (mtds, "java.lang.Double valueOf"); +%! assert (! isempty ([search{:}])); +