Bug 527214 - ITargetDefinition#getTargetLocations null result safety

Change-Id: Icaba1c8aeff0f07474860c1ac377841d1ba32d43
Signed-off-by: Karsten Thoms <karsten.thoms@itemis.de>
diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/ExportTargetJob.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/ExportTargetJob.java
index 7f707f8..b7b6200 100644
--- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/ExportTargetJob.java
+++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/ExportTargetJob.java
@@ -50,6 +50,9 @@
 		try {
 			constructFilter(fTarget);
 			ITargetLocation[] containers = fTarget.getTargetLocations();
+			if (containers == null) {
+				containers = new ITargetLocation[0];
+			}
 			int totalWork = containers.length;
 			monitor.beginTask(PDECoreMessages.ExportTargetDefinition_task, totalWork);
 
diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java
index b877d40..81be3fe 100644
--- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java
+++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java
@@ -1247,6 +1247,9 @@
 	private URI[] getArtifactRepositories(ITargetDefinition target) throws CoreException {
 		Set<URI> result = new HashSet<>();
 		ITargetLocation[] containers = target.getTargetLocations();
+		if (containers == null) {
+			containers = new ITargetLocation[0];
+		}
 		IArtifactRepositoryManager manager = getArtifactRepositoryManager();
 		for (ITargetLocation container : containers) {
 			if (container instanceof IUBundleContainer) {
@@ -1346,6 +1349,9 @@
 
 		HashSet<IInstallableUnit> result = new HashSet<>();
 		ITargetLocation[] containers = definition.getTargetLocations();
+		if (containers == null) {
+			return new IInstallableUnit[0];
+		}
 		SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.IUBundleContainer_0, containers.length * 10);
 		MultiStatus status = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories, null);
 		for (ITargetLocation container : containers) {
@@ -1374,6 +1380,9 @@
 	private URI[] getMetadataRepositories(ITargetDefinition target) throws CoreException {
 		Set<URI> result = new HashSet<>();
 		ITargetLocation[] containers = target.getTargetLocations();
+		if (containers == null) {
+			containers = new ITargetLocation[0];
+		}
 		IMetadataRepositoryManager manager = getRepoManager();
 		for (ITargetLocation container : containers) {
 			if (container instanceof IUBundleContainer) {
diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java
index 27371bc..5c672f5 100644
--- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java
+++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/TargetPlatformService.java
@@ -628,12 +628,14 @@
 
 	private StringBuilder getVMArguments(ITargetLocation[] containers) {
 		StringBuilder arguments = new StringBuilder(""); //$NON-NLS-1$
-		for (ITargetLocation container : containers) {
-			String[] vmargs = container.getVMArguments();
-			if (vmargs == null)
-				continue;
-			for (String vmarg : vmargs) {
-				arguments.append(vmarg).append(' ');
+		if (containers != null) {
+			for (ITargetLocation container : containers) {
+				String[] vmargs = container.getVMArguments();
+				if (vmargs == null)
+					continue;
+				for (String vmarg : vmargs) {
+					arguments.append(vmarg).append(' ');
+				}
 			}
 		}
 		return arguments;
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/EditBundleContainerWizard.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/EditBundleContainerWizard.java
index f9ff3a9..af1dfa9 100644
--- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/EditBundleContainerWizard.java
+++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/EditBundleContainerWizard.java
@@ -63,7 +63,7 @@
 
 			// Add the new container or replace the old one
 			ITargetLocation newContainer = fPage.getBundleContainer();
-			if (newContainer != null) {
+			if (newContainer != null && fTarget.getTargetLocations() != null) {
 				ITargetLocation[] containers = fTarget.getTargetLocations();
 				List<ITargetLocation> newContainers = new ArrayList<>(containers.length);
 				for (int i = 0; i < containers.length; i++) {
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java
index 9b4e10f..43db972 100644
--- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java
+++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2016 IBM Corporation and others.
+ * Copyright (c) 2009, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -262,16 +262,10 @@
 			IStructuredSelection selection = (IStructuredSelection) event.getSelection();
 			Object first = selection.getFirstElement();
 			fTree.setChecked(first, !fTree.getChecked(first));
-			saveIncludedBundleState();
-			contentChanged();
-			updateButtons();
-			fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+			handleTreeStateChange();
 		});
 		fTree.addCheckStateListener(event -> {
-			saveIncludedBundleState();
-			contentChanged();
-			updateButtons();
-			fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+			handleTreeStateChange();
 		});
 		fTree.addSelectionChangedListener(event -> updateButtons());
 		fTree.setSorter(new ViewerSorter() {
@@ -319,6 +313,17 @@
 		return fTree;
 	}
 
+	private void handleTreeStateChange() {
+		saveIncludedBundleState();
+		contentChanged();
+		updateButtons();
+		ITargetLocation[] locations = fTargetDefinition.getTargetLocations();
+		if (locations == null) {
+			locations = new ITargetLocation[0];
+		}
+		fTree.update(locations, new String[] { IBasicPropertyConstants.P_TEXT });
+	}
+
 	/**
 	 * Creates the buttons in this group inside a new composite
 	 *
@@ -456,10 +461,7 @@
 				for (Object selectedObject : selected) {
 					fTree.setChecked(selectedObject, true);
 				}
-				saveIncludedBundleState();
-				contentChanged();
-				updateButtons();
-				fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+				handleTreeStateChange();
 			}
 		}));
 
@@ -469,27 +471,18 @@
 				for (Object selectedObject : selected) {
 					fTree.setChecked(selectedObject, false);
 				}
-				saveIncludedBundleState();
-				contentChanged();
-				updateButtons();
-				fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+				handleTreeStateChange();
 			}
 		}));
 
 		fSelectAllButton.addSelectionListener(widgetSelectedAdapter(e -> {
 			fTree.setAllChecked(true);
-			saveIncludedBundleState();
-			contentChanged();
-			updateButtons();
-			fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+			handleTreeStateChange();
 		}));
 
 		fDeselectAllButton.addSelectionListener(widgetSelectedAdapter(e -> {
 			fTree.setAllChecked(false);
-			saveIncludedBundleState();
-			contentChanged();
-			updateButtons();
-			fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+			handleTreeStateChange();
 		}));
 
 		fSelectRequiredButton.addSelectionListener(widgetSelectedAdapter(e -> {
@@ -503,10 +496,7 @@
 			for (Object requiredObject : required) {
 				fTree.setChecked(requiredObject, true);
 			}
-			saveIncludedBundleState();
-			contentChanged();
-			updateButtons();
-			fTree.update(fTargetDefinition.getTargetLocations(), new String[] {IBasicPropertyConstants.P_TEXT});
+			handleTreeStateChange();
 		}));
 
 		fPluginModeButton.addSelectionListener(widgetSelectedAdapter(e -> {
@@ -1205,7 +1195,7 @@
 					if (fTargetDefinition.getOtherBundles().length > 0) {
 						result.add(OTHER_CATEGORY);
 					}
-				} else if (fGrouping == GROUP_BY_CONTAINER) {
+				} else if (fGrouping == GROUP_BY_CONTAINER && fTargetDefinition.getTargetLocations() != null) {
 					result.addAll(Arrays.asList(fTargetDefinition.getTargetLocations()));
 				} else if (fGrouping == GROUP_BY_NONE) {
 					// Missing bundles are already handled by adding to fMissing, avoid adding twice
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetLocationsGroup.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetLocationsGroup.java
index 093cfb4..4cd2974 100644
--- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetLocationsGroup.java
+++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetLocationsGroup.java
@@ -33,7 +33,8 @@
 import org.eclipse.pde.ui.target.ITargetLocationEditor;
 import org.eclipse.pde.ui.target.ITargetLocationUpdater;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.*;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.*;
@@ -377,7 +378,9 @@
 
 			if (removedContainer) {
 				Set<ITargetLocation> newContainers = new HashSet<>();
-				newContainers.addAll(Arrays.asList(fTarget.getTargetLocations()));
+				if (fTarget.getTargetLocations() != null) {
+					newContainers.addAll(Arrays.asList(fTarget.getTargetLocations()));
+				}
 				newContainers.removeAll(toRemove);
 				if (newContainers.size() > 0) {
 					fTarget.setTargetLocations(newContainers.toArray(new ITargetLocation[newContainers.size()]));