Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 468093

Summary: static imports are removed on "organize imports" save action if annotations can't be found on the classpath
Product: [Eclipse Project] JDT Reporter: David Green <greensopinion>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: andrew.eisenberg, cptwunderlich, jarthana, stephan.herrmann
Version: 4.5   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug

Description David Green CLA 2015-05-22 18:16:13 EDT
When writing a JUnit test, the static import for JUnit's Assert.assertEquals is removed every time I save the file.  The problem appeared to occur because a transitive dependency to an annotation on some other class was not resolved.  I have on-save "organize imports" enabled.

Note that the unresolved annotation is not referenced directly in the the editor's compilation unit.

The problem goes away when the transitive dependency is added under "Require-Bundle" in the plug-in project manifest of the compilation unit being edited.

I'm running the Ecilpse SDK:

Version: Mars (4.5)
Build id: I20150430-1445

The following was in the error log:

org.eclipse.jdt.internal.compiler.problem.AbortCompilation: Pb(324) The type com.tasktop.api.annotations.SomeAnnotationClass cannot be resolved. It is indirectly referenced from required .class files
	at org.eclipse.jdt.internal.compiler.problem.ProblemHandler.handle(ProblemHandler.java:157)
	at org.eclipse.jdt.internal.compiler.problem.ProblemHandler.handle(ProblemHandler.java:222)
	at org.eclipse.jdt.internal.compiler.problem.ProblemReporter.handle(ProblemReporter.java:2382)
	at org.eclipse.jdt.internal.compiler.problem.ProblemReporter.isClassPathCorrect(ProblemReporter.java:4704)
	at org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding.resolve(UnresolvedReferenceBinding.java:104)
	at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:187)
	at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.getTypeOrPackage(PackageBinding.java:186)
	at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.findImport(CompilationUnitScope.java:463)
	at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.findSingleImport(CompilationUnitScope.java:517)
	at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.faultInImports(CompilationUnitScope.java:394)
	at org.eclipse.jdt.internal.compiler.lookup.Scope.getBinding(Scope.java:2095)
	at org.eclipse.jdt.internal.compiler.lookup.BlockScope.getBinding(BlockScope.java:485)
	at org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference.resolveType(QualifiedNameReference.java:999)
	at org.eclipse.jdt.internal.compiler.ast.MemberValuePair.resolveTypeExpecting(MemberValuePair.java:89)
	at org.eclipse.jdt.internal.compiler.ast.Annotation.resolveType(Annotation.java:830)
	at org.eclipse.jdt.internal.compiler.ast.ASTNode.resolveAnnotations(ASTNode.java:829)
	at org.eclipse.jdt.internal.compiler.ast.ASTNode.resolveAnnotations(ASTNode.java:697)
	at org.eclipse.jdt.internal.compiler.lookup.MethodBinding.getAnnotationTagBits(MethodBinding.java:636)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypesFor(SourceTypeBinding.java:1793)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.methods(SourceTypeBinding.java:1480)
	at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.availableMethods(ReferenceBinding.java:238)
	at org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods(TypeBinding.java:302)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.hasFieldWithName(UnresolvedElementsSubProcessor.java:561)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.addSimilarVariableProposals(UnresolvedElementsSubProcessor.java:499)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.getVariableProposals(UnresolvedElementsSubProcessor.java:290)
	at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.process(QuickFixProcessor.java:361)
	at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.getCorrections(QuickFixProcessor.java:316)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionCollector.safeRun(JavaCorrectionProcessor.java:378)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:339)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.process(JavaCorrectionProcessor.java:335)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectCorrections(JavaCorrectionProcessor.java:468)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectProposals(JavaCorrectionProcessor.java:280)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.computeQuickAssistProposals(JavaCorrectionProcessor.java:242)
	at org.eclipse.jface.text.quickassist.QuickAssistAssistant$ContentAssistProcessor.computeCompletionProposals(QuickAssistAssistant.java:75)
	at org.eclipse.jface.text.contentassist.ContentAssistant$5.run(ContentAssistant.java:1904)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jface.text.contentassist.ContentAssistant.computeCompletionProposals(ContentAssistant.java:1902)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.computeProposals(CompletionProposalPopup.java:573)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.access$16(CompletionProposalPopup.java:570)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup$2.run(CompletionProposalPopup.java:505)
	at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.showProposals(CompletionProposalPopup.java:499)
	at org.eclipse.jface.text.contentassist.ContentAssistant.showPossibleCompletions(ContentAssistant.java:1720)
	at org.eclipse.jface.text.quickassist.QuickAssistAssistant.showPossibleQuickAssists(QuickAssistAssistant.java:128)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionAssistant.showPossibleQuickAssists(JavaCorrectionAssistant.java:200)
	at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor$AdaptedSourceViewer.doOperation(CompilationUnitEditor.java:192)
	at org.eclipse.ui.texteditor.TextOperationAction$1.run(TextOperationAction.java:128)
	at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
	at org.eclipse.ui.texteditor.TextOperationAction.run(TextOperationAction.java:126)
	at org.eclipse.jface.action.Action.runWithEvent(Action.java:473)
	at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:122)
	at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
	at sun.reflect.GeneratedMethodAccessor66.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:252)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:234)
	at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
	at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
	at org.eclipse.core.commands.Command.executeWithChecks(Command.java:493)
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:486)
	at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:286)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:507)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:558)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:378)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$0(KeyBindingDispatcher.java:324)
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:86)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1105)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4225)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1481)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1504)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1489)
	at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1518)
	at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1514)
	at org.eclipse.swt.widgets.Canvas.sendKeyEvent(Canvas.java:496)
	at org.eclipse.swt.widgets.Control.doCommandBySelector(Control.java:1060)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5784)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
	at org.eclipse.swt.internal.cocoa.NSResponder.interpretKeyEvents(NSResponder.java:68)
	at org.eclipse.swt.widgets.Composite.keyDown(Composite.java:594)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5694)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
	at org.eclipse.swt.widgets.Widget.callSuper(Widget.java:221)
	at org.eclipse.swt.widgets.Widget.windowSendEvent(Widget.java:2120)
	at org.eclipse.swt.widgets.Shell.windowSendEvent(Shell.java:2337)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5756)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
	at org.eclipse.swt.widgets.Display.applicationSendEvent(Display.java:5193)
	at org.eclipse.swt.widgets.Display.applicationProc(Display.java:5342)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
	at org.eclipse.swt.internal.cocoa.NSApplication.sendEvent(NSApplication.java:128)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3695)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
	at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
