;;; ob-java.el --- org-babel functions for java evaluation -*- lexical-binding: t -*- ;; Copyright (C) 2011-2021 Free Software Foundation, Inc. ;; Authors: Eric Schulte ;; Dan Davison ;; Maintainer: Ian Martins ;; Keywords: literate programming, reproducible research ;; Homepage: https://orgmode.org ;; This file is part of GNU Emacs. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs. If not, see . ;;; Commentary: ;; Org-Babel support for evaluating java source code. ;;; Code: (require 'ob) (defvar org-babel-tangle-lang-exts) (add-to-list 'org-babel-tangle-lang-exts '("java" . "java")) (defvar org-babel-temporary-directory) ; from ob-core (defvar org-babel-default-header-args:java '((:results . "output") (:dir . ".")) "Default header args for java source blocks. The docs say functional mode should be the default [1], but ob-java didn't originally support functional mode, so we keep scripting mode as the default for now to maintain previous behavior. Most languages write tempfiles to babel's temporary directory, but ob-java originally had to write them to the current directory, so we keep that as the default behavior. [1] https://orgmode.org/manual/Results-of-Evaluation.html") (defconst org-babel-header-args:java '((imports . :any)) "Java-specific header arguments.") (defcustom org-babel-java-command "java" "Name of the java command. May be either a command in the path, like java or an absolute path name, like /usr/local/bin/java. Parameters may be used, like java -verbose." :group 'org-babel :package-version '(Org . "9.5") :type 'string) (defcustom org-babel-java-compiler "javac" "Name of the java compiler. May be either a command in the path, like javac or an absolute path name, like /usr/local/bin/javac. Parameters may be used, like javac -verbose." :group 'org-babel :package-version '(Org . "9.5") :type 'string) (defcustom org-babel-java-hline-to "null" "Replace hlines in incoming tables with this when translating to java." :group 'org-babel :package-version '(Org . "9.5") :type 'string) (defcustom org-babel-java-null-to 'hline "Replace `null' in java tables with this before returning." :group 'org-babel :package-version '(Org . "9.5") :type 'symbol) (defconst org-babel-java--package-re (rx line-start (0+ space) "package" (1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the package name (0+ space) ?\; line-end) "Regexp for the package statement.") (defconst org-babel-java--imports-re (rx line-start (0+ space) "import" (opt space "static") (1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the fully qualified class name (0+ space) ?\; line-end) "Regexp for import statements.") (defconst org-babel-java--class-re (rx line-start (0+ space) (opt (seq "public" (1+ space))) "class" (1+ space) (group (1+ (in alnum ?_))) ; capture the class name (0+ space) ?{) "Regexp for the class declaration.") (defconst org-babel-java--main-re (rx line-start (0+ space) "public" (1+ space) "static" (1+ space) "void" (1+ space) "main" (0+ space) ?\( (0+ space) "String" (0+ space) (1+ (in alnum ?_ ?\[ ?\] space)) ; "[] args" or "args[]" (0+ space) ?\) (0+ space) (opt "throws" (1+ (in alnum ?_ ?, ?. space))) ?{) "Regexp for the main method declaration.") (defconst org-babel-java--any-method-re (rx line-start (0+ space) (opt (seq (1+ alnum) (1+ space))) ; visibility (opt (seq "static" (1+ space))) ; binding (1+ (in alnum ?_ ?\[ ?\])) ; return type (1+ space) (1+ (in alnum ?_)) ; method name (0+ space) ?\( (0+ space) (0+ (in alnum ?_ ?\[ ?\] ?, space)) ; params (0+ space) ?\) (0+ space) (opt "throws" (1+ (in alnum ?_ ?, ?. space))) ?{) "Regexp for any method.") (defconst org-babel-java--result-wrapper "\n public static String __toString(Object val) { if (val instanceof String) { return \"\\\"\" + val + \"\\\"\"; } else if (val == null) { return \"null\"; } else if (val.getClass().isArray()) { StringBuffer sb = new StringBuffer(); Object[] vals = (Object[])val; sb.append(\"[\"); for (int ii=0; ii