Browse Source

ob-java.el: Allow non-public classes

* lisp/ob-java.el: Don't require class definitions to be declared
public.

* testing/lisp/test-ob-java.el: Add test with non-public class.
Ian Martins 4 years ago
parent
commit
5b6d774ba1
2 changed files with 18 additions and 6 deletions
  1. 6 6
      lisp/ob-java.el
  2. 12 0
      testing/lisp/test-ob-java.el

+ 6 - 6
lisp/ob-java.el

@@ -125,22 +125,22 @@
 Look through BODY for the package and class.  If found, put them
 Look through BODY for the package and class.  If found, put them
 together into a fully qualified class name and return.  Else just
 together into a fully qualified class name and return.  Else just
 return class name.  If that isn't found either, default to Main."
 return class name.  If that isn't found either, default to Main."
-  (let ((package (if (string-match "package \\\([^ ]*\\\);" body)
+  (let ((package (if (string-match org-babel-java--package-re body)
                      (match-string 1 body)))
                      (match-string 1 body)))
-        (class (if (string-match "public class \\\([^ \n]*\\\)" body)
+        (class (if (string-match org-babel-java--class-re body)
                    (match-string 1 body))))
                    (match-string 1 body))))
     (or (and package class (concat package "." class))
     (or (and package class (concat package "." class))
         (and class class)
         (and class class)
         (and package (concat package ".Main"))
         (and package (concat package ".Main"))
         "Main")))
         "Main")))
 
 
-(defconst org-babel-java--package-re "^[[:space:]]*package .*;$"
+(defconst org-babel-java--package-re "^[[:space:]]*package[[:space:]]+\\\([[:alnum:]_\.]+\\\);$"
   "Regexp for the package statement.")
   "Regexp for the package statement.")
-(defconst org-babel-java--imports-re "^[[:space:]]*import .*;$"
+(defconst org-babel-java--imports-re "^[[:space:]]*import[[:space:]]+\\\([[:alnum:]_\.]+\\\);$"
   "Regexp for import statements.")
   "Regexp for import statements.")
-(defconst org-babel-java--class-re "^public class [[:alnum:]_]+[[:space:]]*\n?[[:space:]]*{"
+(defconst org-babel-java--class-re "^[[:space:]]*\\\(?:public[[:space:]]+\\\)?class[[:space:]]+\\\([[:alnum:]_]+\\\)[[:space:]]*\n?[[:space:]]*{"
   "Regexp for the class declaration.")
   "Regexp for the class declaration.")
-(defconst org-babel-java--main-re "public static void main(String\\(?:\\[]\\)? args\\(?:\\[]\\)?).*\n?[[:space:]]*{"
+(defconst org-babel-java--main-re "public static void main(String\\\(?:\\[]\\\)?[[:space:]]+[^ ]+\\\(?:\\[]\\\)?).*\n?[[:space:]]*{"
   "Regexp for the main method declaration.")
   "Regexp for the main method declaration.")
 (defconst org-babel-java--any-method-re "public .*(.*).*\n?[[:space:]]*{"
 (defconst org-babel-java--any-method-re "public .*(.*).*\n?[[:space:]]*{"
   "Regexp for any method.")
   "Regexp for any method.")

+ 12 - 0
testing/lisp/test-ob-java.el

@@ -137,6 +137,18 @@ public class Simple {
 #+end_src"
 #+end_src"
    (should (string= "42" (org-babel-execute-src-block)))))
    (should (string= "42" (org-babel-execute-src-block)))))
 
 
+(ert-deftest ob-java/simple-with-non-public-class ()
+  "Hello world program that defines a non-public class."
+  (org-test-with-temp-text
+      "#+begin_src java :results output silent
+class Simple {
+    public static void main(String[] args) {
+        System.out.print(42);
+    }
+}
+#+end_src"
+   (should (string= "42" (org-babel-execute-src-block)))))
+
 (ert-deftest ob-java/simple-with-class-and-package ()
 (ert-deftest ob-java/simple-with-class-and-package ()
   "Hello world program that defines a class and package."
   "Hello world program that defines a class and package."
   (org-test-with-temp-text
   (org-test-with-temp-text