Bug 418825 - Code assist fails when including project on class path that contains multiple class in a single file
Summary: Code assist fails when including project on class path that contains multiple...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.3.1   Edit
Hardware: Macintosh Mac OS X
: P3 normal (vote)
Target Milestone: 4.4 M3   Edit
Assignee: ANIRBAN CHAKRABORTY CLA
QA Contact:
URL:
Whiteboard:
Keywords: core
Depends on:
Blocks:
 
Reported: 2013-10-07 10:53 EDT by Michael Ellis CLA
Modified: 2013-11-13 07:17 EST (History)
4 users (show)

See Also:
jarthana: review+
manoj.palat: review+


Attachments
Zipped up workspace containing the two projects (7.45 MB, application/zip)
2013-10-07 11:12 EDT, Michael Ellis CLA
no flags Details
Screenshot of error message (111.60 KB, image/png)
2013-10-07 11:13 EDT, Michael Ellis CLA
no flags Details
Screenshot of Eclipse version info (171.35 KB, image/png)
2013-10-07 11:13 EDT, Michael Ellis CLA
no flags Details
Code patch (2.33 KB, patch)
2013-10-29 04:32 EDT, ANIRBAN CHAKRABORTY CLA
no flags Details | Diff
Code patch after review comments (1.67 KB, patch)
2013-10-29 13:17 EDT, ANIRBAN CHAKRABORTY CLA
anchakrk: review?
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Ellis CLA 2013-10-07 10:53:08 EDT

    
Comment 1 Michael Ellis CLA 2013-10-07 11:12:09 EDT
Created attachment 236178 [details]
Zipped up workspace containing the two projects

This is a new workspace with just the minimal two projects that can be used to demonstrate the bug.
Comment 2 Michael Ellis CLA 2013-10-07 11:13:13 EDT
Created attachment 236179 [details]
Screenshot of error message
Comment 3 Michael Ellis CLA 2013-10-07 11:13:46 EDT
Created attachment 236180 [details]
Screenshot of Eclipse version info
Comment 4 Michael Ellis CLA 2013-10-07 11:15:18 EDT
I have found what I think is a bug with Eclipse (version 4.3.1)

I repeatedly find content assists and code completion will break under the following conditions.

1. Create a new project (mine was called StringComparator) with a singe Java file called TestProjectInclude.java containing the following java code)
--------------------------------------------
import java.util.Comparator;

public class TestProjectInclude  {
}


class OrderedStringComparator1 implements Comparator<String> {
	@Override
	public int compare(String s1, String s2) {
		return 0;
	}
}

class OrderedStringComparator2 implements Comparator<String> {
	@Override
	public int compare(String s1, String s2) {
		return 0;
	}
}

class OrderedStringComparator3 implements Comparator<String> {
	@Override
	public int compare(String s1, String s2) {
		return 0;
	}
}

class OrderedStringComparator4 implements Comparator<String> {
	@Override
	public int compare(String s1, String s2) {
		return 0;
	}
}

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

