Bug 575815 - Improve message about service object not instance of to
include the object classname

Change-Id: I9295dd17ab1a47f1785f3abbcb3f49c8db6424c0
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.framework/+/185120
Tested-by: Equinox Bot <equinox-bot@eclipse.org>
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java
index 49afd98..2e4a99f 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java
@@ -17,12 +17,22 @@
 import java.util.Hashtable;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import org.eclipse.osgi.tests.OSGiTestsActivator;
 import org.eclipse.osgi.tests.bundles.AbstractBundleTests;
 import org.eclipse.osgi.tests.util.MapDictionary;
-import org.osgi.framework.*;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 
 public class ServiceRegistryTests extends AbstractBundleTests {
 	public static Test suite() {
@@ -532,6 +542,42 @@
 		}
 	}
 
+	public void testWrongServiceFactoryObject() throws InterruptedException {
+		AtomicReference<String> errorMsg = new AtomicReference<>();
+		CountDownLatch gotEvent = new CountDownLatch(1);
+		FrameworkListener fwkListener = (e) -> {
+			if (e.getType() == FrameworkEvent.ERROR && e.getThrowable() != null) {
+				errorMsg.set(e.getThrowable().getMessage());
+				gotEvent.countDown();
+			}
+		};
+		ServiceRegistration<Runnable> reg = OSGiTestsActivator.getContext().registerService(Runnable.class,
+				new ServiceFactory() {
+
+					@Override
+					public Object getService(Bundle bundle, ServiceRegistration registration) {
+						return "Wrong object!!";
+					}
+
+					@Override
+					public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
+					}
+
+				}, null);
+		OSGiTestsActivator.getContext().addFrameworkListener(fwkListener);
+		try {
+			ServiceReference<Runnable> ref = reg.getReference();
+			Runnable service = OSGiTestsActivator.getContext().getService(ref);
+			assertNull(service);
+			gotEvent.await(30, TimeUnit.SECONDS);
+			assertNotNull(errorMsg.get());
+			assertTrue("Wrong error message: " + errorMsg.get(), errorMsg.get().contains(String.class.getName()));
+		} finally {
+			OSGiTestsActivator.getContext().removeFrameworkListener(fwkListener);
+			reg.unregister();
+		}
+	}
+
 	private void clearResults(boolean[] results) {
 		for (int i = 0; i < results.length; i++)
 			results[i] = false;
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java
index 374ee21..eb441de 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java
@@ -20,7 +20,11 @@
 import org.eclipse.osgi.internal.framework.BundleContextImpl;
 import org.eclipse.osgi.internal.messages.Msg;
 import org.eclipse.osgi.util.NLS;
-import org.osgi.framework.*;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.ServiceException;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
 
 /**
  * This class represents the use of a service by a bundle. One is created for each
@@ -239,7 +243,11 @@
 			if (debug.DEBUG_SERVICES) {
 				Debug.println("Service object is not an instanceof " + invalidService); //$NON-NLS-1$
 			}
-			ServiceException se = new ServiceException(NLS.bind(Msg.SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION, factory.getClass().getName(), invalidService), ServiceException.FACTORY_ERROR);
+			ServiceException se = new ServiceException(
+					NLS.bind(Msg.SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION,
+							new Object[] { factory.getClass().getName(), service.getClass().getName(),
+									invalidService }),
+					ServiceException.FACTORY_ERROR);
 			context.getContainer().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, registration.getBundle(), se);
 			return null;
 		}
diff --git a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties
index d3b57bd..9b5cdfb 100644
--- a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties
+++ b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties
@@ -23,7 +23,7 @@
 SERVICE_FACTORY_EXCEPTION=Exception in {0}.{1}()
 SERVICE_FACTORY_RECURSION=Recursive ServiceFactory call in {0}.{1}()
 SERVICE_NOT_INSTANCEOF_CLASS_EXCEPTION=The service object is not an instance of the service class {0}
-SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION={0}.getService() returned a service object that is not an instance of the service class {1}
+SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION={0}.getService() returned a service object of type {1} that is not an instance of the service class {2}
 SERVICE_OBJECT_NULL_EXCEPTION={0}.getService() returned a null service object
 SERVICE_ARGUMENT_NULL_EXCEPTION=The service parameter is null
 SERVICE_OBJECTS_UNGET_ARGUMENT_EXCEPTION=The service parameter was not provided by this object