Title: Using semantic + import statement to open class source

Hi,
        I found the java-open package written by Rajeev Karunakaran to be quite useful and quick.
I have written some functions which use the same concept but use semantic to get the import statements instead
of reg-ex. This is very usefull if you don't have class files for the source that you are looking for and for speed.
It would be nice if this could be used to open class source files before using the beanshell or if the user could
specifiy which method to use first. It would also be nice to then fallback to using tags.

Let me know what you think.


(defun jde-custom-find-fqimport (import-list class)
  "Opens the source file for `class' if a fully qualified import statement for
the class is found in `import-list' and returns t; returns nil otherwise.
Uses `jde-find-class-source' to open the file"
  (let ((current-import)
        (match-found nil)
        (iterator import-list))
    (while (and iterator (not match-found))
      (setq current-import (car (car iterator)))
      (message current-import)
      (if (string-match (concat ".*\\." class "\\'") current-import)
          (progn
            (if (not (jde-find-class-source current-import))
                (message "Can't find source"))
            (setq match-found t)))
      (setq iterator (cdr iterator)))
    match-found
    )
)

(defun jde-custom-find-starimport (import-list class)
  "Opens the source file for `class' if a star import statement for
the class is found in `import-list' and returns t; returns nil otherwise.
Uses `jde-find-class-source' to open the file"
  (let ((current-import)
        (match-found nil)
        (iterator import-list))
    (while (and iterator (not match-found))
      (setq current-import (car (car iterator)))
      (message current-import)
      (if (string-match "\\(.*\\.\\)\\*" current-import)
          (progn
            (setq current-import (replace-match "\\1" t nil current-import))
            (message "found star import")
            (message current-import)
            (if (jde-find-class-source (concat current-import class))
                (setq match-found t))))
      (setq iterator (cdr iterator)))
    match-found
    )
)
   

(defun jde-custom-open-class (&optional unqual-class)
   "Opens the source file for `class' if an import statement for
the class is found or the class is found in the current source file
directory or in java.lang.
Uses `jde-find-class-source' to open the file"
 (interactive)
;;  (semantic-bovinate-toplevel t)
  (save-excursion
    (let* ((class (or unqual-class
                      (read-from-minibuffer "Class: " (thing-at-point 'symbol))))
           (tokens (semantic-bovinate-toplevel t))
           (depends  (semantic-find-nonterminal-by-token 'include tokens)))
      (cond ((jde-custom-find-fqimport depends class))
            ((jde-custom-find-starimport depends class))
            ((progn
               ;; look for file in current directory
               (let ((fname (concat class ".java")))
                 (if (file-readable-p fname)
                     (progn (find-file fname)      ; open file in current dir
                            (message "Opened %s" (expand-file-name fname)))))))
            ((jde-find-class-source (concat "java.lang." class)))
            ((jde-open-class-source class))
            )
      )))

(defun jde-custom-open-class-at-point()
  (interactive)
  (jde-custom-open-class (current-word)))




Tim Babin

Reply via email to