Create a new project (mine was called TestProject with a single java file 

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

public class TestClass {

	int f;
	
	public static void main(String[] args) {
		TestClass tc = new TestClass();
		tc.f = 3;
	}

}

--------------------------------------------
This second project at first will work fine and content assist is working. 

Now add the StringComparator project to the TestProject's build path.

Close and re-open the TestProject.

Content assist is now broken for TestProject

An attempt to use it results in the following error:


java.lang.ClassCastException: org.eclipse.jdt.internal.core.SourceType cannot be cast to java.lang.String
	at org.eclipse.jdt.internal.core.JavaModelManager.secondaryTypesSearching(JavaModelManager.java:4579)
	at org.eclipse.jdt.internal.core.JavaModelManager.secondaryTypes(JavaModelManager.java:4438)
	at org.eclipse.jdt.internal.core.NameLookup.findSecondaryType(NameLookup.java:591)
	at org.eclipse.jdt.internal.core.NameLookup.findType(NameLookup.java:697)
	at org.eclipse.jdt.internal.core.NameLookup.findType(NameLookup.java:621)
	at org.eclipse.jdt.internal.core.SearchableEnvironment.find(SearchableEnvironment.java:103)
	at org.eclipse.jdt.internal.core.SearchableEnvironment.findType(SearchableEnvironment.java:294)
	at org.eclipse.jdt.internal.core.CancelableNameEnvironment.findType(CancelableNameEnvironment.java:45)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:145)
	at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.getType(PackageBinding.java:141)
	at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.isViewedAsDeprecated(PackageBinding.java:225)
	at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.isViewedAsDeprecated(ReferenceBinding.java:1416)
	at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.cachePartsFrom(BinaryTypeBinding.java:378)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:700)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:676)
	at org.eclipse.jdt.internal.compiler.Compiler.accept(Compiler.java:298)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:151)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getType(LookupEnvironment.java:1179)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getResolvedType(LookupEnvironment.java:1115)
	at org.eclipse.jdt.internal.compiler.lookup.Scope.getJavaLangObject(Scope.java:2352)
	at org.eclipse.jdt.internal.compiler.lookup.ClassScope.connectSuperclass(ClassScope.java:907)
	at org.eclipse.jdt.internal.compiler.lookup.ClassScope.connectTypeHierarchy(ClassScope.java:1065)
	at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.connectTypeHierarchy(CompilationUnitScope.java:304)
	at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.completeTypeBindings(LookupEnvironment.java:214)
	at org.eclipse.jdt.internal.compiler.Compiler.internalBeginToCompile(Compiler.java:746)
	at org.eclipse.jdt.internal.compiler.Compiler.beginToCompile(Compiler.java:383)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1133)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:685)
	at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1181)
	at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:807)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider$1.run(ASTProvider.java:544)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.createAST(ASTProvider.java:537)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.getAST(ASTProvider.java:480)
	at org.eclipse.jdt.ui.SharedASTProvider.getAST(SharedASTProvider.java:128)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:170)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$3.run(SelectionListenerWithASTManager.java:155)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
------------------

