Re: Trouble with Java package imports

David Ventimiglia <[email protected]> Tue, 20 Sep 2016 05:52:42 -0700
Newsgroups gmane.emacs.semantic
Message-ID <CALaE=t8HCjBB6KYHWvanA6av-WMGgh7VWGu9QNWjuv6PryVFNg@mail.gmail.com>
--===============1440168041441928386==
Content-Type: multipart/alternative; boundary=001a11409da6b4ed17053cefe821

--001a11409da6b4ed17053cefe821
Content-Type: text/plain; charset=UTF-8

I've a few clues about the error with Java package includes.

First, with a package include (i.e., "include java.util.*") the tag at
point has a proxy (:proxy keyword)--whatever that is--that sends us down a
branch starting with semanticdb-javap-resolve-proxy.  Without a package
include (i.e., "include java.util.Map") the proxy is nil, there's no
proxy-function, and we steam on ahead.  See semantic-tag-resolve-proxy in
tag.el:

(defun semantic-tag-resolve-proxy (tag)
  "Resolve the proxy in TAG.
The return value is whatever format the proxy was setup as.
It should be a list of complete tags.
If TAG has no proxy, then just return tag."
  (let* ((proxy (semantic--tag-get-property tag :proxy))
(function (get proxy 'proxy-function))
(data (get proxy 'proxy-data)))
*    (if proxy*
* (funcall function data tag)*
      tag)))


In that branch, we eventually end up in semanticdb-normalize-tags in
db-javap.el where a let* binds realtable to the value
of (semanticdb-jar-extract-and-save-tags obj tfn), which is determined to
set it to nil.  The problem is that the next let* expression binds
foundtags to (semanticdb-find-tags-by-name-method realtable
(semantic-tag-name T)), and realtable has just been set to nil.  See
semanticdb-normalize-tags in db-javap.el:

(defmethod semanticdb-normalize-tags ((obj semanticdb-table-jar-directory)
tags)
  "Convert tags found by our java directory table into a complete tag.
The default tag just has a name, type, and the filename.  Normalize by
loading in the file it belongs to, and looking up that symbol in the file
and returning that tag instead."
  (let ((tagret nil)
(parentdb (oref obj parent-db)))
    (dolist (T tags)
      (let* ((tfn (semantic-tag-file-name T))
*     (realtable (semanticdb-jar-extract-and-save-tags obj tfn))*
*     (foundtags (semanticdb-find-tags-by-name-method*
* realtable (semantic-tag-name T))))*
(dolist (FT foundtags)
 (semantic--tag-put-property FT :filename tfn)
 (setq tagret (cons FT tagret)))))
    tagret))


Some questions leap to mind.

   1. What is a tag proxy and is it correct for the tag to have one with a
   package import (i.e., "import java.util.*") and not have one otherwise?
   2. Is it correct for semanticdb-jar-extract-and-save-tags to return nil
   in this case?
   3. Should semanticdb-find-tags-by-name-method be able to cope with a nil
   argument to the realtable parameter, without throwing an error?
   4. If it were able to cope such that execution would continue, would the
   analyzer do the right thing for a package import, or would it break
   somewhere else down the line?

I'll try to answer these questions in the coming days.  Stay tuned!

Cheers,
David

On Sun, Sep 18, 2016 at 11:57 AM, David Ventimiglia <[email protected]>
wrote:

> Hi!
>
> I'm having trouble with the Semantic Analyzer and "package imports" for
> Java programs.  I've an example Java program that looks like this:
>
> import java.util.*;
>> import java.util.HashMap;
>> public class HelloWorld2 {
>>     public static void main (String[] args) {
>> System.out.println("Hello, World!");
>> Map m = new HashMap();
>> m.
>>     }
>> }
>
>
> When I put point after the "m." and try semantic-ia-complete-symbol-menu
> (for example) it fails with this message:
>
> Cannot find types for `"m"'
>
>
> If I try semantic-analyze-current-context, the output indicates that it
> encountered this error (full message below):
>
> Method semanticdb-find-tags-by-name-method called on nil
>
>
> These problems don't occur if I replace the "import java.util.*;" in the
> Java file with "import java.utl.Map" and so I'd never encounter them if I
> didn't used wildcards in the import statements.
>
> Any idea what's going on here?
>
> I have a Docker image that reproduces this behavior, if anyone's game for
> trying it out.  It uses Ubuntu, Emacs 24.5.1, the latest CEDET (as of
> 2016/09/18), and OpenJDK 8.  If you have Docker installed, you should be
> ale to get the image with this command:
>
> docker pull dventimi/docker-emacs
>
>
> You can then launch a container that opens Emacs on a version of the Java
> file with package imports, with this command:
>
> docker run -t -i dventimi/docker-emacs emacs /root/Scratch/HelloWorld2.java
>
>
> Likewise, you can launch a container that opens Emacs on a version of the
> Java file without package imports, with this command:
>
> docker run -t -i dventimi/docker-emacs emacs /root/Scratch/HelloWorld.java
>
>
> The Semantic Analyzer works in the second case, but not in the first
> case.  I've been groping my way through this with the debugger, and will
> continue to do so, but I thought I'd throw this out there to see if anyone
> else has a clue as to what the problem might be.  Thanks!
>
> Best,
> David
>
> ====== Output of semantic-analyze-current-context =======
>
> Context Type: #<semantic-analyze-context context>
> Bounds: (186 . 186)
> Prefix: "m"
>         ""
> Prefix Classes: 'function
>                 'variable
>                 'type
> Prefix Types: <none>
> Encountered Errors: '(error "Method semanticdb-find-tags-by-name-method
> called on nil")
> --------
> -> ScopeTypes: class HelloWorld2
> -> Parents: class HelloWorld2
> -> Scope: void main(String[])
> -> Local Args: String[] args
> -> Local Vars: String[] args
>                 this
>                Map m
>
>

--001a11409da6b4ed17053cefe821
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I&#39;ve a few clues about the error with Java package inc=
ludes. =C2=A0<div><br></div><div>First, with a package include (i.e., &quot=
;include java.util.*&quot;) the tag at point has a proxy (:proxy keyword)--=
whatever that is--that sends us down a branch starting with semanticdb-java=
p-resolve-proxy.=C2=A0 Without a package include (i.e., &quot;include java.=
util.Map&quot;) the proxy is nil, there&#39;s no proxy-function, and we ste=
am on ahead.=C2=A0 See semantic-tag-resolve-proxy in tag.el:</div><div><br>=
</div><blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div>=
<div><font face=3D"monospace, monospace">(defun semantic-tag-resolve-proxy =
(tag)</font></div></div><div><div><font face=3D"monospace, monospace">=C2=
=A0 &quot;Resolve the proxy in TAG.</font></div></div><div><div><font face=
=3D"monospace, monospace">The return value is whatever format the proxy was=
 setup as.</font></div></div><div><div><font face=3D"monospace, monospace">=
It should be a list of complete tags.</font></div></div><div><div><font fac=
e=3D"monospace, monospace">If TAG has no proxy, then just return tag.&quot;=
</font></div></div><div><div><font face=3D"monospace, monospace">=C2=A0 (le=
t* ((proxy (semantic--tag-get-property tag :proxy))</font></div></div><div>=
<div><font face=3D"monospace, monospace"><span class=3D"gmail-Apple-tab-spa=
n" style=3D"white-space:pre">	</span> (function (get proxy &#39;proxy-funct=
ion))</font></div></div><div><div><font face=3D"monospace, monospace"><span=
 class=3D"gmail-Apple-tab-span" style=3D"white-space:pre">	</span> (data (g=
et proxy &#39;proxy-data)))</font></div></div><div><div><font face=3D"monos=
pace, monospace"><b>=C2=A0 =C2=A0 (if proxy</b></font></div></div><div><div=
><font face=3D"monospace, monospace"><b><span class=3D"gmail-Apple-tab-span=
" style=3D"white-space:pre">	</span>(funcall function data tag)</b></font><=
/div></div><div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=
=A0 tag)))</font></div></div></blockquote><div><br></div><div>In that branc=
h, we eventually end up in semanticdb-normalize-tags in db-javap.el where a=
 let* binds realtable to the value of=C2=A0(semanticdb-jar-extract-and-save=
-tags obj tfn), which is determined to set it to nil.=C2=A0 The problem is =
that the next let* expression binds foundtags to (semanticdb-find-tags-by-n=
ame-method=C2=A0realtable (semantic-tag-name T)), and realtable has just be=
en set to nil.=C2=A0 See semanticdb-normalize-tags in db-javap.el:</div><di=
v><br></div><blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"=
><div><div><font face=3D"monospace, monospace">(defmethod semanticdb-normal=
ize-tags ((obj semanticdb-table-jar-directory) tags)</font></div></div><div=
><div><font face=3D"monospace, monospace">=C2=A0 &quot;Convert tags found b=
y our java directory table into a complete tag.</font></div></div><div><div=
><font face=3D"monospace, monospace">The default tag just has a name, type,=
 and the filename.=C2=A0 Normalize by</font></div></div><div><div><font fac=
e=3D"monospace, monospace">loading in the file it belongs to, and looking u=
p that symbol in the file</font></div></div><div><div><font face=3D"monospa=
ce, monospace">and returning that tag instead.&quot;</font></div></div><div=
><div><font face=3D"monospace, monospace">=C2=A0 (let ((tagret nil)</font><=
/div></div><div><div><font face=3D"monospace, monospace"><span class=3D"gma=
il-Apple-tab-span" style=3D"white-space:pre">	</span>(parentdb (oref obj pa=
rent-db)))</font></div></div><div><div><font face=3D"monospace, monospace">=
=C2=A0 =C2=A0 (dolist (T tags)</font></div></div><div><div><font face=3D"mo=
nospace, monospace">=C2=A0 =C2=A0 =C2=A0 (let* ((tfn (semantic-tag-file-nam=
e T))</font></div></div><div><div><font face=3D"monospace, monospace"><b><s=
pan class=3D"gmail-Apple-tab-span" style=3D"white-space:pre">	</span> =C2=
=A0 =C2=A0 (realtable (semanticdb-jar-extract-and-save-tags obj tfn))</b></=
font></div></div><div><div><font face=3D"monospace, monospace"><b><span cla=
ss=3D"gmail-Apple-tab-span" style=3D"white-space:pre">	</span> =C2=A0 =C2=
=A0 (foundtags (semanticdb-find-tags-by-name-method</b></font></div></div><=
div><div><font face=3D"monospace, monospace"><b><span class=3D"gmail-Apple-=
tab-span" style=3D"white-space:pre">			</span> realtable (semantic-tag-name=
 T))))</b></font></div></div><div><div><font face=3D"monospace, monospace">=
<span class=3D"gmail-Apple-tab-span" style=3D"white-space:pre">	</span>(dol=
ist (FT foundtags)</font></div></div><div><div><font face=3D"monospace, mon=
ospace"><span class=3D"gmail-Apple-tab-span" style=3D"white-space:pre">	</s=
pan> =C2=A0(semantic--tag-put-property FT :filename tfn)</font></div></div>=
<div><div><font face=3D"monospace, monospace"><span class=3D"gmail-Apple-ta=
b-span" style=3D"white-space:pre">	</span> =C2=A0(setq tagret (cons FT tagr=
et)))))</font></div></div><div><div><font face=3D"monospace, monospace">=C2=
=A0 =C2=A0 tagret))</font></div></div></blockquote><div><br></div><div>Some=
 questions leap to mind.</div><div><ol><li>What is a tag proxy and is it co=
rrect for the tag to have one with a package import (i.e., &quot;import jav=
a.util.*&quot;) and not have one otherwise?</li><li>Is it correct for seman=
ticdb-jar-extract-and-save-tags to return nil in this case?</li><li>Should =
semanticdb-find-tags-by-name-method be able to cope with a nil argument to =
the realtable parameter, without throwing an error?</li><li>If it were able=
 to cope such that execution would continue, would the analyzer do the righ=
t thing for a package import, or would it break somewhere else down the lin=
e?</li></ol><div>I&#39;ll try to answer these questions in the coming days.=
=C2=A0 Stay tuned!</div></div><div><br></div><div>Cheers,</div><div>David</=
div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sun,=
 Sep 18, 2016 at 11:57 AM, David Ventimiglia <span dir=3D"ltr">&lt;<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt;=
</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hi!<div><=
br></div><div>I&#39;m having trouble with the Semantic Analyzer and &quot;p=
ackage imports&quot; for Java programs.=C2=A0 I&#39;ve an example Java prog=
ram that looks like this:</div><div><br></div><div><blockquote class=3D"gma=
il_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-le=
ft-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font f=
ace=3D"monospace, monospace">import java.util.*;<br>import java.util.HashMa=
p;<br>public class HelloWorld2 {<br>=C2=A0 =C2=A0 public static void main (=
String[] args) {<br><span style=3D"white-space:pre-wrap">	</span>System.out=
.println(&quot;Hello, World!&quot;);<br><span style=3D"white-space:pre-wrap=
">	</span>Map m =3D new HashMap();<br><span style=3D"white-space:pre-wrap">=
	</span>m.<br>=C2=A0 =C2=A0 }<br>}</font></blockquote></div><div><br></div>=
<div>When I put point after the &quot;m.&quot; and try <font face=3D"monosp=
ace, monospace">semantic-ia-complete-symbol-<wbr>menu</font> (for example) =
it fails with this message:</div><div><br></div><blockquote class=3D"gmail_=
quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-=
style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Cannot fin=
d types for `&quot;m&quot;&#39;</blockquote><div><br></div><div>If I try <f=
ont face=3D"monospace, monospace">semantic-analyze-current-<wbr>context</fo=
nt>, the output indicates that it encountered this error (full message belo=
w):</div><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-=
color:rgb(204,204,204);padding-left:1ex"><font face=3D"monospace, monospace=
">Method semanticdb-find-tags-by-name-<wbr>method called on nil</font></blo=
ckquote><div><br></div><div>These problems don&#39;t occur if I replace the=
 &quot;import java.util.*;&quot; in the Java file with &quot;import java.ut=
l.Map&quot; and so I&#39;d never encounter them if I didn&#39;t used wildca=
rds in the import statements. =C2=A0</div><div><br></div><div>Any idea what=
&#39;s going on here?</div><div><br></div><div>I have a Docker image that r=
eproduces this behavior, if anyone&#39;s game for trying it out.=C2=A0 It u=
ses Ubuntu, Emacs 24.5.1, the latest CEDET (as of 2016/09/18), and OpenJDK =
8.=C2=A0 If you have Docker installed, you should be ale to get the image w=
ith this command:</div><div><br></div><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:soli=
d;border-left-color:rgb(204,204,204);padding-left:1ex"><font face=3D"monosp=
ace, monospace">docker pull dventimi/docker-emacs</font></blockquote><div><=
br></div><div>You can then launch a container that opens Emacs on a version=
 of the Java file with package imports, with this command:</div><div><br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,20=
4);padding-left:1ex"><font face=3D"monospace, monospace">docker run -t -i d=
ventimi/docker-emacs emacs /root/Scratch/HelloWorld2.java</font></blockquot=
e><div><br></div><div>Likewise, you can launch a container that opens Emacs=
 on a version of the Java file without package imports, with this command:<=
/div><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0=
px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-colo=
r:rgb(204,204,204);padding-left:1ex"><font face=3D"monospace, monospace">do=
cker run -t -i dventimi/docker-emacs emacs /root/Scratch/HelloWorld.java</f=
ont></blockquote><div><br></div><div>The Semantic Analyzer works in the sec=
ond case, but not in the first case.=C2=A0 I&#39;ve been groping my way thr=
ough this with the debugger, and will continue to do so, but I thought I&#3=
9;d throw this out there to see if anyone else has a clue as to what the pr=
oblem might be.=C2=A0 Thanks!</div><div><br></div><div>Best,</div><div>Davi=
d</div><div><br></div><div><font face=3D"monospace, monospace">=3D=3D=3D=3D=
=3D=3D Output of semantic-analyze-current-<wbr>context =3D=3D=3D=3D=3D=3D=
=3D</font></div><div><br></div><div><div><font face=3D"monospace, monospace=
">Context Type: #&lt;semantic-analyze-context context&gt;</font></div><div>=
<font face=3D"monospace, monospace">Bounds: (186 . 186)</font></div><div><f=
ont face=3D"monospace, monospace">Prefix: &quot;m&quot;</font></div><div><f=
ont face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 &quot;&quot;<=
/font></div><div><font face=3D"monospace, monospace">Prefix Classes: &#39;f=
unction</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &#39;variable</font></div><div><f=
ont face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 &#39;type</font></div><div><font face=3D"monospace, monospac=
e">Prefix Types: &lt;none&gt;</font></div><div><font face=3D"monospace, mon=
ospace">Encountered Errors: &#39;(error &quot;Method semanticdb-find-tags-b=
y-name-<wbr>method called on nil&quot;)</font></div><div><font face=3D"mono=
space, monospace">--------</font></div><div><font face=3D"monospace, monosp=
ace">-&gt; ScopeTypes: class HelloWorld2</font></div><div><font face=3D"mon=
ospace, monospace">-&gt; Parents: class HelloWorld2</font></div><div><font =
face=3D"monospace, monospace">-&gt; Scope: void main(String[])</font></div>=
<div><font face=3D"monospace, monospace">-&gt; Local Args: String[] args</f=
ont></div><div><font face=3D"monospace, monospace">-&gt; Local Vars: String=
[] args</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 this</font></div><div><font face=
=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0Map m</font></div></div><div><br></div></div>
</blockquote></div><br></div>

--001a11409da6b4ed17053cefe821--


--===============1440168041441928386==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------

--===============1440168041441928386==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
cedet-semantic mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cedet-semantic

--===============1440168041441928386==--