eclipse / xtext

Eclipse Xtext™ is a language development framework
http://www.eclipse.org/Xtext
Eclipse Public License 2.0
769 stars 321 forks source link

Can't access Activator/BundleContext during shared service initialization #2453

Open meysholdt opened 7 years ago

meysholdt commented 7 years ago

Steps to reproduce:

  1. create a service (Java class) that accesses something like Activator.getDefault().getBundle().getBundleContext().getServiceReference(SomeService.class) in the constructor.
  2. bind the service in a custom shared state module that has been registered via Xtext's extension point.
  3. Starting org.eclipse.xtext.ui.shared will now fail.

Analysis: the code from (1) throws an NPE because it's called during org.eclipse.xtext.ui.shared.internal.Activator.start(BundleContext)

Since the bundle can't be activated users see tons of subsequent ClassNotFoundExceptions because classes from that bundle can't be accessed.

While I don't think the code from (1) needs to be supported in any case... I can say that this used to work because the Shared Injector used to be initialized lazily and thus after bundle startup finished.

cdietrich commented 7 years ago

yes had something similar at the customer. problem did not occur with eclipse 3x but after migration to 4x. i have no idea how to solve this. do you @meysholdt have any idea. i dont see how if and what changed about lazy initialization.

=> where is the regression

even in xtext 2.8.4 org.eclipse.xtext.ui.shared.SharedStateModule is loaded as soon as a xtext plugin is created

cdietrich commented 7 years ago

@meysholdt can you give details on what you exactly do in (2)

meysholdt commented 7 years ago

(2):

 <extension
        point="org.eclipse.xtext.ui.shared.overridingGuiceModule">
    <module
           class="MyModule">
     </module>
  </extension>
public class MyModule implements Module {

    @Override
    public void configure(final Binder binder) {
    binder.bind(IMyService.class).to(MyServiceImplWithEvilConstructor.class).in(Scopes.SINGLETON);
    }

}
cdietrich commented 7 years ago

still cannot reproduce that. the constructor gets called lazily. where do you make use of IMyService

cdietrich commented 7 years ago

agggg overridingGuiceModule != shared service (which is )

  <extension
        point="org.eclipse.xtext.ui.shared.sharedStateContributingModule">
     <module
           class="org.xtext.example.mydsl.ui.Module1">
     </module>
  </extension>

but i still cannot reproduce.

meysholdt commented 7 years ago

In Xtext 2.12 injector creation wasn't lazy either, thus unsure if regression.

cdietrich commented 7 years ago

would you mind sharing a small example.

meysholdt commented 7 years ago

I can share a stack trace:

java.lang.NullPointerException: A null service reference is not allowed.
    at org.eclipse.osgi.internal.framework.BundleContextImpl.getService(BundleContextImpl.java:617)
    at com.company.MyService.<init>(MyService.java:29)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:85)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.Scopes$1$1.get(Scopes.java:65)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.Scopes$1$1.get(Scopes.java:65)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
    at org.eclipse.xtext.ui.shared.contribution.SharedStateContribution.getContribution(SharedStateContribution.java:57)
    at org.eclipse.xtext.ui.shared.internal.SharedStateContributionRegistryImpl.getContributedInstances(SharedStateContributionRegistryImpl.java:78)
    at org.eclipse.xtext.ui.shared.internal.EagerContributionInitializer.setContributions(EagerContributionInitializer.java:25)
    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:498)
    at com.google.inject.internal.SingleMethodInjector$1.invoke(SingleMethodInjector.java:71)
    at com.google.inject.internal.SingleMethodInjector.inject(SingleMethodInjector.java:90)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:75)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:73)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
    at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:73)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:60)
    at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:944)
    at org.eclipse.xtext.ui.shared.internal.Activator.initializeInjector(Activator.java:97)
    at org.eclipse.xtext.ui.shared.internal.Activator.start(Activator.java:208)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:941)
    at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:318)
    at org.eclipse.osgi.container.Module.doStart(Module.java:571)
    at org.eclipse.osgi.container.Module.start(Module.java:439)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:454)
    at org.eclipse.osgi.internal.hooks.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:107)
    at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClass(ClasspathManager.java:531)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.findLocalClass(ModuleClassLoader.java:324)
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:327)
    at org.eclipse.osgi.internal.loader.sources.SingleSourcePackage.loadClass(SingleSourcePackage.java:36)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:398)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.company.ui.internal.MyDSLActivator.getSharedStateModule(MyDSLActivator.java:92)
    at com.company.ui.internal.MyDSLActivator.createInjector(MyDSLActivator.java:64)
    at com.company.ui.internal.MyDSLActivator.getInjector(MyDSLActivator.java:55)
    at com.company.MyDSL.MyDSLUiInjectorProvider.getInjector(MyDSLUiInjectorProvider.java:14)
    at org.eclipse.xtext.testing.XtextRunner.createTest(XtextRunner.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.eclipse.xtext.testing.XtextRunner.superMethodBlock(XtextRunner.java:61)
    at org.eclipse.xtext.testing.XtextRunner.methodBlock(XtextRunner.java:56)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    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:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:156)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:82)
    at org.eclipse.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:95)
    at org.eclipse.tycho.surefire.osgibooter.HeadlessTestApplication.run(HeadlessTestApplication.java:21)
    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:498)
    at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
    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:498)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
cdietrich commented 7 years ago

are you using SharedStateContribution or overridingGuiceModule ???

meysholdt commented 7 years ago

overridingGuiceModule

cdietrich commented 7 years ago

but where do you use IMyService? in the builder?

cdietrich commented 7 years ago

i give up with reproducing this :(