Bug 547096 - [display] show value type instead of variable type

Added test for displayVariableTypeNames option.
Extracted appendTypeName, skip getReferenceTypeName() call if option is
disabled.

Change-Id: I82cf0af9bc8feffe6484392c3f73c92c85637914
Signed-off-by: Julian Honnen <julian.honnen@vector.com>
diff --git a/org.eclipse.jdt.debug.tests/testprograms/ModelPresentationTests.java b/org.eclipse.jdt.debug.tests/testprograms/ModelPresentationTests.java
new file mode 100644
index 0000000..e8d8fa8
--- /dev/null
+++ b/org.eclipse.jdt.debug.tests/testprograms/ModelPresentationTests.java
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ *  Copyright (c) 2020 Julian Honnen
+ *
+ *  This program and the accompanying materials
+ *  are made available under the terms of the Eclipse Public License 2.0
+ *  which accompanies this distribution, and is available at
+ *  https://www.eclipse.org/legal/epl-2.0/
+ *
+ *  SPDX-License-Identifier: EPL-2.0
+ *
+ *  Contributors:
+ *     Julian Honnen <julian.honnen@vector.com> - initial API and implementation
+ *******************************************************************************/
+
+public class ModelPresentationTests {
+
+	public static void main(String[] args) {
+		Object stringArray = new String[0];
+		System.out.println(stringArray);
+	}
+
+}
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
index 98a5c47..e16bd72 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
@@ -206,7 +206,7 @@
 			"org.eclipse.debug.tests.targets.HcrClass9", "TestContributedStepFilterClass", "TerminateAll_01", "TerminateAll_02", "StepResult1",
 			"StepResult2", "StepResult3", "StepUncaught", "TriggerPoint_01", "BulkThreadCreationTest", "MethodExitAndException",
 			"Bug534319earlyStart", "Bug534319lateStart", "Bug534319singleThread", "Bug534319startBetwen", "MethodCall", "Bug538303", "Bug540243",
-			"OutSync", "OutSync2", "ConsoleOutputUmlaut", "ErrorRecurrence" };
+			"OutSync", "OutSync2", "ConsoleOutputUmlaut", "ErrorRecurrence", "ModelPresentationTests" };
 
 	/**
 	 * the default timeout
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/presentation/ModelPresentationTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/presentation/ModelPresentationTests.java
index 7a7ead3..b996ff5 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/presentation/ModelPresentationTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/presentation/ModelPresentationTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011 IBM Corporation and others.
+ * Copyright (c) 2011, 2020 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -13,8 +13,14 @@
  *******************************************************************************/
 package org.eclipse.jdt.debug.tests.ui.presentation;
 
+import org.eclipse.debug.ui.IDebugModelPresentation;
 import org.eclipse.jdt.core.Signature;
+import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
+import org.eclipse.jdt.debug.core.IJavaObject;
+import org.eclipse.jdt.debug.core.IJavaStackFrame;
+import org.eclipse.jdt.debug.core.IJavaThread;
 import org.eclipse.jdt.debug.core.IJavaValue;
+import org.eclipse.jdt.debug.core.IJavaVariable;
 import org.eclipse.jdt.debug.tests.AbstractDebugTest;
 import org.eclipse.jdt.internal.debug.ui.JDIModelPresentation;
 
@@ -136,4 +142,33 @@
 			pres.dispose();
 		}
 	}
+
+	/**
+	 * Tests displayVariableTypeNames option
+	 */
+	public void testShowTypeTest() throws Exception {
+		String typeName = "ModelPresentationTests";
+		IJavaLineBreakpoint bp = createLineBreakpoint(19, typeName);
+		JDIModelPresentation pres = new JDIModelPresentation();
+
+		IJavaThread thread = null;
+		try {
+			thread = launchToLineBreakpoint(typeName, bp);
+
+			IJavaStackFrame frame = (IJavaStackFrame) thread.getTopStackFrame();
+			IJavaVariable stringArrayVariable = findVariable(frame, "stringArray");
+			long id = ((IJavaObject) stringArrayVariable.getValue()).getUniqueId();
+
+			assertEquals("stringArray= String[0]  (id=" + id + ")", pres.getText(stringArrayVariable));
+
+			pres.setAttribute(IDebugModelPresentation.DISPLAY_VARIABLE_TYPE_NAMES, Boolean.TRUE);
+
+			assertEquals("String[] stringArray= String[0]  (id=" + id + ")", pres.getText(stringArrayVariable));
+
+		} finally {
+			terminateAndRemove(thread);
+			removeAllBreakpoints();
+			pres.dispose();
+		}
+	}
 }
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
index c5696fa..3008731 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
@@ -1295,22 +1295,8 @@
 			javaValue = (IJavaValue) var.getValue();
 		} catch (DebugException e1) {
 		}
-		boolean showTypes= isShowVariableTypeNames();
 		StringBuilder buff= new StringBuilder();
-		String typeName= DebugUIMessages.JDIModelPresentation_unknown_type__2;
-		try {
-			if (javaValue != null) {
-				typeName = javaValue.getReferenceTypeName();
-				if (showTypes) {
-					typeName = getQualifiedName(typeName);
-				}
-			}
-		} catch (DebugException exception) {
-		}
-		if (showTypes) {
-			buff.append(typeName);
-			buff.append(' ');
-		}
+		appendTypeName(javaValue, buff);
 		buff.append(varLabel);
 
 		// add declaring type name if required
@@ -1335,6 +1321,22 @@
 		return buff.toString();
 	}
 
+	private void appendTypeName(IJavaValue javaValue, StringBuilder buff) {
+		if (!isShowVariableTypeNames()) {
+			return;
+		}
+
+		String typeName = DebugUIMessages.JDIModelPresentation_unknown_type__2;
+		try {
+			if (javaValue != null) {
+				typeName = getQualifiedName(javaValue.getReferenceTypeName());
+			}
+		} catch (DebugException exception) {
+		}
+		buff.append(typeName);
+		buff.append(' ');
+	}
+
 	/**
 	 * Returns text for the given value based on user preferences to display
 	 * toString() details.