1 ;; Association list of fully qualified class names (package + class name)
2 ;; and their source files.
3 (defvar gud-jdb-class-source-alist nil
4 "Association list of fully qualified class names and source files.")
6 ;; This is used to hold a source file during analysis.
7 (defvar gud-jdb-analysis-buffer nil)
9 (defvar gud-jdb-classpath-string nil
10 "Holds temporary classpath values.")
12 (defun gud-jdb-build-source-files-list (path extn)
13 "Return a list of java source files (absolute paths).
14 PATH gives the directories in which to search for files with
15 extension EXTN. Normally EXTN is given as the regular expression
17 (apply 'nconc (mapcar (lambda (d)
18 (when (file-directory-p d)
19 (directory-files d t extn nil)))
22 ;; Move point past whitespace.
23 (defun gud-jdb-skip-whitespace ()
24 (skip-chars-forward " \n\r\t\014"))
26 ;; Move point past a "// <eol>" type of comment.
27 (defun gud-jdb-skip-single-line-comment ()
30 ;; Move point past a "/* */" or "/** */" type of comment.
31 (defun gud-jdb-skip-traditional-or-documentation-comment ()
35 (if (eq (following-char) ?*)
39 (if (eq (following-char) ?/)
42 (throw 'break nil)))))
45 ;; Move point past any number of consecutive whitespace chars and/or comments.
46 (defun gud-jdb-skip-whitespace-and-comments ()
47 (gud-jdb-skip-whitespace)
52 (gud-jdb-skip-single-line-comment)
53 (gud-jdb-skip-whitespace))
55 (gud-jdb-skip-traditional-or-documentation-comment)
56 (gud-jdb-skip-whitespace))
57 (t (throw 'done nil))))))
59 ;; Move point past things that are id-like. The intent is to skip regular
60 ;; id's, such as class or interface names as well as package and interface
62 (defun gud-jdb-skip-id-ish-thing ()
63 (skip-chars-forward "^ /\n\r\t\014,;{"))
65 ;; Move point past a string literal.
66 (defun gud-jdb-skip-string-literal ()
69 ((eq (following-char) ?\\)
71 ((eq (following-char) ?\042))))
75 ;; Move point past a character literal.
76 (defun gud-jdb-skip-character-literal ()
80 (if (eq (following-char) ?\\)
82 (not (eq (following-char) ?\')))
86 ;; Move point past the following block. There may be (legal) cruft before
87 ;; the block's opening brace. There must be a block or it's the end of life
88 ;; in petticoat junction.
89 (defun gud-jdb-skip-block ()
91 ;; Find the beginning of the block.
93 (not (eq (following-char) ?{))
95 ;; Skip any constructs that can harbor literal block delimiter
96 ;; characters and/or the delimiters for the constructs themselves.
99 (gud-jdb-skip-single-line-comment))
101 (gud-jdb-skip-traditional-or-documentation-comment))
102 ((eq (following-char) ?\042)
103 (gud-jdb-skip-string-literal))
104 ((eq (following-char) ?\')
105 (gud-jdb-skip-character-literal))
108 ;; Now at the beginning of the block.
111 ;; Skip over the body of the block as well as the final brace.
112 (let ((open-level 1))
113 (while (not (eq open-level 0))
116 (gud-jdb-skip-single-line-comment))
118 (gud-jdb-skip-traditional-or-documentation-comment))
119 ((eq (following-char) ?\042)
120 (gud-jdb-skip-string-literal))
121 ((eq (following-char) ?\')
122 (gud-jdb-skip-character-literal))
123 ((eq (following-char) ?{)
124 (setq open-level (+ open-level 1))
126 ((eq (following-char) ?})
127 (setq open-level (- open-level 1))
129 (t (forward-char))))))
131 ;; Find the package and class definitions in Java source file FILE. Assumes
132 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
133 ;; to hold the source during analysis.
134 (defun gud-jdb-analyze-source (buf file)
137 (insert-file-contents file nil nil nil t)
142 (gud-jdb-skip-whitespace)
146 ;; Any number of semi's following a block is legal. Move point
147 ;; past them. Note that comments and whitespace may be
148 ;; interspersed as well.
149 ((eq (following-char) ?\073)
152 ;; Move point past a single line comment.
154 (gud-jdb-skip-single-line-comment))
156 ;; Move point past a traditional or documentation comment.
158 (gud-jdb-skip-traditional-or-documentation-comment))
160 ;; Move point past a package statement, but save the PackageName.
161 ((looking-at "package")
163 (gud-jdb-skip-whitespace-and-comments)
165 (gud-jdb-skip-id-ish-thing)
166 (setq p (concat (buffer-substring s (point)) "."))
167 (gud-jdb-skip-whitespace-and-comments)
168 (if (eq (following-char) ?\073)
171 ;; Move point past an import statement.
172 ((looking-at "import")
174 (gud-jdb-skip-whitespace-and-comments)
175 (gud-jdb-skip-id-ish-thing)
176 (gud-jdb-skip-whitespace-and-comments)
177 (if (eq (following-char) ?\073)
180 ;; Move point past the various kinds of ClassModifiers.
181 ((looking-at "public")
183 ((looking-at "abstract")
185 ((looking-at "final")
188 ;; Move point past a ClassDeclaration, but save the class
190 ((looking-at "class")
192 (gud-jdb-skip-whitespace-and-comments)
194 (gud-jdb-skip-id-ish-thing)
196 l (nconc l (list (concat p (buffer-substring s (point)))))))
197 (gud-jdb-skip-block))
199 ;; Move point past an interface statement.
200 ((looking-at "interface")
202 (gud-jdb-skip-block))
204 ;; Anything else means the input is invalid.
206 (message "Error parsing file %s." file)
207 (throw 'abort nil))))))
210 (defun gud-jdb-build-class-source-alist-for-file (file)
214 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
216 ;; Return an alist of fully qualified classes and the source files
217 ;; holding their definitions. SOURCES holds a list of all the source
219 (defun gud-jdb-build-class-source-alist (sources)
220 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
225 'gud-jdb-build-class-source-alist-for-file
227 (kill-buffer gud-jdb-analysis-buffer)
228 (setq gud-jdb-analysis-buffer nil)))