eclipse.buildId=4.3.0.M20130911-1000
java.version=1.8.0-ea
java.vendor=Oracle Corporation
BootLoader constants: OS=macosx, ARCH=x86_64, WS=cocoa, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.standard.product -keyring /Users/michaelellis/.eclipse_keyring -showlocation
Command-line arguments:  -os macosx -ws cocoa -arch x86_64 -product org.eclipse.epp.package.standard.product -keyring /Users/michaelellis/.eclipse_keyring -showlocation
Comment 5 Srikanth Sankaran CLA 2013-10-07 22:45:04 EDT
We have multiple reports of this. The CCE is indicative of basic mess up
somewhere. Anirban, please fix this for M3.
Comment 6 Manoj N Palat CLA 2013-10-08 23:26:05 EDT
(In reply to Srikanth Sankaran from comment #5)
> We have multiple reports of this. The CCE is indicative of basic mess up
> somewhere. Anirban, please fix this for M3.

This issue is seen at BETA_JAVA8 as well. There is a concurrency issue here as follows:
 The iterator is being used to iterate loop, while the entry is being replaced - though its just a replacement, in some cases, internally this causes a resize, which results in pointer corruption from iterator's point of view, which in turn retrieves an already replaced entry (the value of which would now be of type SourceType) resulting in the error reported.
Comment 7 Srikanth Sankaran CLA 2013-10-08 23:29:01 EDT
(In reply to Manoj Palat from comment #6)
> (In reply to Srikanth Sankaran from comment #5)
> > We have multiple reports of this. The CCE is indicative of basic mess up
> > somewhere. Anirban, please fix this for M3.
> 
> This issue is seen at BETA_JAVA8 as well. There is a concurrency issue here
> as follows:
>  The iterator is being used to iterate loop, while the entry is being
> replaced - though its just a replacement, in some cases, internally this
> causes a resize, which results in pointer corruption from iterator's point
> of view, which in turn retrieves an already replaced entry (the value of
> which would now be of type SourceType) resulting in the error reported.

Thanks for the analysis Manoj. Anirban, please work with Jay on this since
he knows the Java model well to figure out what the suitable synchronization
primitives should be.
Comment 8 Jay Arthanareeswaran CLA 2013-10-28 09:36:44 EDT
I recently ran into this, which I recorded at:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=420041#c4

Unfortunately I can no longer reproduce this. But at that point, I strongly believed (still do), that this was an issue with JRE 8 (I used b108).

Michael, could you do a small experiment with a JRE7 or lower and see if you still see the bug?
Comment 9 Michael Ellis CLA 2013-10-28 10:17:11 EDT
The problem still exists when using Java 7

Java version = 1.7.0_25

I'm running on Apple MacBook Retina
Comment 10 Jay Arthanareeswaran CLA 2013-10-28 11:31:33 EDT
(In reply to Michael Ellis from comment #4)
> I have found what I think is a bug with Eclipse (version 4.3.1)


Anirban, I can reproduce the bug with the given testcase. I believe you need to have the annotation based null analysis enabled at the global preferences. Could you give it a try?
Comment 11 Jay Arthanareeswaran CLA 2013-10-28 11:36:16 EDT
(In reply to Jayaprakash Arthanareeswaran from comment #10)
> (In reply to Michael Ellis from comment #4)
> > I have found what I think is a bug with Eclipse (version 4.3.1)
> 
> 
> Anirban, I can reproduce the bug with the given testcase. I believe you need
> to have the annotation based null analysis enabled at the global
> preferences. Could you give it a try?

As I observed in bug 420041, the key is having secondary type, which means the HashTable contains same path with different type names as keys.
Comment 12 ANIRBAN CHAKRABORTY CLA 2013-10-29 04:32:56 EDT
Created attachment 236995 [details]
Code patch

Hello,
Thanks to Manoj and Jay for help while solving this bug.
The problem is due to the fact that an underlying collection is constantly getting changed while the iterator is walking. The documentation clearly mentions that HashMap-iterator.remove() does not allow changing the container in any other way.
Like many other memory errors, this shows up inconsistently.
I'm posting the solution patch, which has passed regression. I'm working on the junit testcase, with help of Jay, where it seems difficult to hit the scenario.
Thanks
Anirban
Comment 13 ANIRBAN CHAKRABORTY CLA 2013-10-29 13:17:02 EDT
Created attachment 237032 [details]
Code patch after review comments

Uploading code patch after review comments. Yet to come up with a junit testcase.
Comment 14 Jay Arthanareeswaran CLA 2013-10-29 14:36:41 EDT
Thanks Anirban, I have released the fix with a couple of minor changes:

1. tempTypes.clear() is redundant as tempTypes itself go out of scope.
2. Removed the redundant comment.

http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=76962ee0c50a3b7551973355aa71fa2a5e76ae6b

Manoj, I believe you have seen the patch already. In any case, I encourage you to look at the final commit and update the review flag. Thanks!
Comment 15 Manoj N Palat CLA 2013-10-30 08:58:52 EDT
(In reply to Jayaprakash Arthanareeswaran from comment #14)
> Thanks Anirban, I have released the fix with a couple of minor changes:

> Manoj, I believe you have seen the patch already. In any case, I encourage
> you to look at the final commit and update the review flag. Thanks!
Yes Jay. +1 from my review.
Comment 16 Manoj N Palat CLA 2013-10-30 10:01:04 EDT
Verified for version 4.4 M3 Build id: I20131029-2000
Comment 17 Michael Ellis CLA 2013-11-13 06:41:41 EST
Can someone tell me how I can get this fix. Is there a new version of Eclipse or some update mechanism. This bug is driving me spare!

Thanks for any help.
Comment 18 Jay Arthanareeswaran CLA 2013-11-13 06:57:35 EST
(In reply to Michael Ellis from comment #17)
> Can someone tell me how I can get this fix. Is there a new version of
> Eclipse or some update mechanism. This bug is driving me spare!
> 
> Thanks for any help.

You can update to the latest I build via this update site:

http://download.eclipse.org/eclipse/updates/4.4-I-builds
Comment 19 Michael Ellis CLA 2013-11-13 07:17:49 EST
Ah. Sorry to waste anyone's time. Worked it out. Downloaded 4.4M3 from http://download.eclipse.org/eclipse/downloads/drops4/S-4.4M3-201310302000/


(In reply to Jayaprakash Arthanareeswaran from comment #18)
> (In reply to Michael Ellis from comment #17)
> > Can someone tell me how I can get this fix. Is there a new version of
> > Eclipse or some update mechanism. This bug is driving me spare!
> > 
> > Thanks for any help.
> 
> You can update to the latest I build via this update site:
> 
> http://download.eclipse.org/eclipse/updates/4.4-I-builds