Comment 1 Andrew Eisenberg CLA 2015-05-22 18:37:27 EDT
Unfortunately, we can't share the code, but I can give a more detailed description of what is happenig.

The project where this occurs in is a PDE project. There is a class in that project that is trying to load a type from another dependent project. This type is annotated by an annotation from a third project. This annotation has runtime retention.  The set up looks roughly like this:

Test bundle (requires main bundle)

class AClassTest {
  private AClass aClass;
}

Main bundle (requires annotation bundle)
 
@SomeAnnotation
class AClass {}


Annotation bundle

@Retention(RetentionPolicy.RUNTIME)
@interface SomeAnnotation{}


Now with this setup it seems like the compiler should be happy and indeed it is, but it seems like the resolver is not.  And the exceptions being thrown are happening during resolve time.

Hope this helps a bit with reproducibility.
Comment 2 Andrew Eisenberg CLA 2015-05-22 18:38:05 EDT
Moving to JDT core since this is not a UI bug.
Comment 3 Stephan Herrmann CLA 2015-05-22 20:00:13 EDT
Hi Andrew, thanks for explanations.

(In reply to Andrew Eisenberg from comment #1)
> Now with this setup it seems like the compiler should be happy and indeed it
> is, but it seems like the resolver is not.  And the exceptions being thrown
> are happening during resolve time.

By resolver you mean call paths following org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods() like above above? (which is a "side-entry" into the compiler).

BTW: the stack trace above is triggered from quick assist, do you see similar exceptions also during organize imports?
Comment 4 Andrew Eisenberg CLA 2015-05-25 22:38:04 EDT
> By resolver you mean call paths following
> org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods() like above above?
> (which is a "side-entry" into the compiler).

Yes.


> BTW: the stack trace above is triggered from quick assist, do you see
> similar exceptions also during organize imports?

I can't seem to elicit exceptions in any other way, but I have the following behavior:

1. No code hovers
2. Organize imports removes static imports, but leaves all others
3. Semantic highlighting (eg- italicizing static field references) is not working
4. Mark occurrences not working
5. Extract method fails with the following exception:

java.lang.NullPointerException
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypesFor(SourceTypeBinding.java:1770)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.methods(SourceTypeBinding.java:1464)
	at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.availableMethods(ReferenceBinding.java:229)
	at org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods(TypeBinding.java:289)
	at org.eclipse.jdt.internal.corext.dom.Bindings.findMethodInType(Bindings.java:360)
	at org.eclipse.jdt.internal.corext.refactoring.Checks.checkMethodInType(Checks.java:313)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodAnalyzer.checkInput(ExtractMethodAnalyzer.java:351)
	at org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring.checkFinalConditions(ExtractMethodRefactoring.java:476)
	at org.eclipse.ltk.core.refactoring.CheckConditionsOperation.run(CheckConditionsOperation.java:85)
	at org.eclipse.ltk.core.refactoring.CreateChangeOperation.run(CreateChangeOperation.java:121)
	at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2313)
	at org.eclipse.ltk.internal.ui.refactoring.WorkbenchRunnableAdapter.run(WorkbenchRunnableAdapter.java:87)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:122)



Oddly however, rename refactoring on a method of the class works correctly. It finds references both inside the file and outside.
Comment 5 Benjmain Maurer CLA 2017-09-04 12:20:22 EDT
This issue affects me as well on Oxygen 4.7 JEE Bundle
> Eclipse Java EE IDE for Web Developers.
> Version: Oxygen Release (4.7.0)
> Build id: 20170620-1800
> OS: Windows 10, v.10.0, x86_64 / win32

The major difference is, that the annotations should be on the classpath. It is a Maven project, the two classes are in the very same package.

Saving once sometimes does nothing, but two consecutive changes + save almost certainly trigger the behavior.
Comment 6 Benjmain Maurer CLA 2017-09-04 12:21:33 EDT
(In reply to Benjmain Maurer from comment #5)
> This issue affects me as well on Oxygen 4.7 JEE Bundle
> > Eclipse Java EE IDE for Web Developers.
> > Version: Oxygen Release (4.7.0)
> > Build id: 20170620-1800
> > OS: Windows 10, v.10.0, x86_64 / win32
> 
> The major difference is, that the annotations should be on the classpath. It
> is a Maven project, the two classes are in the very same package.
> 
> Saving once sometimes does nothing, but two consecutive changes + save
> almost certainly trigger the behavior.

I forgot to mention, that my Error Log is empty.
Comment 7 Eclipse Genie CLA 2019-12-14 11:21:20 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.