private EvaluatedExpression GetValueInScope(string name) { Contract.Requires <ArgumentNullException>(name != null, "name"); Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name)); IMethod method = _stackFrame.GetLocation().GetMethod(); // check the stack frame if (_stackFrame.GetHasVariableInfo()) { ILocalVariable variable = _stackFrame.GetVisibleVariableByName(name); if (variable != null) { return(new EvaluatedExpression(name, name, variable, _stackFrame.GetValue(variable), false)); } ReadOnlyCollection <ILocalVariable> variables = method.GetVariablesByName(name); if (variables.Count > 0) { throw new InvalidOperationException("Evaluation failed because the variable is not visible in the current scope."); } } // check the enclosing object for a visible field IReferenceType declaringType = method.GetDeclaringType(); IField field = declaringType.GetFieldByName(name); if (field != null) { if (field.GetIsStatic()) { return(new EvaluatedExpression(name, name, null, field, declaringType.GetValue(field), false)); } if (method.GetIsStatic()) { throw new InvalidOperationException("The instance field cannot be accessed from a static method."); } return(new EvaluatedExpression(name, name, _stackFrame.GetThisObject(), field, _stackFrame.GetThisObject().GetValue(field), false)); } // check the outer object IField outerClassField = declaringType.GetFieldByName("this$0"); IObjectReference nestedClassInstance = null; while (outerClassField != null) { if (nestedClassInstance == null) { nestedClassInstance = _stackFrame.GetThisObject(); } else { nestedClassInstance = nestedClassInstance.GetValue(outerClassField) as IObjectReference; } if (nestedClassInstance == null) { break; } // what if this$0 is defined in a superclass? IClassType fieldType = outerClassField.GetFieldType() as IClassType; field = fieldType.GetFieldByName(name); if (field != null) { if (field.GetIsStatic()) { return(new EvaluatedExpression(name, name, null, field, fieldType.GetValue(field), false)); } if (method.GetIsStatic()) { throw new InvalidOperationException("The instance field cannot be accessed from a static method."); } return(new EvaluatedExpression(name, name, nestedClassInstance, field, nestedClassInstance.GetValue(field), false)); } outerClassField = fieldType.GetFieldByName("this$0"); } throw new InvalidOperationException("No member by this name is available in the current scope."); }
public int EnumChildren(enum_DEBUGPROP_INFO_FLAGS dwFields, uint dwRadix, ref Guid guidFilter, enum_DBG_ATTRIB_FLAGS dwAttribFilter, string pszNameFilter, uint dwTimeout, out IEnumDebugPropertyInfo2 ppEnum) { Stopwatch stopwatch = Stopwatch.StartNew(); bool infinite = dwTimeout == unchecked ((uint)Timeout.Infinite); TimeSpan timeout = infinite ? TimeSpan.Zero : TimeSpan.FromMilliseconds(dwTimeout); IObjectReference objectReference = _evaluatedExpression.Value as IObjectReference; IReferenceType referenceType = _evaluatedExpression.ValueType as IReferenceType; if (objectReference == null || referenceType == null) { ppEnum = new EnumDebugPropertyInfo(Enumerable.Empty <DEBUG_PROPERTY_INFO>()); return(VSConstants.S_OK); } ppEnum = null; bool getFullName = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME) != 0; bool getName = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME) != 0; bool getType = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE) != 0; bool getValue = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE) != 0; bool getAttributes = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB) != 0; bool getProperty = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP) != 0; bool useAutoExpandValue = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_AUTOEXPAND) != 0; bool noFormatting = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_RAW) != 0; bool noToString = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_NO_TOSTRING) != 0; List <DEBUG_PROPERTY_INFO> properties = new List <DEBUG_PROPERTY_INFO>(); DEBUG_PROPERTY_INFO[] propertyInfo = new DEBUG_PROPERTY_INFO[1]; bool isCollection = false; int collectionSize = 0; bool haveCollectionValues = false; ReadOnlyCollection <IValue> collectionValues = null; IType componentType = null; if (!_useRawValues) { isCollection = TryGetCollectionSize(objectReference, out collectionSize); if (isCollection && (getValue || getProperty)) { haveCollectionValues = TryGetCollectionValues(objectReference, out collectionValues, out componentType); } } if (isCollection) { for (int i = 0; i < collectionSize; i++) { propertyInfo[0] = default(DEBUG_PROPERTY_INFO); if (haveCollectionValues) { string name = "[" + i + "]"; IType propertyType = componentType; IValue value = collectionValues[i]; JavaDebugProperty property = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + name, propertyType, value, _evaluatedExpression.HasSideEffects); bool timedout = !infinite && timeout < stopwatch.Elapsed; enum_DEBUGPROP_INFO_FLAGS fields = dwFields; if (timedout) { fields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_NO_TOSTRING; } int hr = property.GetPropertyInfo(fields, dwRadix, dwTimeout, null, 0, propertyInfo); if (ErrorHandler.Failed(hr)) { return(hr); } if (timedout) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_TIMEOUT; } properties.Add(propertyInfo[0]); continue; } else { if (getFullName) { propertyInfo[0].bstrFullName = _evaluatedExpression.FullName + "[" + i + "]"; propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME; } if (getName) { propertyInfo[0].bstrName = "[" + i + "]"; propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME; } if (getType) { propertyInfo[0].bstrType = componentType.GetName(); propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE; } if (getAttributes) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_AUTOEXPANDED; propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_READONLY; if (_evaluatedExpression.HasSideEffects) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_SIDE_EFFECT; } propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB; } } } bool useRawValues = referenceType is IClassType; if (useRawValues) { if (_rawValuesProperty == null) { _rawValuesProperty = new JavaDebugProperty(this, "Raw Values") { _useRawValues = true }; } propertyInfo[0] = default(DEBUG_PROPERTY_INFO); ErrorHandler.ThrowOnFailure(_rawValuesProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); properties.Add(propertyInfo[0]); } } else { ReadOnlyCollection <IField> fields = referenceType.GetFields(false); List <IField> staticFields = new List <IField>(fields.Where(i => i.GetIsStatic())); bool useMostDerived = !_preventMostDerived && _evaluatedExpression.Value != null && !_evaluatedExpression.Value.GetValueType().Equals(referenceType); if (useMostDerived) { if (_mostDerivedProperty == null) { _mostDerivedProperty = new JavaDebugProperty(this, string.Format("[{0}]", _evaluatedExpression.Value.GetValueType().GetName()), _evaluatedExpression.Value.GetValueType()) { _preventMostDerived = true }; } propertyInfo[0] = default(DEBUG_PROPERTY_INFO); ErrorHandler.ThrowOnFailure(_mostDerivedProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); properties.Add(propertyInfo[0]); } bool useSuper = false; if (!useMostDerived) { IClassType valueType = _evaluatedExpression.ValueType as IClassType; if (valueType != null) { IClassType superClass = valueType.GetSuperclass(); useSuper = superClass != null && superClass.GetName() != "java.lang.Object"; if (useSuper) { if (_superProperty == null) { _superProperty = new JavaDebugProperty(this, "[super]", superClass) { _preventMostDerived = true }; } propertyInfo[0] = default(DEBUG_PROPERTY_INFO); ErrorHandler.ThrowOnFailure(_superProperty.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); properties.Add(propertyInfo[0]); } } } foreach (var field in fields) { if (field.GetIsStatic()) { continue; } propertyInfo[0] = default(DEBUG_PROPERTY_INFO); if (getValue || getProperty) { IDebugProperty2 property; try { string name = field.GetName(); IType propertyType = field.GetFieldType(); IValue value = objectReference.GetValue(field); property = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + "." + name, propertyType, value, _evaluatedExpression.HasSideEffects, field); ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } string name = field.GetName(); IType propertyType = field.GetFieldType(); IValue value = field.GetVirtualMachine().GetMirrorOf(0); property = new JavaDebugProperty(this, name, _evaluatedExpression.FullName + "." + name, propertyType, value, _evaluatedExpression.HasSideEffects, field); ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); } } else { if (getFullName) { propertyInfo[0].bstrFullName = _evaluatedExpression.FullName + "." + field.GetName(); propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME; } if (getName) { propertyInfo[0].bstrName = field.GetName(); propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME; } if (getType) { propertyInfo[0].bstrType = field.GetFieldTypeName(); propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE; } if (getAttributes) { if (field.GetIsStatic()) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_STORAGE_STATIC; } if (field.GetIsPrivate()) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PRIVATE; } if (field.GetIsProtected()) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PROTECTED; } if (field.GetIsPublic()) { propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PUBLIC; } propertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_READONLY; propertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB; #if false bool expandable; bool hasId; bool canHaveId; bool readOnly; bool error; bool sideEffect; bool overloadedContainer; bool boolean; bool booleanTrue; bool invalid; bool notAThing; bool autoExpanded; bool timeout; bool rawString; bool customViewer; bool accessNone; bool accessPrivate; bool accessProtected; bool accessPublic; bool storageNone; bool storageGlobal; bool storageStatic; bool storageRegister; bool noModifiers; bool @virtual; bool constant; bool synchronized; bool @volatile; bool dataField; bool method; bool property; bool @class; bool baseClass; bool @interface; bool innerClass; bool mostDerived; bool multiCustomViewers; #endif } } properties.Add(propertyInfo[0]); continue; } if (staticFields.Count > 0) { propertyInfo[0] = default(DEBUG_PROPERTY_INFO); JavaDebugStaticMembersPseudoProperty property = new JavaDebugStaticMembersPseudoProperty(this, referenceType, staticFields); ErrorHandler.ThrowOnFailure(property.GetPropertyInfo(dwFields, dwRadix, dwTimeout, null, 0, propertyInfo)); properties.Add(propertyInfo[0]); } } ppEnum = new EnumDebugPropertyInfo(properties); return(VSConstants.S_OK); }
private EvaluatedExpression GetField(EvaluatedExpression value, string name) { Contract.Requires <ArgumentNullException>(value != null, "value"); Contract.Requires <ArgumentNullException>(name != null, "name"); Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name)); IArrayReference arrayValue = value.Value as IArrayReference; if (arrayValue != null) { if (name == "length") { string fullName = string.Format("({0}).{1}", value.FullName, name); return(new EvaluatedExpression(name, fullName, _stackFrame.GetVirtualMachine().GetMirrorOf(arrayValue.GetLength()), value.HasSideEffects)); } else { throw new InvalidOperationException(); } } IReferenceType declaringType = value.ValueType as IReferenceType; if (declaringType == null) { throw new InvalidOperationException(); } IField field = declaringType.GetFieldByName(name); if (field != null) { string fullName = string.Format("({0}).{1}", value.FullName, name); if (field.GetIsStatic()) { return(new EvaluatedExpression(name, fullName, null, field, declaringType.GetValue(field), value.HasSideEffects)); } else { IObjectReference objectReference = value.Value as IObjectReference; if (objectReference == null) { throw new InvalidOperationException("Evaluation failed (todo: distinguish between null pointer and instance field referenced as a static field)."); } return(new EvaluatedExpression(name, fullName, objectReference, field, objectReference.GetValue(field), value.HasSideEffects)); } } throw new NotImplementedException(); }