{"id":49,"date":"2012-07-31T14:24:00","date_gmt":"2012-07-31T14:24:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=49"},"modified":"2012-07-31T14:24:00","modified_gmt":"2012-07-31T14:24:00","slug":"switching-to-a-java-package-implementation-in-emacs","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=49","title":{"rendered":"Switching to a Java package implementation in Emacs"},"content":{"rendered":"<p>There already quite a few methods to switch between header and implementation of C and C++ sources in Emacs. Maybe I will put one of those here as well later. But now I needed something different: In a big project, I would like to point at a line like <span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">import com.foo.tech.implementation.SomeClass;<\/span> and Emacs automatically visits the file where this class has been implemented. I wrote two small Elisp functions to perform this:  <\/p>\n<pre>(defun my-switch-to-java-implementation (packagename classname)<br \/>  \"Tries to switch to the java source file containing the package packagename and<br \/>the class classname.\"<br \/>  (with-temp-buffer<br \/>    (shell-command-to-string<br \/>     (format \"%s %s -iname '*.java' | xargs egrep -l '%s' | sort -u | xargs egrep -l 'class %s' | head -n 1\" <br \/>             \"find\"<br \/>             \"path\/to\/your\/java\/sources\"<br \/>             packagename<br \/>             classname<br \/>             )<br \/>     (current-buffer) )<br \/>    (if (= (count-lines (point-min) (point-max) ) 1)<br \/>        (progn<br \/>          (search-forward \"n\")<br \/>          (replace-match \"\")<br \/>          (find-file (buffer-string))<br \/>          )<br \/>      (message \"Could not find suitable implementation.\")<br \/>      )<br \/>    )<br \/>  )<br \/><br \/>(defun my-switch-to-java-implementation-at-point ()<br \/>  \"Tries to switch to the java source file which contains the package and class imported<br \/>in the current line.\"<br \/>  (interactive)<br \/>  (save-excursion<br \/>    (beginning-of-line)<br \/>    (if (looking-at \"^import \\(.*\\)\\.\\(.+\\);$\")<br \/>        (let ( (packagename (buffer-substring (match-beginning 1) (match-end 1) ) )<br \/>               (classname (buffer-substring (match-beginning 2) (match-end 2) ) ) )<br \/>          (my-switch-to-java-implementation packagename classname)<br \/>          )<br \/>      (message \"This is not an import\")<br \/>      )<br \/>    )<br \/>  )<br \/><\/pre>\n<p>You basically call <span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">my-switch-to-java-implementation-at-point<\/span> (e.g. by binding it to a key) when you are on the <span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">import<\/span> line. Emacs then launches find and grep to look for the definition of your class. Of course, using something like GNU Global would be faster, but also a bit more tricky, since it does not use the packagename, but rather only the class name, which might be used multiple times in your project.  <br \/>Also note that you have to set the path to your Java source files. I suggest you make a customizable variable of that, or use your ede-project-root or something similar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There already quite a few methods to switch between header and implementation of C and C++ sources in Emacs. Maybe I will put one of those here as well later. But now I needed something different: In a big project, I would like to point at a line like import com.foo.tech.implementation.SomeClass; and Emacs automatically visits &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=49\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Switching to a Java package implementation in Emacs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,6,37,8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/49"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}