Bug 537550 - No error logged if Thread.setName() location is not found.

If the location of method java.lang.Thread.setName() is not found in the
debugee JVM, a warning is logged instead of an error. If the debuggee
JVM is not available at the point of logging, nothing is logged.

As before, the ThreadNameChangeListener is not registered in this case.

Change-Id: I7cb22e202f3d246cea1f72be98de75dffeaa180a
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
diff --git a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIDebugTarget.java b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIDebugTarget.java
index 2f28909..51238f6 100644
--- a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIDebugTarget.java
+++ b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIDebugTarget.java
@@ -33,7 +33,6 @@
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspaceRoot;
 import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Assert;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -2405,33 +2404,42 @@
 			EventRequestManager manager = getEventRequestManager();
 			if (manager != null) {
 				try {
-					Location location = setNameMethodLocation();
-					Assert.isNotNull(location, "Unable to locate Thread.setName method in debuggee JVM"); //$NON-NLS-1$
-					request = manager.createBreakpointRequest(location);
-					request.setSuspendPolicy(EventRequest.SUSPEND_NONE);
-					request.enable();
-					addJDIEventListener(this, request);
+					Location location = locationOfSetNameMethod();
+					if (location != null) {
+						request = manager.createBreakpointRequest(location);
+						request.setSuspendPolicy(EventRequest.SUSPEND_NONE);
+						request.enable();
+						addJDIEventListener(this, request);
+					}
 				} catch (RuntimeException e) {
 					String errorMessage = "Failed to add thread name change listener to debug target " + JDIDebugTarget.this; //$NON-NLS-1$
 					IStatus errorStatus = new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), errorMessage, e);
-					JDIDebugPlugin.log(errorStatus);
+					logRequestStatus(errorStatus);
 				}
 			}
 		}
 
-		private Location setNameMethodLocation() {
+		private Location locationOfSetNameMethod() {
 			List<ReferenceType> types = jdiClassesByName(TYPE_NAME);
+			boolean foundThreadType = false;
 			for (ReferenceType type : types) {
 				if (type instanceof ClassType) {
+					foundThreadType = true;
 					Method method = ((ClassType) type).concreteMethodByName(METHOD_NAME, METHOD_SIGNATURE);
 					if (method != null && !method.isNative()) {
 						Location location = method.location();
 						if (location != null && location.codeIndex() != -1) {
 							return location;
 						}
+						logRequestWarning("Unable to find location of java.lang.Thread.setName() in debuggee JVM, for type " + type); //$NON-NLS-1$
+					} else {
+						logRequestWarning("Unable to find java.lang.Thread.setName() method in debuggee JVM, for type " + type); //$NON-NLS-1$
 					}
 				}
 			}
+			if (!foundThreadType) {
+				logRequestWarning("Unable to find type java.lang.Thread.setName() in debuggee JVM"); //$NON-NLS-1$
+			}
 			return null;
 		}
 
@@ -2460,6 +2468,17 @@
 		public void eventSetComplete(Event event, JDIDebugTarget target, boolean suspendVote, EventSet eventSet) {
 			// nothing to do here, we do work in handleEvent
 		}
+
+		private void logRequestWarning(String warningMessage) {
+			IStatus warningStatus = new Status(IStatus.WARNING, JDIDebugPlugin.getUniqueIdentifier(), warningMessage);
+			logRequestStatus(warningStatus);
+		}
+
+		private void logRequestStatus(IStatus status) {
+			if (isAvailable()) {
+				JDIDebugPlugin.log(status);
+			}
+		}
 	}
 
 	class CleanUpJob extends Job {