1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 15:05:36 +02:00

Fixed 240916 (casting a child of a global results in an NPE). Also fixed all warnings in changed files and improved code with generics

This commit is contained in:
John Cortell 2008-07-15 19:26:53 +00:00
parent f4e51c6139
commit 80c2baf464
5 changed files with 60 additions and 49 deletions

View file

@ -16,7 +16,7 @@ package org.eclipse.cdt.debug.core.model;
public interface ICGlobalVariable extends ICVariable { 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 * @return the descriptor of this variable
*/ */

View file

@ -14,12 +14,13 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.CDebugUtils; import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
@ -61,7 +62,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
private IGlobalVariableDescriptor[] fInitialDescriptors = new IGlobalVariableDescriptor[0]; private IGlobalVariableDescriptor[] fInitialDescriptors = new IGlobalVariableDescriptor[0];
private ArrayList fGlobals; private List<ICGlobalVariable> fGlobals;
/** /**
* Constructor for CGlobalVariableManager. * Constructor for CGlobalVariableManager.
@ -89,16 +90,16 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
DebugPlugin.log( e ); DebugPlugin.log( e );
} }
} }
return (ICGlobalVariable[])fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); return fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] );
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#addGlobals(IGlobalVariableDescriptor[]) * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#addGlobals(IGlobalVariableDescriptor[])
*/ */
public void addGlobals( IGlobalVariableDescriptor[] descriptors ) throws DebugException { public void addGlobals( IGlobalVariableDescriptor[] descriptors ) throws DebugException {
fGlobals = new ArrayList( 10 ); fGlobals = new ArrayList<ICGlobalVariable>( 10 );
MultiStatus ms = new MultiStatus( CDebugCorePlugin.getUniqueIdentifier(), 0, "", null ); //$NON-NLS-1$ MultiStatus ms = new MultiStatus( CDebugCorePlugin.getUniqueIdentifier(), 0, "", null ); //$NON-NLS-1$
ArrayList globals = new ArrayList( descriptors.length ); List<ICGlobalVariable> globals = new ArrayList<ICGlobalVariable>( descriptors.length );
for ( int i = 0; i < descriptors.length; ++i ) { for ( int i = 0; i < descriptors.length; ++i ) {
try { try {
globals.add( getDebugTarget().createGlobalVariable( descriptors[i] ) ); globals.add( getDebugTarget().createGlobalVariable( descriptors[i] ) );
@ -142,7 +143,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
ICGlobalVariable[] globals = new ICGlobalVariable[0]; ICGlobalVariable[] globals = new ICGlobalVariable[0];
synchronized( fGlobals ) { synchronized( fGlobals ) {
globals = (ICGlobalVariable[])fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] ); globals = fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] );
fGlobals.clear(); fGlobals.clear();
} }
for ( int i = 0; i < globals.length; ++i ) { for ( int i = 0; i < globals.length; ++i ) {
@ -154,9 +155,8 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
public void dispose() { public void dispose() {
if ( fGlobals != null ) { if ( fGlobals != null ) {
Iterator it = fGlobals.iterator(); for (ICGlobalVariable global : fGlobals) {
while( it.hasNext() ) { ((CVariable)global).dispose();
((CVariable)it.next()).dispose();
} }
fGlobals.clear(); fGlobals.clear();
fGlobals = null; fGlobals = null;
@ -170,13 +170,16 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
Element node = document.createElement( GLOBAL_VARIABLE_LIST ); Element node = document.createElement( GLOBAL_VARIABLE_LIST );
document.appendChild( node ); document.appendChild( node );
ICGlobalVariable[] globals = getGlobals(); ICGlobalVariable[] globals = getGlobals();
for ( int i = 0; i < globals.length; ++i ) { for (ICGlobalVariable global : globals) {
IGlobalVariableDescriptor descriptor = globals[i].getDescriptor(); 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 ); Element child = document.createElement( GLOBAL_VARIABLE );
child.setAttribute( ATTR_GLOBAL_VARIABLE_NAME, descriptor.getName() ); child.setAttribute( ATTR_GLOBAL_VARIABLE_NAME, descriptor.getName() );
child.setAttribute( ATTR_GLOBAL_VARIABLE_PATH, descriptor.getPath().toOSString() ); child.setAttribute( ATTR_GLOBAL_VARIABLE_PATH, descriptor.getPath().toOSString() );
node.appendChild( child ); node.appendChild( child );
} }
}
return CDebugUtils.serializeDocument( document ); return CDebugUtils.serializeDocument( document );
} }
catch( ParserConfigurationException e ) { catch( ParserConfigurationException e ) {
@ -200,7 +203,7 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
InputSource source = new InputSource( reader ); InputSource source = new InputSource( reader );
root = parser.parse( source ).getDocumentElement(); root = parser.parse( source ).getDocumentElement();
if ( root.getNodeName().equalsIgnoreCase( GLOBAL_VARIABLE_LIST ) ) { if ( root.getNodeName().equalsIgnoreCase( GLOBAL_VARIABLE_LIST ) ) {
List descriptors = new ArrayList(); List<IGlobalVariableDescriptor> descriptors = new ArrayList<IGlobalVariableDescriptor>();
NodeList list = root.getChildNodes(); NodeList list = root.getChildNodes();
int length = list.getLength(); int length = list.getLength();
for( int i = 0; i < length; ++i ) { 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; return;
} }
} }
@ -276,11 +279,13 @@ public class CGlobalVariableManager implements ICGlobalVariableManager {
public IGlobalVariableDescriptor[] getDescriptors() { public IGlobalVariableDescriptor[] getDescriptors() {
if ( fGlobals == null ) if ( fGlobals == null )
return getInitialDescriptors(); return getInitialDescriptors();
IGlobalVariableDescriptor[] result = new IGlobalVariableDescriptor[fGlobals.size()]; List<IGlobalVariableDescriptor> descrs = new ArrayList<IGlobalVariableDescriptor>();
Iterator it = fGlobals.iterator(); for (ICGlobalVariable global : fGlobals) {
for ( int i = 0; it.hasNext(); ++i ) { IGlobalVariableDescriptor descr = global.getDescriptor();
result[i] = ((ICGlobalVariable)it.next()).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()]);
} }
} }

View file

@ -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; private IGlobalVariableDescriptor fDescriptor;
/** /**

View file

@ -18,8 +18,6 @@ import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException; import java.nio.charset.CharacterCodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.IAddress; import org.eclipse.cdt.core.IAddress;
@ -66,7 +64,7 @@ public class CValue extends AbstractCValue {
/** /**
* List of child variables. * List of child variables.
*/ */
private List fVariables = Collections.EMPTY_LIST; private List<AbstractCVariable> fVariables = new ArrayList<AbstractCVariable>();
private CType fType; private CType fType;
@ -125,20 +123,26 @@ public class CValue extends AbstractCValue {
* @see org.eclipse.debug.core.model.IValue#getVariables() * @see org.eclipse.debug.core.model.IValue#getVariables()
*/ */
public IVariable[] getVariables() throws DebugException { public IVariable[] getVariables() throws DebugException {
List list = getVariables0(); List<AbstractCVariable> list = getVariables0();
return (IVariable[])list.toArray( new IVariable[list.size()] ); return list.toArray( new IVariable[list.size()] );
} }
protected synchronized List getVariables0() throws DebugException { protected synchronized List<AbstractCVariable> getVariables0() throws DebugException {
if ( !isAllocated() || !hasVariables() ) if ( !isAllocated() || !hasVariables() )
return Collections.EMPTY_LIST; return new ArrayList<AbstractCVariable>();
if ( fVariables.size() == 0 ) { if ( fVariables.size() == 0 ) {
try { try {
List vars = getCDIVariables(); List<ICDIVariable> vars = getCDIVariables();
fVariables = new ArrayList( vars.size() ); for (ICDIVariable var : vars) {
Iterator it = vars.iterator(); if (getParentVariable() instanceof CGlobalVariable) {
while( it.hasNext() ) { fVariables.add(CVariableFactory.createGlobalVariable(
fVariables.add( CVariableFactory.createLocalVariable( this, (ICDIVariable)it.next() ) ); this,
null,
var));
}
else {
fVariables.add(CVariableFactory.createLocalVariable(this, var));
}
} }
resetStatus(); resetStatus();
} }
@ -168,7 +172,7 @@ public class CValue extends AbstractCValue {
return fCDIValue; return fCDIValue;
} }
protected List getCDIVariables() throws DebugException { protected List<ICDIVariable> getCDIVariables() throws DebugException {
ICDIVariable[] vars = null; ICDIVariable[] vars = null;
try { try {
ICDIValue value = getUnderlyingValue(); ICDIValue value = getUnderlyingValue();
@ -198,9 +202,8 @@ public class CValue extends AbstractCValue {
fValueString = null; fValueString = null;
} }
Iterator it = fVariables.iterator(); for (AbstractCVariable var : fVariables) {
while( it.hasNext() ) { var.setChanged( changed );
((AbstractCVariable)it.next()).setChanged( changed );
} }
} }
@ -208,9 +211,8 @@ public class CValue extends AbstractCValue {
* @see org.eclipse.cdt.debug.internal.core.model.AbstractCValue#dispose() * @see org.eclipse.cdt.debug.internal.core.model.AbstractCValue#dispose()
*/ */
public void dispose() { public void dispose() {
Iterator it = fVariables.iterator(); for (AbstractCVariable var : fVariables) {
while( it.hasNext() ) { var.dispose();
((AbstractCVariable)it.next()).dispose();
} }
} }
@ -621,9 +623,8 @@ public class CValue extends AbstractCValue {
protected void reset() { protected void reset() {
resetStatus(); resetStatus();
fValueString = null; fValueString = null;
Iterator it = fVariables.iterator(); for (AbstractCVariable var : fVariables) {
while( it.hasNext() ) { var.resetValue();
((AbstractCVariable)it.next()).resetValue();
} }
} }
@ -654,9 +655,8 @@ public class CValue extends AbstractCValue {
protected void preserve() { protected void preserve() {
setChanged( false ); setChanged( false );
resetStatus(); resetStatus();
Iterator it = fVariables.iterator(); for (AbstractCVariable var : fVariables) {
while( it.hasNext() ) { var.preserve();
((AbstractCVariable)it.next()).preserve();
} }
} }

View file

@ -46,7 +46,7 @@ public class CVariableFactory {
} }
public String toString() { 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 ) { public boolean equals( Object obj ) {
@ -71,6 +71,9 @@ public class CVariableFactory {
return createGlobalVariableDescriptor( symbol.getName(), symbol.getFilename() ); 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 ) { public static CGlobalVariable createGlobalVariable( CDebugElement parent, IGlobalVariableDescriptor descriptor, ICDIVariableDescriptor cdiVariableObject ) {
return new CGlobalVariable( parent, descriptor, cdiVariableObject ); return new CGlobalVariable( parent, descriptor, cdiVariableObject );
} }