Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.debug.core;

import java.util.logging.Logger;

import com.google.gson.JsonSyntaxException;
import com.microsoft.java.debug.core.protocol.JsonUtils;

public final class DebugSettings {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static DebugSettings current = new DebugSettings();

public int maxStringLength = 0;
public boolean showStaticVariables = true;
public boolean showQualifiedNames = false;
public boolean showHex = false;
public String logLevel;

public static DebugSettings getCurrent() {
return current;
}

/**
* Update current settings with the values in the parameter.
*
* @param jsonSettings
* the new settings represents in json format.
*/
public void updateSettings(String jsonSettings) {
try {
current = JsonUtils.fromJson(jsonSettings, DebugSettings.class);
} catch (JsonSyntaxException ex) {
logger.severe(String.format("Invalid json for debugSettings: %s, %s", jsonSettings, ex.getMessage()));
}
}

private DebugSettings() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class StringObjectFormatter extends ObjectFormatter implements IValueFormatter {
public static final String MAX_STRING_LENGTH_OPTION = "max_string_length";
private static final int DEFAULT_MAX_STRING_LENGTH = 100;
private static final int DEFAULT_MAX_STRING_LENGTH = 0;
private static final String QUOTE_STRING = "\"";

public StringObjectFormatter() {
Expand All @@ -42,9 +42,9 @@ public Map<String, Object> getDefaultOptions() {

@Override
public String toString(Object value, Map<String, Object> options) {
int maxLength = getMaxStringLength(options);
return String.format("\"%s\" %s",
StringUtils.abbreviate(((StringReference) value).value(),
getMaxStringLength(options)),
maxLength > 0 ? StringUtils.abbreviate(((StringReference) value).value(), maxLength) : ((StringReference) value).value(),
getIdPostfix((ObjectReference) value, options));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

import org.apache.commons.lang3.StringUtils;

import com.microsoft.java.debug.core.DebugSettings;
import com.microsoft.java.debug.core.adapter.AdapterUtils;
import com.microsoft.java.debug.core.adapter.ErrorCode;
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatEnum;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatter;
import com.microsoft.java.debug.core.adapter.formatter.SimpleTypeFormatter;
import com.microsoft.java.debug.core.adapter.variables.JdiObjectProxy;
import com.microsoft.java.debug.core.adapter.variables.Variable;
import com.microsoft.java.debug.core.adapter.variables.VariableProxy;
Expand Down Expand Up @@ -61,18 +59,10 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
return;
}

// This should be false by default(currently true for test).
// User will need to explicitly turn it on by configuring launch.json
final boolean showStaticVariables = true;
// TODO: when vscode protocol support customize settings of value format, showFullyQualifiedNames should be one of the options.
boolean showFullyQualifiedNames = true;
final boolean showStaticVariables = DebugSettings.getCurrent().showStaticVariables;

Map<String, Object> options = context.getVariableFormatter().getDefaultOptions();
if (evalArguments.format != null && evalArguments.format.hex) {
options.put(NumericFormatter.NUMERIC_FORMAT_OPTION, NumericFormatEnum.HEX);
}
if (showFullyQualifiedNames) {
options.put(SimpleTypeFormatter.QUALIFIED_CLASS_NAME_OPTION, showFullyQualifiedNames);
}
VariableUtils.applyFormatterOptions(options, evalArguments.format != null && evalArguments.format.hex);
String expression = evalArguments.expression;

if (StringUtils.isBlank(expression)) {
Expand Down Expand Up @@ -174,7 +164,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
if (currentValue instanceof ObjectReference && VariableUtils.hasChildren(currentValue, showStaticVariables)) {
// save the evaluated value in object pool, because like java.lang.String, the evaluated object will have sub structures
// we need to set up the id map.
VariableProxy varProxy = new VariableProxy(thread.uniqueID(), "Local", (ObjectReference) currentValue);
VariableProxy varProxy = new VariableProxy(thread.uniqueID(), "Local", currentValue);
referenceId = context.getRecyclableIdPool().addObject(thread.uniqueID(), varProxy);
}
int indexedVariables = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import java.util.Set;
import java.util.stream.Collectors;

import com.microsoft.java.debug.core.DebugSettings;
import com.microsoft.java.debug.core.adapter.AdapterUtils;
import com.microsoft.java.debug.core.adapter.ErrorCode;
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatEnum;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatter;
import com.microsoft.java.debug.core.adapter.formatter.SimpleTypeFormatter;
import com.microsoft.java.debug.core.adapter.variables.IVariableFormatter;
import com.microsoft.java.debug.core.adapter.variables.Variable;
import com.microsoft.java.debug.core.adapter.variables.VariableProxy;
Expand Down Expand Up @@ -57,18 +55,11 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
IVariableFormatter variableFormatter = context.getVariableFormatter();
VariablesArguments varArgs = (VariablesArguments) arguments;


boolean showStaticVariables = DebugSettings.getCurrent().showStaticVariables;

Map<String, Object> options = variableFormatter.getDefaultOptions();
// This should be false by default(currently true for test).
// User will need to explicitly turn it on by configuring launch.json
boolean showStaticVariables = true;
// TODO: When vscode protocol support customize settings of value format, showFullyQualifiedNames should be one of the options.
boolean showFullyQualifiedNames = true;
if (varArgs.format != null && varArgs.format.hex) {
options.put(NumericFormatter.NUMERIC_FORMAT_OPTION, NumericFormatEnum.HEX);
}
if (showFullyQualifiedNames) {
options.put(SimpleTypeFormatter.QUALIFIED_CLASS_NAME_OPTION, showFullyQualifiedNames);
}
VariableUtils.applyFormatterOptions(options, varArgs.format != null && varArgs.format.hex);

List<Types.Variable> list = new ArrayList<>();
Object container = context.getRecyclableIdPool().getObjectById(varArgs.variablesReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.DebugSettings;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatEnum;
import com.microsoft.java.debug.core.adapter.formatter.NumericFormatter;
import com.microsoft.java.debug.core.adapter.formatter.SimpleTypeFormatter;
import com.microsoft.java.debug.core.adapter.formatter.StringObjectFormatter;
import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.ArrayReference;
import com.sun.jdi.ArrayType;
Expand Down Expand Up @@ -222,6 +228,28 @@ public static List<Variable> listStaticVariables(StackFrame stackFrame) {
return res;
}

/**
* Apply the display options for variable formatter, it is used in variable and evaluate requests, controls the display content in
* variable view/debug console.
*
* @param defaultOptions the initial options for adding options from user settings
* @param hexInArgument when request sent by vscode declare hex format explicitly, settings this parameter true to override value in DebugSettings class.
*/
public static void applyFormatterOptions(Map<String, Object> defaultOptions, boolean hexInArgument) {
Map<String, Object> options = defaultOptions;
boolean showFullyQualifiedNames = DebugSettings.getCurrent().showQualifiedNames;
if (hexInArgument || DebugSettings.getCurrent().showHex) {
options.put(NumericFormatter.NUMERIC_FORMAT_OPTION, NumericFormatEnum.HEX);
}
if (showFullyQualifiedNames) {
options.put(SimpleTypeFormatter.QUALIFIED_CLASS_NAME_OPTION, true);
}

if (DebugSettings.getCurrent().maxStringLength > 0) {
options.put(StringObjectFormatter.MAX_STRING_LENGTH_OPTION, DebugSettings.getCurrent().maxStringLength);
}
}

private VariableUtils() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

package com.microsoft.java.debug.core.adapter.formatter;

import static com.microsoft.java.debug.core.adapter.formatter.StringObjectFormatter.MAX_STRING_LENGTH_OPTION;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -23,15 +30,9 @@
import com.sun.jdi.StringReference;
import com.sun.jdi.Value;

import static junit.framework.TestCase.assertNull;
import static com.microsoft.java.debug.core.adapter.formatter.StringObjectFormatter.MAX_STRING_LENGTH_OPTION;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class StringObjectFormatterTest extends BaseJdiTestCase {
protected StringObjectFormatter formatter;
@Override
@Before
public void setup() throws Exception {
super.setup();
Expand Down Expand Up @@ -72,8 +73,8 @@ public void testToString() throws Exception {
assertEquals("Should be able to format string type.", String.format("\"st...\" (id=%d)",
((ObjectReference) string).uniqueID()),
formatter.toString(string, options));
assertTrue("Should be able to trim long string..",
formatter.toString(string, new HashMap<>()).contains("aaaaaaaaaaaaaaaaaaaaaaa...\""));
assertTrue("Should not trim long string by default",
formatter.toString(string, new HashMap<>()).contains(((StringReference) string).value()));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion com.microsoft.java.debug.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<command id="vscode.java.resolveMainClass"/>
<command id="vscode.java.buildWorkspace"/>
<command id="vscode.java.fetchUsageData"/>
<command id="vscode.java.configLogLevel"/>
<command id="vscode.java.updateDebugSettings"/>
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.debug.plugin.internal;

import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonSyntaxException;
import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.DebugSettings;
import com.microsoft.java.debug.core.protocol.JsonUtils;

public final class DebugSettingUtils {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);

/**
* Configure setting for java debugger.
*
* @param arguments
* the arguments for the settings, the format is json,
* eg:{"showHex":true,"showStaticVariables":true,"maxStringLength":100}
*/
public static Object configDebugSettings(List<Object> arguments) {
if (arguments != null && arguments.size() > 0) {
arguments.forEach(arg -> {
if (arg instanceof String) {
String jsonStr = (String) arg;

try {
DebugSettings.getCurrent().updateSettings(jsonStr);
DebugSettings.getCurrent().logLevel = LogUtils.configLogLevel(DebugSettings.getCurrent().logLevel);
} catch (JsonSyntaxException ex) {
logger.severe(String.format("Parameters for userSettings must be a valid json: %s", String.valueOf(arg)));
}

} else {
logger.severe(String.format("Parameters for userSettings must be json string: %s", String.valueOf(arg)));
}
});

} else {
logger.severe(String.format("Invalid parameters for debugSettings: %s", StringUtils.join(arguments)));
}
return JsonUtils.toJson(DebugSettings.getCurrent());
}

/**
* Private constructor to prevent creating instance of this class.
*/
private DebugSettingUtils() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler

public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";

public static String CONFIG_LOG_LEVEL = "vscode.java.configLogLevel";
public static String UPDATE_DEBUG_SETTINGS = "vscode.java.updateDebugSettings";

@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
Expand All @@ -48,8 +48,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
// TODO
} else if (FETCH_USER_DATA.equals(commandId)) {
return UsageDataStore.getInstance().fetchAll();
} else if (CONFIG_LOG_LEVEL.equals(commandId)) {
return LogUtils.configLogLevel(arguments);
} else if (UPDATE_DEBUG_SETTINGS.equals(commandId)) {
return DebugSettingUtils.configDebugSettings(arguments);
}

throw new UnsupportedOperationException(String.format("Java debug plugin doesn't support the command '%s'.", commandId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

package com.microsoft.java.debug.plugin.internal;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;

import com.microsoft.java.debug.core.Configuration;

public final class LogUtils {
Expand All @@ -37,22 +34,22 @@ public static void initialize(Level level) {

/**
* Configure log level setting for java debugger.
* @param arguments the first element of the arguments should be the String representation of level(info, fine, warning..).
*
* @param logLevel
* the String representation of level(info, fine, warning..).
* @return the updated log level
*/
public static Object configLogLevel(List<Object> arguments) {
if (arguments != null && arguments.size() == 1 && arguments.get(0) instanceof String) {
try {
logger.setLevel(Level.parse((String) arguments.get(0)));
logger.info(String.format("Set log level to : %s", arguments.get(0)));
return logger.getLevel().toString();
} catch (IllegalArgumentException e) {
logger.severe(String.format("Invalid log level: %s", arguments.get(0)));
}

} else {
logger.severe(String.format("Invalid parameters for configLogLevel: %s", StringUtils.join(arguments)));
public static String configLogLevel(Object logLevel) {
try {
logger.setLevel(Level.parse((String) logLevel));
logger.info(String.format("Set log level to : %s", logLevel));
} catch (IllegalArgumentException e) {
logger.severe(String.format("Invalid log level: %s", logLevel));
} catch (ClassCastException e) {
logger.severe("logLevel should be a string.");
}
return null;

return logger.getLevel().toString();
}

}