mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +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:
parent
f4e51c6139
commit
80c2baf464
5 changed files with 60 additions and 49 deletions
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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<ICGlobalVariable> 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<ICGlobalVariable>( 10 );
|
||||
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 ) {
|
||||
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<IGlobalVariableDescriptor> descriptors = new ArrayList<IGlobalVariableDescriptor>();
|
||||
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<IGlobalVariableDescriptor> descrs = new ArrayList<IGlobalVariableDescriptor>();
|
||||
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()]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<AbstractCVariable> fVariables = new ArrayList<AbstractCVariable>();
|
||||
|
||||
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<AbstractCVariable> list = getVariables0();
|
||||
return list.toArray( new IVariable[list.size()] );
|
||||
}
|
||||
|
||||
protected synchronized List getVariables0() throws DebugException {
|
||||
protected synchronized List<AbstractCVariable> getVariables0() throws DebugException {
|
||||
if ( !isAllocated() || !hasVariables() )
|
||||
return Collections.EMPTY_LIST;
|
||||
return new ArrayList<AbstractCVariable>();
|
||||
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<ICDIVariable> 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<ICDIVariable> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue