Bug 513185 - FillLayout throws ClassCastExecption for children with
wrong layout data

Change-Id: I2a1fdce982d9b3257849e5b45cb117ddd34df39e
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
index f66f280..250ccc2 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2020 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Christoph Läubrich - Bug 513185
  *******************************************************************************/
 package org.eclipse.swt.layout;
 
@@ -145,7 +146,7 @@
 }
 
 Point computeChildSize (Control control, int wHint, int hHint, boolean flushCache) {
-	FillData data = (FillData)control.getLayoutData ();
+	FillData data = validateLayoutData(control, -1);
 	if (data == null) {
 		data = new FillData ();
 		control.setLayoutData (data);
@@ -172,8 +173,10 @@
 
 @Override
 protected boolean flushCache (Control control) {
-	Object data = control.getLayoutData();
-	if (data != null) ((FillData)data).flushCache();
+	FillData data = validateLayoutData(control, -1);
+	if (data != null) {
+		data.flushCache();
+	}
 	return true;
 }
 
@@ -198,6 +201,7 @@
 		int y = rect.y + marginHeight, cellWidth = width / count;
 		for (int i=0; i<count; i++) {
 			Control child = children [i];
+			validateLayoutData(child, i);
 			int childWidth = cellWidth;
 			if (i == 0) {
 				childWidth += extra / 2;
@@ -213,6 +217,7 @@
 		int y = rect.y + marginHeight, extra = height % count;
 		for (int i=0; i<count; i++) {
 			Control child = children [i];
+			validateLayoutData(child, i);
 			int childHeight = cellHeight;
 			if (i == 0) {
 				childHeight += extra / 2;
@@ -225,6 +230,21 @@
 	}
 }
 
+private FillData validateLayoutData(Control control, int index) {
+	if (control == null) {
+		throw new SWTException(SWT.ERROR_NULL_ARGUMENT, "control can't be null");
+	}
+	Object layoutData = control.getLayoutData();
+	if (layoutData == null) {
+		return null;
+	}
+	if (layoutData instanceof FillData) {
+		return (FillData) layoutData;
+	}
+	throw new SWTException(SWT.ERROR_INVALID_ARGUMENT, "Child#" + index + " of type " + control.getClass().getName()
+			+ " has illegal layout data type " + layoutData.getClass().getName());
+}
+
 /**
  * Returns a string containing a concise, human-readable
  * description of the receiver.