From 80c2baf4641a36965f745df45aa1ce17072da223 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Tue, 15 Jul 2008 19:26:53 +0000 Subject: [PATCH] Fixed 240916 (casting a child of a global results in an NPE). Also fixed all warnings in changed files and improved code with generics --- .../debug/core/model/ICGlobalVariable.java | 2 +- .../internal/core/CGlobalVariableManager.java | 49 ++++++++++-------- .../internal/core/model/CGlobalVariable.java | 3 ++ .../cdt/debug/internal/core/model/CValue.java | 50 +++++++++---------- .../internal/core/model/CVariableFactory.java | 5 +- 5 files changed, 60 insertions(+), 49 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/model/ICGlobalVariable.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/model/ICGlobalVariable.java index 705d3a86afc..e83a8db8901 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/model/ICGlobalVariable.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/model/ICGlobalVariable.java @@ -16,7 +16,7 @@ package org.eclipse.cdt.debug.core.model; public interface ICGlobalVariable extends ICVariable { /** - * Returns the descriptor of this variable. + * Returns the descriptor of this variable. Will be null if a child of a global. * * @return the descriptor of this variable */ diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CGlobalVariableManager.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CGlobalVariableManager.java index 80440c1f157..e6dc0ce4845 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CGlobalVariableManager.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CGlobalVariableManager.java @@ -14,12 +14,13 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; + import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugUtils; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; @@ -61,7 +62,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { private IGlobalVariableDescriptor[] fInitialDescriptors = new IGlobalVariableDescriptor[0]; - private ArrayList fGlobals; + private List fGlobals; /** * Constructor for CGlobalVariableManager. @@ -89,16 +90,16 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { DebugPlugin.log( e ); } } - return (ICGlobalVariable[])fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); + return fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); } /* (non-Javadoc) * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#addGlobals(IGlobalVariableDescriptor[]) */ public void addGlobals( IGlobalVariableDescriptor[] descriptors ) throws DebugException { - fGlobals = new ArrayList( 10 ); + fGlobals = new ArrayList( 10 ); MultiStatus ms = new MultiStatus( CDebugCorePlugin.getUniqueIdentifier(), 0, "", null ); //$NON-NLS-1$ - ArrayList globals = new ArrayList( descriptors.length ); + List globals = new ArrayList( descriptors.length ); for ( int i = 0; i < descriptors.length; ++i ) { try { globals.add( getDebugTarget().createGlobalVariable( descriptors[i] ) ); @@ -142,7 +143,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { ICGlobalVariable[] globals = new ICGlobalVariable[0]; synchronized( fGlobals ) { - globals = (ICGlobalVariable[])fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); + globals = fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); fGlobals.clear(); } for ( int i = 0; i < globals.length; ++i ) { @@ -154,9 +155,8 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { public void dispose() { if ( fGlobals != null ) { - Iterator it = fGlobals.iterator(); - while( it.hasNext() ) { - ((CVariable)it.next()).dispose(); + for (ICGlobalVariable global : fGlobals) { + ((CVariable)global).dispose(); } fGlobals.clear(); fGlobals = null; @@ -170,12 +170,15 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { Element node = document.createElement( GLOBAL_VARIABLE_LIST ); document.appendChild( node ); ICGlobalVariable[] globals = getGlobals(); - for ( int i = 0; i < globals.length; ++i ) { - IGlobalVariableDescriptor descriptor = globals[i].getDescriptor(); - Element child = document.createElement( GLOBAL_VARIABLE ); - child.setAttribute( ATTR_GLOBAL_VARIABLE_NAME, descriptor.getName() ); - child.setAttribute( ATTR_GLOBAL_VARIABLE_PATH, descriptor.getPath().toOSString() ); - node.appendChild( child ); + for (ICGlobalVariable global : globals) { + IGlobalVariableDescriptor descriptor = global.getDescriptor(); + // children of globals don't have a descriptor, though getGlobals() shouldn't return only top level globals + if (descriptor != null) { + Element child = document.createElement( GLOBAL_VARIABLE ); + child.setAttribute( ATTR_GLOBAL_VARIABLE_NAME, descriptor.getName() ); + child.setAttribute( ATTR_GLOBAL_VARIABLE_PATH, descriptor.getPath().toOSString() ); + node.appendChild( child ); + } } return CDebugUtils.serializeDocument( document ); } @@ -200,7 +203,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { InputSource source = new InputSource( reader ); root = parser.parse( source ).getDocumentElement(); if ( root.getNodeName().equalsIgnoreCase( GLOBAL_VARIABLE_LIST ) ) { - List descriptors = new ArrayList(); + List descriptors = new ArrayList(); NodeList list = root.getChildNodes(); int length = list.getLength(); for( int i = 0; i < length; ++i ) { @@ -218,7 +221,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { } } } - fInitialDescriptors = (IGlobalVariableDescriptor[])descriptors.toArray( new IGlobalVariableDescriptor[descriptors.size()] ); + fInitialDescriptors = descriptors.toArray( new IGlobalVariableDescriptor[descriptors.size()] ); return; } } @@ -276,11 +279,13 @@ public class CGlobalVariableManager implements ICGlobalVariableManager { public IGlobalVariableDescriptor[] getDescriptors() { if ( fGlobals == null ) return getInitialDescriptors(); - IGlobalVariableDescriptor[] result = new IGlobalVariableDescriptor[fGlobals.size()]; - Iterator it = fGlobals.iterator(); - for ( int i = 0; it.hasNext(); ++i ) { - result[i] = ((ICGlobalVariable)it.next()).getDescriptor(); + List descrs = new ArrayList(); + for (ICGlobalVariable global : fGlobals) { + IGlobalVariableDescriptor descr = global.getDescriptor(); + if (descr != null) { // children of globals don't have a descriptor, though 'fGlobals' should contain only top level globals + descrs.add(descr); + } } - return result; + return descrs.toArray(new IGlobalVariableDescriptor[descrs.size()]); } } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CGlobalVariable.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CGlobalVariable.java index 8dc0579421b..21b4fda395b 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CGlobalVariable.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CGlobalVariable.java @@ -318,6 +318,9 @@ public class CGlobalVariable extends CVariable implements ICGlobalVariable { } } + /** + * Will be null for a child of a global (array member, struct field, etc) + */ private IGlobalVariableDescriptor fDescriptor; /** diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CValue.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CValue.java index 18a6ed8fe22..cc64112bb0e 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CValue.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CValue.java @@ -18,8 +18,6 @@ import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; import java.util.List; import org.eclipse.cdt.core.IAddress; @@ -66,7 +64,7 @@ public class CValue extends AbstractCValue { /** * List of child variables. */ - private List fVariables = Collections.EMPTY_LIST; + private List fVariables = new ArrayList(); private CType fType; @@ -125,20 +123,26 @@ public class CValue extends AbstractCValue { * @see org.eclipse.debug.core.model.IValue#getVariables() */ public IVariable[] getVariables() throws DebugException { - List list = getVariables0(); - return (IVariable[])list.toArray( new IVariable[list.size()] ); + List list = getVariables0(); + return list.toArray( new IVariable[list.size()] ); } - protected synchronized List getVariables0() throws DebugException { + protected synchronized List getVariables0() throws DebugException { if ( !isAllocated() || !hasVariables() ) - return Collections.EMPTY_LIST; + return new ArrayList(); if ( fVariables.size() == 0 ) { try { - List vars = getCDIVariables(); - fVariables = new ArrayList( vars.size() ); - Iterator it = vars.iterator(); - while( it.hasNext() ) { - fVariables.add( CVariableFactory.createLocalVariable( this, (ICDIVariable)it.next() ) ); + List vars = getCDIVariables(); + for (ICDIVariable var : vars) { + if (getParentVariable() instanceof CGlobalVariable) { + fVariables.add(CVariableFactory.createGlobalVariable( + this, + null, + var)); + } + else { + fVariables.add(CVariableFactory.createLocalVariable(this, var)); + } } resetStatus(); } @@ -168,7 +172,7 @@ public class CValue extends AbstractCValue { return fCDIValue; } - protected List getCDIVariables() throws DebugException { + protected List getCDIVariables() throws DebugException { ICDIVariable[] vars = null; try { ICDIValue value = getUnderlyingValue(); @@ -198,9 +202,8 @@ public class CValue extends AbstractCValue { fValueString = null; } - Iterator it = fVariables.iterator(); - while( it.hasNext() ) { - ((AbstractCVariable)it.next()).setChanged( changed ); + for (AbstractCVariable var : fVariables) { + var.setChanged( changed ); } } @@ -208,9 +211,8 @@ public class CValue extends AbstractCValue { * @see org.eclipse.cdt.debug.internal.core.model.AbstractCValue#dispose() */ public void dispose() { - Iterator it = fVariables.iterator(); - while( it.hasNext() ) { - ((AbstractCVariable)it.next()).dispose(); + for (AbstractCVariable var : fVariables) { + var.dispose(); } } @@ -621,9 +623,8 @@ public class CValue extends AbstractCValue { protected void reset() { resetStatus(); fValueString = null; - Iterator it = fVariables.iterator(); - while( it.hasNext() ) { - ((AbstractCVariable)it.next()).resetValue(); + for (AbstractCVariable var : fVariables) { + var.resetValue(); } } @@ -654,9 +655,8 @@ public class CValue extends AbstractCValue { protected void preserve() { setChanged( false ); resetStatus(); - Iterator it = fVariables.iterator(); - while( it.hasNext() ) { - ((AbstractCVariable)it.next()).preserve(); + for (AbstractCVariable var : fVariables) { + var.preserve(); } } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CVariableFactory.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CVariableFactory.java index fc59eb021b8..cd9a86866e0 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CVariableFactory.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/model/CVariableFactory.java @@ -46,7 +46,7 @@ public class CVariableFactory { } public String toString() { - return MessageFormat.format( "{0}::{1}", new String[] { getPath().toOSString(), getName() } ); //$NON-NLS-1$ + return MessageFormat.format( "{0}::{1}", (Object[])new String[] { getPath().toOSString(), getName() } ); //$NON-NLS-1$ } public boolean equals( Object obj ) { @@ -71,6 +71,9 @@ public class CVariableFactory { return createGlobalVariableDescriptor( symbol.getName(), symbol.getFilename() ); } + /** + * @param descriptor can be null if creating a child for a global + */ public static CGlobalVariable createGlobalVariable( CDebugElement parent, IGlobalVariableDescriptor descriptor, ICDIVariableDescriptor cdiVariableObject ) { return new CGlobalVariable( parent, descriptor, cdiVariableObject ); }