Bug 526388 - don't enable advanced source lookup when run-as

Change-Id: Ib2f7a538701407a5acdb28ed84d7f5ddd379444b
Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/sourcelookup/advanced/AdvancedRemoteJavaLaunchDelegate.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/sourcelookup/advanced/AdvancedRemoteJavaLaunchDelegate.java
index eb02b32..664b8b0 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/sourcelookup/advanced/AdvancedRemoteJavaLaunchDelegate.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/sourcelookup/advanced/AdvancedRemoteJavaLaunchDelegate.java
@@ -10,22 +10,12 @@
  *******************************************************************************/
 package org.eclipse.jdt.internal.launching.sourcelookup.advanced;
 
-import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.createAdvancedLaunch;
-import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.isAdvancedSourcelookupEnabled;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.debug.core.ILaunch;
-import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.jdt.internal.launching.JavaRemoteApplicationLaunchConfigurationDelegate;
 
 public class AdvancedRemoteJavaLaunchDelegate extends JavaRemoteApplicationLaunchConfigurationDelegate {
 
-	@Override
-	public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
-		if (!isAdvancedSourcelookupEnabled()) {
-			return super.getLaunch(configuration, mode);
-		}
-		return createAdvancedLaunch(configuration, mode);
+	public AdvancedRemoteJavaLaunchDelegate() {
+		allowAdvancedSourcelookup();
 	}
 
 }
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractJavaLaunchConfigurationDelegate.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractJavaLaunchConfigurationDelegate.java
index ae0c1e6..0084dc9 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractJavaLaunchConfigurationDelegate.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractJavaLaunchConfigurationDelegate.java
@@ -9,6 +9,11 @@
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
 package org.eclipse.jdt.launching;
+
+import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.createAdvancedLaunch;
+import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.getJavaagentString;
+import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.isAdvancedSourcelookupEnabled;
+
 import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -62,10 +67,13 @@
  * @since 2.0
  */
 public abstract class AbstractJavaLaunchConfigurationDelegate extends LaunchConfigurationDelegate implements IDebugEventSetListener {
+	private boolean allowAdvancedSourcelookup;
+
 	/**
 	 * A list of prerequisite projects ordered by their build order.
 	 */
 	private IProject[] fOrderedProjects;
+
 	/**
 	 * Convenience method to get the launch manager.
 	 *
@@ -596,6 +604,33 @@
 		}
 		return args;
 	}
+
+	/**
+	 * Returns the VM arguments specified by the given launch configuration, as a string. The returned string is empty if no VM arguments are
+	 * specified.
+	 *
+	 * @param configuration
+	 *            launch configuration
+	 * @param mode
+	 *            the mode in which to launch, one of the mode constants defined by <code>ILaunchManager</code> - <code>RUN_MODE</code> or
+	 *            <code>DEBUG_MODE</code>.
+	 * @return the VM arguments specified by the given launch configuration, possibly an empty string
+	 * @exception CoreException
+	 *                if unable to retrieve the attribute
+	 * @since 3.10
+	 */
+	public String getVMArguments(ILaunchConfiguration configuration, String mode) throws CoreException {
+		if (!isAdvancedSourcelup(mode)) {
+			return ""; //$NON-NLS-1$
+		}
+
+		return getJavaagentString();
+	}
+
+	private boolean isAdvancedSourcelup(String mode) {
+		return allowAdvancedSourcelookup && ILaunchManager.DEBUG_MODE.equals(mode) && isAdvancedSourcelookupEnabled();
+	}
+
 	/**
 	 * Returns the Map of VM-specific attributes specified by the given launch
 	 * configuration, or <code>null</code> if none.
@@ -1051,4 +1086,23 @@
 		}
 		return null;
 	}
+
+	@Override
+	public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
+		if (!isAdvancedSourcelup(mode)) {
+			return null;
+		}
+		return createAdvancedLaunch(configuration, mode);
+	}
+
+	/**
+	 * Enabled advanced sourcelookup for this launch delegate. Advanced source lookup is disabled by default. This call has not effect if advanced
+	 * source lookup is disabled at workspace level, i.e. advanced source lookup will remain disabled even if this method is called for the launch
+	 * delegate instance.
+	 *
+	 * @since 3.10
+	 */
+	protected final void allowAdvancedSourcelookup() {
+		this.allowAdvancedSourcelookup = true;
+	}
 }
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaLaunchDelegate.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaLaunchDelegate.java
index c99ee58..613602f 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaLaunchDelegate.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaLaunchDelegate.java
@@ -67,7 +67,7 @@
 
 			// Program & VM arguments
 			String pgmArgs = getProgramArguments(configuration);
-			String vmArgs = getVMArguments(configuration);
+			String vmArgs = concat(getVMArguments(configuration), getVMArguments(configuration, mode));
 			ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs);
 
 			// VM-specific attributes
@@ -134,4 +134,15 @@
 		}
 	}
 
+	private static String concat(String args1, String args2) {
+		StringBuilder args = new StringBuilder();
+		if (args1 != null && !args1.isEmpty()) {
+			args.append(args1);
+		}
+		if (args2 != null && !args2.isEmpty()) {
+			args.append(" "); //$NON-NLS-1$
+			args.append(args2);
+		}
+		return args.toString();
+	}
 }
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/advanced/AdvancedJavaLaunchDelegate.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/advanced/AdvancedJavaLaunchDelegate.java
index ead7a35..583038d 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/advanced/AdvancedJavaLaunchDelegate.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/advanced/AdvancedJavaLaunchDelegate.java
@@ -10,13 +10,6 @@
  *******************************************************************************/
 package org.eclipse.jdt.launching.sourcelookup.advanced;
 
-import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.createAdvancedLaunch;
-import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.getJavaagentString;
-import static org.eclipse.jdt.internal.launching.sourcelookup.advanced.AdvancedSourceLookupSupport.isAdvancedSourcelookupEnabled;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.debug.core.ILaunch;
-import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.jdt.launching.JavaLaunchDelegate;
 
 /**
@@ -27,20 +20,8 @@
  */
 public class AdvancedJavaLaunchDelegate extends JavaLaunchDelegate {
 
-	@Override
-	public String getVMArguments(ILaunchConfiguration configuration) throws CoreException {
-		if (!isAdvancedSourcelookupEnabled()) {
-			return super.getVMArguments(configuration);
-		}
-		// TODO wish we had API similar to zt-exec or at least commons-exec
-		return getJavaagentString() + " " + super.getVMArguments(configuration); //$NON-NLS-1$
+	public AdvancedJavaLaunchDelegate() {
+		allowAdvancedSourcelookup();
 	}
 
-	@Override
-	public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
-		if (!isAdvancedSourcelookupEnabled()) {
-			return super.getLaunch(configuration, mode);
-		}
-		return createAdvancedLaunch(configuration, mode);
-	}
 }