This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 493393 - ClassCastException from KeywordCompletionProposalComputer & IdentifierCompletionProposalComputer witn Neon M7
Summary: ClassCastException from KeywordCompletionProposalComputer & IdentifierComplet...
Status: RESOLVED FIXED
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.8 RC3   Edit
Assignee: Victor Rubezhny CLA
QA Contact: Victor Rubezhny CLA
URL:
Whiteboard: RHT PMC_Approved
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-10 22:12 EDT by Angelo ZERR CLA
Modified: 2016-05-26 21:27 EDT (History)
8 users (show)

See Also:
vrubezhny: pmc_approved? (david_williams)
vrubezhny: pmc_approved? (raghunathan.srinivasan)
vrubezhny: pmc_approved? (naci.dai)
vrubezhny: pmc_approved? (neil.hauge)
cbridgha: pmc_approved+
vrubezhny: pmc_approved? (ccc)
vrubezhny: pmc_approved+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Angelo ZERR CLA 2016-05-10 22:12:25 EDT
The Eclipse Neon M7 provides KeywordCompletionProposalComputer & IdentifierCompletionProposalComputer classes which doesn't use "instanceof" to check that the given ContentAssistInvocationContext is a JavaContentAssistInvocationContext:

```
public class IdentifierCompletionProposalComputer implements IJavaCompletionProposalComputer {

  public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
		JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
```

It's a big problem for typescript.java which is based on JSDT and which defines TypeScriptContentAssistInvocationContext (see issue at 
https://github.com/angelozerr/typescript.java/issues/56)

So with oEclipse Neon M7, when I do completion with my TypeScriptEditor based on JSDT Edit, I have a popup error with this error

```
java.lang.ClassCastException: ts.eclipse.ide.jsdt.internal.ui.editor.contentassist.TypeScriptContentAssistInvocationContext cannot be cast to org.eclipse.wst.jsdt.ui.text.java.JavaContentAssistInvocationContext
	at org.eclipse.wst.jsdt.internal.ui.text.java.KeywordCompletionProposalComputer.computeCompletionProposals(KeywordCompletionProposalComputer.java:45)

```

and 

```
java.lang.ClassCastException: ts.eclipse.ide.jsdt.internal.ui.editor.contentassist.TypeScriptContentAssistInvocationContext cannot be cast to org.eclipse.wst.jsdt.ui.text.java.JavaContentAssistInvocationContext
	at org.eclipse.wst.jsdt.internal.ui.text.java.IdentifierCompletionProposalComputer.computeCompletionProposals(IdentifierCompletionProposalComputer.java:49)

```

It should b every cool if you could check that ContentAssistInvocationContext is an instanceof JavaContentAssistInvocationContext:

```
public class IdentifierCompletionProposalComputer implements IJavaCompletionProposalComputer {

  public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
  if (context instanceof JavaContentAssistInvocationContext) {
		JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
```

for :

 * http://git.eclipse.org/c/jsdt/webtools.jsdt.git/tree/bundles/org.eclipse.wst.jsdt.ui/src/org/eclipse/wst/jsdt/internal/ui/text/java/IdentifierCompletionProposalComputer.java
 * http://git.eclipse.org/c/jsdt/webtools.jsdt.git/tree/bundles/org.eclipse.wst.jsdt.ui/src/org/eclipse/wst/jsdt/internal/ui/text/java/KeywordCompletionProposalComputer.java

For your infomration, I have created my own TypeScriptContentAssistInvocationContext, because I need IResource and I don't use IJavaCompilationUnit. If http://git.eclipse.org/c/jsdt/webtools.jsdt.git/tree/bundles/org.eclipse.wst.jsdt.ui/src/org/eclipse/wst/jsdt/ui/text/java/JavaContentAssistInvocationContext.java could just define a getter for the Editor, I think I could use directly JavaContentAssistInvocationContext to retrieve my IResource from the getEditor getter.

Hope this issue will be able to fixed for Neon release, otherwise typescript.java will not work with Eclipse Neon release -(  (we must desactivate the whole JSDT completions to avoid having those 2 popup errors)

Many thanks for your help!
Comment 1 Victor Rubezhny CLA 2016-05-24 14:33:55 EDT
@Angelo

JavaContentAssistInvocationContext defines method getCompilationUnit() that returns a CompilationUnit for you. You can use its getResource() method to get the underlined resource.
Comment 2 Eclipse Genie CLA 2016-05-24 14:44:15 EDT
New Gerrit change created: https://git.eclipse.org/r/73523
Comment 3 Victor Rubezhny CLA 2016-05-24 15:01:21 EDT
Information for PMC Defect Review:

* Explain why you believe this is a stop-ship defect. Or, if it is a "hotbug"
(requested by an adopter) please document it as such.

The fix adds the required "instanceof" checks to Completion Proposal Computers and two new methods required by adopters.

* Is there a work-around? If so, why do you believe the work-around is
insufficient?

No.

* How has the fix been tested? Is there a test case attached to the bugzilla
record? Has a JUnit Test been added?

Tested manually.

* Give a brief technical overview. Who has reviewed this fix?

The fix is reviewed by Victor Rubezhny

* What is the risk associated with this fix?

There is no risk.
Comment 4 Angelo ZERR CLA 2016-05-24 16:12:32 EDT
Thanks @Victor for the fix!

> JavaContentAssistInvocationContext defines method getCompilationUnit() that returns a CompilationUnit for you. You can use its getResource() method to get the underlined resource.

Yes I know, I do that inside tern.java. But in typescript.java context (and tern.java too), I don't need this compilation unit (I would like to avoid 2 parsing (one for ICompilationUnit although I don't need it, and one parse done with TypeScript tsserver) 

More with JSDT 2.0.0, teh compute of ICompilationUnit is slow because of esprima parser.

With typescript.java I have redefined my own editor which uses JSDT features like completion, syntax coloration, etc and I don't compute an ICompilationUnit (it's very fast).

That's why it should be very cool if we could retrieve the editor (just by adding a getter for the editor) from JavaContentAssistInvocationContext (after that I could retrieve the resource)
Comment 5 Victor Rubezhny CLA 2016-05-24 16:30:30 EDT
@Angelo,

I have added two new methods to the context:

- getResource()
- getEditor(),

but you have not to use getResource() because it uses ICompilationUnit to find the resource - so it wouldn't work for you. Instead of this, use getEditor() method to get the editor then follow your plan to retrieve a resource from that editor.
Comment 6 Angelo ZERR CLA 2016-05-24 16:55:47 EDT
Very cool @Victor, many thanks!
Comment 7 Chuck Bridgham CLA 2016-05-26 14:04:26 EDT
looks good thanks
Comment 9 Angelo ZERR CLA 2016-05-26 21:27:05 EDT
Works like a charm, thank's Victor!