4 ;; SUMMARY: Support routines for Java inheritance browsing.
5 ;; USAGE: GNU Emacs Lisp Library
6 ;; KEYWORDS: c, oop, tools
11 ;; ORIG-DATE: 01-Aug-95
12 ;; LAST-MOD: 10-May-01 at 05:39:36 by Bob Weiner
14 ;; Copyright (C) 1995, 1996, 1997 BeOpen.com
15 ;; See the file BR-COPY for license information.
17 ;; This file is part of the OO-Browser.
22 ;;; ************************************************************************
23 ;;; Other required Elisp libraries
24 ;;; ************************************************************************
31 ;;; ************************************************************************
32 ;;; User visible variables
33 ;;; ************************************************************************
35 (defvar java-class-keyword
36 "\\(class\\|interface\\)[ \t\n\r]+"
37 "*Keyword regexp preceding a java class declaration or definition.")
39 (defvar java-lib-search-dirs nil
40 "List of directories below which java Library source files are found.
41 Subdirectories of Library source are also searched. A Library is a stable
44 (defvar java-sys-search-dirs nil
45 "List of directories below which java System source files are found.
46 Subdirectories of System source are also searched. A System class is one
47 that is not yet reusable and is likely to change before release.")
49 (defvar java-package-name nil
50 "Name of current packge if any. Nil otherwise.")
52 (defconst java-narrow-view-to-class nil
53 "*Non-nil means narrow buffer to just the matching class definition when displayed.")
55 ;;; ************************************************************************
56 ;;; Internal variables
57 ;;; ************************************************************************
59 (defconst java-class-modifier-keyword
60 "\\(public[ \t\n\r]+\\|final[ \t\n\r]+\\|abstract[ \t\n\r]+\\)*")
62 (defconst java-class-name-before
63 (concat "^[ \t]*" java-class-modifier-keyword java-class-keyword)
64 "Regexp preceding the class name in a class definition.")
66 (defconst java-class-name-after
67 "[ \t\n\r]*\\(\{\\|extends[^\{\(\;]+\\|implements[^\{\(\;]+\\)"
68 "Regexp following the class name in a class definition.
69 Last character matched is a curly brace if the class does not implement any
70 protocols or inherit explicitly from any classes.")
72 (defconst java-identifier-chars "_$.a-zA-Z0-9"
73 "String of chars and char ranges that may be used within a Java identifier.")
75 (defconst java-return-type-chars java-identifier-chars
76 "String of chars and char ranges that may be used within a Java return type identifier.")
78 (defconst java-identifier (concat "\\([_$a-zA-Z][" java-identifier-chars "]*\\)")
79 "Regular expression matching a Java identifier.")
81 (defconst java-class-def-regexp
82 (concat java-class-name-before java-identifier java-class-name-after)
83 "Regular expression used to match to class definitions in source text.
84 The `java-class-type-grpn' grouping matches to one of the literals `class' or
85 `interface', indicating the type of declaration found. The class name
86 identifier is grouping `java-class-def-name-grpn'. The
87 first part of the `java-class-def-derived-grpn' grouping matches to the first
88 `extends' or `implements' keyword, unless the class does not explicitly
89 inherit from any class or interface, in which case this grouping matches to
92 (defconst java-class-type-grpn 2)
93 (defconst java-class-def-name-grpn 3)
94 (defconst java-class-def-derived-grpn 4)
96 (defconst java-lang-prefix "java-"
97 "Prefix string that starts \"br-java.el\" symbol names.")
99 ;; Sample expressions:
100 ;; class DataOutputStream extends FilterOutputStream implements DataOutput {
101 ;; class VectorEnumerator implements Enumeration {
102 ;; class Hashtable extends Dictionary implements Cloneable {
103 ;; class RandomAccessFile implements DataOutput, DataInput {
104 (defconst java-class-parent-regexp
105 (concat "\\(implements\\|extends\\|,\\)[ \t\n\r]*" java-identifier "[ \t\n\r]*")
106 "Matches to a single class or interface name inherited by a class.
107 Keyword or comma preceding the name is group `java-parent-prefix-grpn'.
108 The name itself is group `java-parent-name-grpn'.")
109 ;; part 2 of original
110 ;; "\\(\\(public\\|private\\|protected\\|final\||abstract\\|implements\\|extends\\)[,]?[ \t\n\r]+\\)?\\)?"
112 (defconst java-parent-prefix-grpn 1)
113 (defconst java-parent-name-grpn 2)
115 (defconst java-interface-parent-regexp
116 (concat "\\(extends\\|,\\)[ \t\n\r]*" java-identifier "[ \t\n\r]*")
117 "Matches to a single interface name inherited by an interface.
118 The `extends' keyword or the comma preceding the name is group
119 `java-parent-prefix-grpn'. The name itself is group
120 `java-parent-name-grpn'.")
122 (defconst java-package-name-regexp
123 (concat "[ \t\n\r]*" java-identifier "[ \t\n\r]*\;")
124 "Regexp matching a package statement. Package name is java-package-name-grpn.")
126 (defconst java-package-name-grpn 1)
128 (defconst java-package-word-regexp
129 "\\([a-zA-z_0-9]*\\)\\.?"
130 "Return a single component of a package name.")
132 (defconst java-static-init-regexp
133 "[ \t\n\r]*static[ \t\n\r]+\{"
134 "Regexp matching start of static initializer block.")
136 (defvar java-package-htable
138 "Hash table of split package names.")
140 (defconst java-src-file-regexp "[^.]\\.\\(java\\)$"
141 "Regular expression matching a unique part of java source or header file name and no others.")
143 (defvar java-children-htable nil
144 "Htable whose elements are of the form: (LIST-OF-CHILD-CLASSES . CLASS-NAME).
145 Used to traverse java inheritance graph. `br-build-children-htable' builds
147 (defvar java-parents-htable nil
148 "Htable whose elements are of the form: (LIST-OF-PARENT-CLASSES . CLASS-NAME).
149 Used to traverse java inheritance graph. `br-build-parents-htable' builds
151 (defvar java-paths-htable nil
152 "Htable whose elements are of the form: (LIST-OF-CLASS-NAMES . FILE-PATH).
153 FILE-PATH gives the location of classes found in LIST-OF-CLASS-NAMES.
154 `br-build-paths-htable' builds this list.")
157 (defvar java-lib-parents-htable nil
158 "Htable whose elements are of the form: (LIST-OF-PARENT-CLASSES . CLASS-NAME).
159 Only classes from stable software libraries are used to build the list.")
160 (defvar java-lib-paths-htable nil
161 "Htable whose elements are of the form: (LIST-OF-CLASS-NAMES . FILE-PATH).
162 FILE-PATH gives the location of classes found in LIST-OF-CLASS-NAMES.
163 Only classes from stable software libraries are used to build the list.")
165 (defvar java-sys-parents-htable nil
166 "Htable whose elements are of the form: (LIST-OF-PARENT-CLASSES . CLASS-NAME).
167 Only classes from systems that are likely to change are used to build the
169 (defvar java-sys-paths-htable nil
170 "Alist whose elements are of the form: (LIST-OF-CLASS-NAMES . FILE-PATH).
171 FILE-PATH gives the location of classes found in LIST-OF-CLASS-NAMES.
172 Only classes from systems that are likely to change are used to build the
175 (defvar java-lib-prev-search-dirs nil
176 "Used to check if `java-lib-classes-htable' must be regenerated.")
177 (defvar java-sys-prev-search-dirs nil
178 "Used to check if `java-sys-classes-htable' must be regenerated.")
180 (defvar java-env-spec nil
181 "Non-nil value means Environment specification has been given but not yet built.
182 Nil means current Environment has been built, though it may still require
185 ;;; ************************************************************************
186 ;;; Internal functions
187 ;;; ************************************************************************
189 (defun java-get-classes-from-source (filename &optional skip-tags
191 "Scans FILENAME and returns cons of class list with parents-class alist.
192 Handles multiple inheritance. Assumes file existence and readability have
193 already been checked.
194 With optional SKIP-TAGS non-nil, does not compute and store lookup tags
195 for member definitions. If SKIP-TAGS is nil, normally a cleanup
196 function is called after scanning the members. SKIP-TAGS-CLEANUP
197 non-nil suppresses this action."
198 (let ((no-kill (get-file-buffer filename))
199 class-name-end classes class has-parents interface-p
200 open-brace-point end parents-start parents parent-cons parent-list
201 signatures superclass-flag)
204 (funcall br-view-file-function filename))
205 ;; Static initializers confuse the parser and don't define anything
206 ;; that we need, so remove them.
207 (java-strip-static-code)
208 ;; Is more than one package statement allowed?
209 (setq java-package-name (java-get-package-name))
213 (goto-char (point-min))
214 (while (re-search-forward java-class-def-regexp nil t)
216 (not (eq ?\{ (char-after (match-beginning
217 java-class-def-derived-grpn))))
218 parents-start (match-beginning java-class-def-derived-grpn)
219 interface-p (string-equal
221 (match-beginning java-class-type-grpn)
222 (match-end java-class-type-grpn))
225 class-name-end (match-end java-class-def-name-grpn)
227 ;; Now since we've saved all the match expressions we need
228 ;; from our last regexp match, we can call functions which
229 ;; change the match data below here.
230 class (java-normalize-class-match)
233 (if interface-p (setq class (concat "<" class ">")))
234 (goto-char parents-start)
236 ;; Return parents as a list.
238 (setq parent-list (java-scan-interface-parents end))
239 (setq parent-list (java-scan-class-parents end)
240 superclass-flag (car parent-list)
241 parent-list (cdr parent-list))))
242 (if (not (or superclass-flag interface-p (equal class "Object")))
243 ;; All classes have Object as an ancestor, so if
244 ;; no superclass was found, add Object to the parent-list.
245 (setq parent-list (cons "Object" parent-list)))
247 ;; Ensure class name is not within a comment.
248 (if (c-within-comment-p)
249 (progn (search-forward "*/" nil t)
250 (setq class nil parent-cons nil))
251 (setq parent-cons (cons parent-list class)
252 classes (cons class classes)
253 parents (cons parent-cons parents))
257 ;; Add this interface def to the default
258 ;; [interface] class.
260 (cons (java-feature-normalize
261 class java-default-interface-class
264 ;; Scan members defined within class or interface
265 (goto-char class-name-end)
266 (if (search-forward "\{" nil t)
267 (progn (setq open-brace-point (point))
269 ;; Move to class close brace but ignore
270 ;; any error if braces are unbalanced.
271 ;; Let the compiler tell the user about
273 (if (condition-case ()
274 (progn (forward-sexp) t)
280 class open-brace-point
284 (java-output-feature-tags filename signatures)
285 (or skip-tags-cleanup (br-feature-build-htables)))
287 (progn (set-buffer-modified-p nil)
288 (kill-buffer (current-buffer))))
289 (cons classes (delq nil parents))))
291 (defun java-get-package-name()
292 "Return the package name of the current file."
294 (goto-char (point-min))
295 (if (re-search-forward java-package-name-regexp nil t)
296 (br-buffer-substring (match-beginning java-package-name-grpn)
297 (match-end java-package-name-grpn))
300 (defun java-split-identifier (name)
301 "Return list of component words (in reverse order) of the given NAME."
302 (or (hash-lookup name java-package-htable)
305 (while (and (not (null s)) (> (length s) 0))
306 (setq start (string-match java-package-word-regexp s))
309 (setq tmp (substring s (match-beginning 1) (match-end 1)))
310 (setq s (substring s (match-end 0)))
311 (setq words (cons tmp words)))))
312 (hash-add words java-package-name java-package-htable))))
314 (defun java-normalize-class-name (name)
315 "Normalize class NAME to include the package name that defines it."
316 ;; Currently incomplete. The defined class has a package name, but
317 ;; the parents do not. How do we match the parents to the correct
318 ;; class if there are multiple matches?
319 (or (car (java-split-identifier name))
320 (if (null java-package-name)
321 (car (java-split-identifier name))
322 ;; Note: maybe allow user to pick how many words to prepend.
323 (let ((prefix (car (java-split-identifier java-package-name))))
324 (if (and prefix (> (length prefix) 0))
325 (concat prefix "." (car (java-split-identifier name)))
326 (car (java-split-identifier name)))))))
328 (defun java-class-definition-regexp (class &optional regexp-flag)
329 "Return regexp to uniquely match the definition of CLASS name.
330 Optional REGEXP-FLAG non-nil means CLASS has already been quoted for use in a
332 (if (string-match "\\`<\\(.+\\)>\\'" class)
333 ;; Strip interface delimiters which don't appear within class or
334 ;; interface declarations.
335 (setq class (substring class (match-beginning 1) (match-end 1))))
336 (concat "^[ \t]*" java-class-modifier-keyword
337 java-class-keyword (if regexp-flag class (regexp-quote class))
338 java-class-name-after))
340 (defun java-normalize-class-match ()
341 "After a regexp match to a class definition, return the matching class name."
342 (java-normalize-class-name
343 (br-buffer-substring (match-beginning java-class-def-name-grpn)
344 (match-end java-class-def-name-grpn))))
346 (defun java-scan-class-parents (end)
347 "Return (superclass-flag parent-list) from a java class declaration preceding END point.
348 The superclass-flag is t if the class has an actual concrete parent. The
349 parent-list contains that parent and any interfaces to which the class
350 directly conforms. Although Java permits only single inheritance via the
351 `extends' clause, the `implements' clause supports inheritance of multiple
352 interfaces and thus allows classes to have a limited form of multiple
353 parents. Point must be before the `implements' or `extends' keyword that
354 precedes the first parent name."
355 (let ((superclass-flag) (superclass-here) parent-list parent)
356 (while (re-search-forward java-class-parent-regexp end t)
357 (setq superclass-here
358 (string-equal (br-buffer-substring
359 (match-beginning java-parent-prefix-grpn)
360 (match-end java-parent-prefix-grpn))
362 superclass-flag (or superclass-flag superclass-here)
363 parent (java-normalize-class-name
365 (match-beginning java-parent-name-grpn)
366 (match-end java-parent-name-grpn))))
369 ;; Mark as an interface
370 (setq parent (concat "<" parent ">")))
371 (setq parent-list (cons parent parent-list)))
372 (cons superclass-flag (nreverse parent-list))))
374 (defun java-scan-interface-parents (end)
375 "Return a list of the parent interfaces from a java interface declaration which precede END point.
376 Point must be before the `extends' keyword that precedes the first parent name."
377 (let (parent-list parent)
378 (while (re-search-forward java-interface-parent-regexp end t)
379 (setq parent (concat "<"
380 (java-normalize-class-name
382 (match-beginning java-parent-name-grpn)
383 (match-end java-parent-name-grpn)))
385 parent-list (cons parent parent-list)))
386 (nreverse parent-list)))
388 (defun java-get-parents-from-source (filename class)
389 "Scan source in FILENAME and return list of parents of CLASS.
390 Assume file existence has already been checked."
391 (cond ((null class) nil)
392 ((equal filename br-null-path)
393 ;; This means there is no source for this class, so
394 ;; if this is not the Object class or an interface, since all
395 ;; other classes without parents have Object as an implicit
396 ;; parent, return Object as the sole parent.
397 (if (not (or (if (stringp class) (eq (aref class 0) ?\<))
398 (equal class "Object")))
400 (t (car (car (br-rassoc
402 (cdr (java-get-classes-from-source filename t))))))))
404 (defun java-select-path (paths-htable-elt &optional feature-p)
405 "Select proper pathname from PATHS-HTABLE-ELT based upon value of optional FEATURE-P.
406 Selection is between path of class definition and path for features associated
408 (let ((elt (cdr paths-htable-elt)))
410 (if feature-p (cdr elt) (car elt))
411 ;; Both paths are the same.
414 (defun java-set-case (type)
415 "Return string TYPE identifier for use as a class name."
418 (defun java-set-case-type (class-name)
419 "Return string CLASS-NAME for use as a type identifier."
422 (defun java-to-class-end ()
423 "Assuming point is at start of class, move to start of line after end of class."
427 (error (progn (or (re-search-forward "^}" nil t)
428 (goto-char (point-max))))))
431 (defalias 'java-to-comments-begin 'br-c-to-comments-begin)
433 ;; Static initializers confuse the parser, and don't define anything
435 (defun java-strip-static-code ()
436 "Strip the static initializers from this buffer."
437 (let (buffer-read-only start open-brace)
439 (goto-char (point-min))
440 (while (re-search-forward java-static-init-regexp (point-max) t)
441 (setq start (match-beginning 0)
442 open-brace (1- (match-end 0)))
443 (goto-char open-brace)
444 (let ((before (point)))
445 (if (eq (following-char) ?\{)
449 (delete-region before (point))
450 (delete-region start open-brace))))))