Skip to main content
summaryrefslogtreecommitdiffstats
blob: c489de218ec0ac24a4337610f6c0c29a32fa9701 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/*
 *(c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 * 
 */
package org.eclipse.cdt.debug.internal.core.model;


import java.text.MessageFormat;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.ICDebugConstants;
import org.eclipse.cdt.debug.core.cdi.CDIException;
import org.eclipse.cdt.debug.core.cdi.ICDIFormat;
import org.eclipse.cdt.debug.core.cdi.event.ICDIChangedEvent;
import org.eclipse.cdt.debug.core.cdi.event.ICDIDestroyedEvent;
import org.eclipse.cdt.debug.core.cdi.event.ICDIEvent;
import org.eclipse.cdt.debug.core.cdi.event.ICDIEventListener;
import org.eclipse.cdt.debug.core.cdi.event.ICDIResumedEvent;
import org.eclipse.cdt.debug.core.cdi.model.ICDIArgumentObject;
import org.eclipse.cdt.debug.core.cdi.model.ICDIObject;
import org.eclipse.cdt.debug.core.cdi.model.ICDIStackFrame;
import org.eclipse.cdt.debug.core.cdi.model.ICDITarget;
import org.eclipse.cdt.debug.core.cdi.model.ICDIValue;
import org.eclipse.cdt.debug.core.cdi.model.ICDIVariable;
import org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject;
import org.eclipse.cdt.debug.core.cdi.model.type.ICDIType;
import org.eclipse.cdt.debug.core.model.ICDebugElementErrorStatus;
import org.eclipse.cdt.debug.core.model.ICType;
import org.eclipse.cdt.debug.core.model.ICValue;
import org.eclipse.cdt.debug.core.model.ICVariable;
import org.eclipse.cdt.debug.core.model.ICastToArray;
import org.eclipse.cdt.debug.core.model.ICastToType;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.core.model.IVariable;

/**
 * 
 * A superclass for all variable classes.
 * 
 * @since Aug 9, 2002
 */
public abstract class CVariable extends CDebugElement 
								implements ICVariable,
										   ICDIEventListener,
										   ICastToType,
										   ICastToArray
{
	/**
	 * The instance of this class is created when the 'getCDIVariable' call throws an exception.
	 * 
	 * @since Jul 22, 2003
	 */
	public static class ErrorVariable implements ICDIVariable
	{
		private ICDIVariableObject fVariableObject;
		private Exception fException;

		public ErrorVariable( ICDIVariableObject varObject, Exception e )
		{
			fVariableObject = varObject;
			fException = e;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#getStackFrame()
		 */
		public ICDIStackFrame getStackFrame() throws CDIException
		{
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject#getName()
		 */
		public String getName()
		{
			return ( fVariableObject != null ) ? fVariableObject.getName() : ""; //$NON-NLS-1$
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#getTypeName()
		 */
		public String getTypeName() throws CDIException
		{
			return ( fVariableObject != null ) ? fVariableObject.getTypeName() : ""; //$NON-NLS-1$
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#getType()
		 */
		public ICDIType getType() throws CDIException
		{
			return ( fVariableObject != null ) ? fVariableObject.getType() : null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#getValue()
		 */
		public ICDIValue getValue() throws CDIException
		{
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#isEditable()
		 */
		public boolean isEditable() throws CDIException
		{
			return false;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#setValue(java.lang.String)
		 */
		public void setValue( String expression ) throws CDIException
		{
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#setValue(org.eclipse.cdt.debug.core.cdi.model.ICDIValue)
		 */
		public void setValue( ICDIValue value ) throws CDIException
		{
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariable#setFormat(int)
		 */
		public void setFormat( int format ) throws CDIException
		{
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIObject#getTarget()
		 */
		public ICDITarget getTarget()
		{
			return ( fVariableObject != null ) ? fVariableObject.getTarget() : null;
		}

		public Exception getException()
		{
			return fException;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject#sizeof()
		 */
		public int sizeof() throws CDIException
		{
			return 0;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject#getQualifiedName()
		 */
		public String getQualifiedName() throws CDIException
		{
			return ( fVariableObject != null ) ? fVariableObject.getQualifiedName() : null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject#equals(org.eclipse.cdt.debug.core.cdi.model.ICDIVariableObject)
		 */
		public boolean equals( ICDIVariableObject varObject )
		{
			return ( fVariableObject != null ) ? fVariableObject.equals( varObject ) : false;
		}
	}

	class InternalVariable
	{
		private ICDIVariableObject fCDIVariableObject;

		private ICDIVariable fCDIVariable;

		private Boolean fEditable = null;

		private ICType fType = null;

		private String fQualifiedName = null;

		public InternalVariable( ICDIVariableObject varObject )
		{
			setCDIVariableObject( varObject );
			setCDIVariable( ( varObject instanceof ICDIVariable ) ? (ICDIVariable)varObject : null );
		}

		protected synchronized ICDIVariable getCDIVariable() throws CDIException
		{
			if ( fCDIVariable == null )
			{
				try
				{
					if ( getCDIVariableObject() instanceof ICDIArgumentObject )
						fCDIVariable = getCDISession().getVariableManager().createArgument( (ICDIArgumentObject)getCDIVariableObject() );
					else
						fCDIVariable = getCDISession().getVariableManager().createVariable( getCDIVariableObject() );
				}
				catch( CDIException e )
				{
					fCDIVariable = new ErrorVariable( getCDIVariableObject(), e );
					setStatus( ICDebugElementErrorStatus.ERROR, MessageFormat.format( CoreModelMessages.getString( "CVariable.0" ), new String[] { e.getMessage() } ) ); //$NON-NLS-1$
				}
			}
			return fCDIVariable;
		}

		protected ICDIVariableObject getCDIVariableObject()
		{
			return fCDIVariableObject;
		}

		protected ICType getType() throws CDIException
		{
			if ( fType == null )
			{
				ICDIVariableObject varObject = getCDIVariableObject();
				if ( varObject != null )
					fType = new CType( varObject.getType() );
			}
			return fType;
		}

		protected boolean isEditable() throws CDIException
		{
			if ( fEditable == null )
			{
				ICDIVariableObject varObject = getCDIVariableObject();
				if ( varObject != null && !(varObject instanceof ErrorVariable) )
					fEditable = new Boolean( varObject.isEditable() );
			}
			return ( fEditable != null ) ? fEditable.booleanValue() : false;
		}

		protected boolean hasChildren() throws CDIException
		{
			CType type = (CType)getType();
			return ( type != null ) ? type.hasChildren() : false;
		}

		private void setCDIVariable( ICDIVariable variable )
		{
			fCDIVariable = variable;
		}

		private void setCDIVariableObject( ICDIVariableObject object )
		{
			fCDIVariableObject = object;
		}

		protected synchronized void invalidate()
		{
			try
			{
				if ( fCDIVariable != null && !(fCDIVariable instanceof ErrorVariable) )
					getCDISession().getVariableManager().destroyVariable( fCDIVariable );
			}
			catch( CDIException e )
			{
				logError( e.getMessage() );
			}
			setCDIVariable( null );
			if ( fType != null )
				fType.dispose();
			fType = null;
			fEditable = null;
		}

		protected void dispose()
		{
			invalidate();
			setCDIVariableObject( null );
		}

		protected boolean isSameVariable( ICDIVariable cdiVar )
		{
			return ( fCDIVariable != null ) ? fCDIVariable.equals( cdiVar ) : false; 
		}

		protected int sizeof()
		{
			if ( getCDIVariableObject() != null )
			{
				try
				{
					return getCDIVariableObject().sizeof();
				}
				catch( CDIException e )
				{
				}
			}
			return 0;
		}

		protected String getQualifiedName() throws CDIException
		{
			if ( fQualifiedName == null )
			{
				fQualifiedName = ( fCDIVariableObject != null ) ? fCDIVariableObject.getQualifiedName() : null;
			}
			return fQualifiedName;
		}

		public boolean hasErrors() {
			return ( fCDIVariable instanceof ErrorVariable );
		}
	}

	/**
	 * The parent object this variable is contained in.
	 */
	private CDebugElement fParent;

	/**
	 * The original internal variable.
	 */
	private InternalVariable fOriginal;
	
	/**
	 * The shadow internal variable used for casting.
	 */
	private InternalVariable fShadow = null;
	
	/**
	 * Cache of current value - see #getValue().
	 */
	protected ICValue fValue;

	/**
	 * The name of this variable.
	 */
	private String fName = null;

	/**
	 * Change flag.
	 */
	protected boolean fChanged = false;

	/**
	 * The current format of this variable.
	 */
	protected int fFormat = ICDIFormat.NATURAL;

	private boolean fIsEnabled = true;

	/*
	 * Temporary solution to avoid NPE in VariablesView. 
	 * This is fixed in the Eclipse 2.1.1 Maintenance Build.
	 */
	static protected IValue fDisabledValue = new IValue()
												{
													public String getReferenceTypeName() throws DebugException
													{
														return null;
													}

													public String getValueString() throws DebugException
													{
														return null;
													}

													public boolean isAllocated() throws DebugException
													{
														return false;
													}

													public IVariable[] getVariables() throws DebugException
													{
														return null;
													}

													public boolean hasVariables() throws DebugException
													{
														return false;
													}

													public String getModelIdentifier()
													{
														return CDebugCorePlugin.getUniqueIdentifier();
													}

													public IDebugTarget getDebugTarget()
													{
														return null;
													}

													public ILaunch getLaunch()
													{
														return null;
													}

													public Object getAdapter( Class adapter )
													{
														return null;
													}
													
												};
	/**
	 * Constructor for CVariable.
	 * @param target
	 */
	public CVariable( CDebugElement parent, ICDIVariableObject cdiVariableObject )
	{
		super( (CDebugTarget)parent.getDebugTarget() );
		fParent = parent;
		fIsEnabled = !enableVariableBookkeeping();
		fOriginal = createOriginal( cdiVariableObject );
		fShadow = null;
		if ( cdiVariableObject instanceof ErrorVariable )
			setStatus( ICDebugElementErrorStatus.ERROR, 
					MessageFormat.format( CoreModelMessages.getString( "CVariable.1" ), new String[] { ((ErrorVariable)cdiVariableObject).getException().getMessage() } ) ); //$NON-NLS-1$
		fFormat = CDebugCorePlugin.getDefault().getPluginPreferences().getInt( ICDebugConstants.PREF_DEFAULT_VARIABLE_FORMAT );
		getCDISession().getEventManager().addEventListener( this );
	}

	private InternalVariable createOriginal( ICDIVariableObject varObject )
	{
		return new InternalVariable( varObject );
	}

	/**
	 * Returns the current value of this variable. The value
	 * is cached, but on each access we see if the value has changed
	 * and update if required.
	 * 
	 * @see org.eclipse.debug.core.model.IVariable#getValue()
	 */
	public IValue getValue() throws DebugException
	{
		if ( !isEnabled() )
			return fDisabledValue;
		if ( fValue == null )
		{
			ICType type = null;
			try
			{
				type = getType();
			}
			catch( DebugException e )
			{
				// ignore and use default type
			}
			if ( type != null && type.isArray() )
			{
				ICDIVariable var = null;
				try
				{
					var = getInternalVariable().getCDIVariable();
				}
				catch( CDIException e )
				{
					requestFailed( "", e ); //$NON-NLS-1$
				}
				int[] dims = type.getArrayDimensions();
				if ( dims.length > 0 && dims[0] > 0 )
					fValue = CValueFactory.createArrayValue( this, var, 0, dims[0] - 1 );
			}
			else
			{
				ICDIValue cdiValue = getCurrentValue();
				if ( cdiValue != null )
					fValue = CValueFactory.createValue( this, cdiValue );
			}
		}
		return fValue;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
	 */
	public boolean hasValueChanged() throws DebugException
	{
		return fChanged;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IValueModification#setValue(String)
	 */
	public void setValue( String expression ) throws DebugException
	{
		notSupported( CoreModelMessages.getString( "CVariable.2" ) ); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IValueModification#setValue(IValue)
	 */
	public void setValue( IValue value ) throws DebugException
	{
		notSupported( CoreModelMessages.getString( "CVariable.3" ) ); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
	 */
	public boolean supportsValueModification()
	{
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(String)
	 */
	public boolean verifyValue( String expression ) throws DebugException
	{
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(IValue)
	 */
	public boolean verifyValue( IValue value ) throws DebugException
	{
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
	 */
	public Object getAdapter( Class adapter )
	{
		if ( adapter.equals( ICastToType.class ) )
			return this;
		if ( adapter.equals( IVariable.class ) )
			return this;
		if ( adapter.equals( ICVariable.class ) )
			return this;
		return super.getAdapter( adapter );
	}

	/**
	 * Returns this variable's current underlying CDI value.
	 * Subclasses must implement #retrieveValue() and do not
	 * need to guard against CDIException, as this method
	 * handles them.
	 *
	 * @exception DebugException if unable to access the value
	 */
	protected final ICDIValue getCurrentValue() throws DebugException
	{
		try
		{
			return retrieveValue();
		}
		catch( CDIException e )
		{
			targetRequestFailed( e.getMessage(), null );
		}
		return null;
	}

	/**
	 * Returns the last known value for this variable
	 */
	protected ICDIValue getLastKnownValue()
	{
		return ( fValue instanceof CValue ) ? ((CValue)fValue).getUnderlyingValue() : null;
	}
	
	public void dispose()
	{
		if ( fValue != null )
		{
			fValue.dispose();
		}
		getCDISession().getEventManager().removeEventListener( this );
		if ( getShadow() != null )
			getShadow().dispose();
	}
	
	protected synchronized void setChanged( boolean changed ) throws DebugException
	{
		if ( getValue() != null && getValue() instanceof CValue )
		{
			fChanged = changed;
			((CValue)getValue()).setChanged( changed );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.cdi.event.ICDIEventListener#handleDebugEvents(ICDIEvent)
	 */
	public void handleDebugEvents( ICDIEvent[] events )
	{
		for (int i = 0; i < events.length; i++)
		{
			ICDIEvent event = events[i];
			ICDIObject source = event.getSource();
			if (source == null)
				continue;

			if ( source.getTarget().equals( getCDITarget() ) )
			{
				if ( event instanceof ICDIChangedEvent )
				{
					if ( source instanceof ICDIVariable && isSameVariable( (ICDIVariable)source ) )
					{
						handleChangedEvent( (ICDIChangedEvent)event );
					}
				}
				else if ( event instanceof ICDIDestroyedEvent )
				{
					if ( source instanceof ICDIVariable && isSameVariable( (ICDIVariable)source ) )
					{
						handleDestroyedEvent( (ICDIDestroyedEvent)event );
					}
				}
				else if ( event instanceof ICDIResumedEvent )
				{
					handleResumedEvent( (ICDIResumedEvent)event );
				}
			}
		}
	}

	private void handleResumedEvent( ICDIResumedEvent event )
	{
		try
		{
			if ( getCDIVariable() instanceof ErrorVariable )
			{
				getInternalVariable().invalidate();
				setStatus( ICDebugElementErrorStatus.OK, null );
			}
		}
		catch( CDIException e )
		{
		}
	}

	private void handleChangedEvent( ICDIChangedEvent event )
	{
		try
		{
			setChanged( true );
			fireChangeEvent( DebugEvent.STATE );
		}
		catch( DebugException e )
		{
			logError( e );
		}
	}

	private void handleDestroyedEvent( ICDIDestroyedEvent event )
	{
		if ( fOriginal != null )
			fOriginal.invalidate();
		if ( getShadow() != null )
			getShadow().invalidate();
		invalidateValue();
	}

	/**
	 * Returns the stack frame this variable is contained in.
	 * 
	 * @return the stack frame
	 */
	protected CDebugElement getParent()
	{
		return fParent;
	}

	/**
	 * Returns the underlying CDI variable.
	 * 
	 * @return the underlying CDI variable
	 */
	protected ICDIVariable getCDIVariable() throws CDIException
	{
		if ( getShadow() != null )
			return getShadow().getCDIVariable();
		return getOriginalCDIVariable();
	}

	/**
	 * Returns this variable's underlying CDI value.
	 */
	protected ICDIValue retrieveValue() throws DebugException, CDIException
	{
		return ( getParent().getDebugTarget().isSuspended() && getCDIVariable() != null ) ? 
											getCDIVariable().getValue() : getLastKnownValue();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IVariable#getName()
	 */
	public String getName() throws DebugException
	{
		if ( fName == null )
		{
			fName = ( fOriginal != null ) ? fOriginal.getCDIVariableObject().getName() : null;
		}
		return fName;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
	 */
	public String getReferenceTypeName() throws DebugException
	{
		return ( getType() != null ) ? getType().getName() : null;
	}

	protected void updateParentVariable( CValue parentValue ) throws DebugException
	{
		parentValue.getParentVariable().setChanged( true );
		parentValue.getParentVariable().fireChangeEvent( DebugEvent.STATE );
	}

	/**
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#getFormat()
	 */
	public int getFormat()
	{
		return fFormat;
	}

	/**
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#setFormat(int)
	 */
	public void setFormat( int format ) throws DebugException
	{
		doSetFormat( format );
		reset();
	}

	protected void doSetFormat( int format )
	{
		fFormat = format;
	}

	protected void reset() throws DebugException
	{
		IValue value = getValue();
		if ( value instanceof CValue )
		{
			((CValue)getValue()).reset();
			fireChangeEvent( DebugEvent.STATE );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToType#cast(java.lang.String)
	 */
	public void cast( String type ) throws DebugException
	{
		try
		{
			InternalVariable newVar = createShadow( getOriginalCDIVariable().getStackFrame(), type );
			if ( getShadow() != null )
				getShadow().dispose();
			setShadow( newVar );
		}
		catch( CDIException e )
		{
			targetRequestFailed( e.getMessage(), null );
		}
		finally
		{
			invalidateValue();
			fireChangeEvent( DebugEvent.STATE );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToType#getCurrentType()
	 */
	public String getCurrentType()
	{
		try
		{
			return getReferenceTypeName();
		}
		catch( DebugException e )
		{
			logError( e );
		}
		return ""; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToType#restoreDefault()
	 */
	public void restoreDefault() throws DebugException
	{
		InternalVariable oldVar = getShadow();
		setShadow( null );
		if ( oldVar != null )
			oldVar.dispose();
		invalidateValue();
		fireChangeEvent( DebugEvent.STATE );
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToType#supportsCasting()
	 */
	public boolean supportsCasting()
	{
		CDebugTarget target = (CDebugTarget)getDebugTarget().getAdapter( CDebugTarget.class );
		return ( target != null && isEditable() );
	}
	
	protected ICDIVariable getOriginalCDIVariable() throws CDIException
	{
		return ( fOriginal != null ) ? fOriginal.getCDIVariable() : null;
	}

	protected InternalVariable getShadow()
	{
		return fShadow;
	}

	private void setShadow( InternalVariable shadow )
	{
		fShadow = shadow;
	}
	
	private InternalVariable createShadow( ICDIStackFrame cdiFrame, String type ) throws DebugException
	{
		try
		{
			return new InternalVariable( getCDISession().getVariableManager().getVariableObjectAsType( getOriginalCDIVariable(), type ) );
		}
		catch( CDIException e )
		{
			targetRequestFailed( e.getMessage(), null );
		}
		return null;
	}
	
	private InternalVariable createShadow( ICDIStackFrame cdiFrame, int start, int length ) throws DebugException
	{
		try
		{
			return new InternalVariable( getCDISession().getVariableManager().getVariableObjectAsArray( getCDIVariable(), start, length ) );
		}
		catch( CDIException e )
		{
			targetRequestFailed( e.getMessage(), null );
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToType#isCasted()
	 */
	public boolean isCasted()
	{
		return ( getShadow() != null );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToArray#castToArray(int, int)
	 */
	public void castToArray( int startIndex, int length ) throws DebugException
	{
		try
		{
			InternalVariable newVar = createShadow( getOriginalCDIVariable().getStackFrame(), startIndex, length );
			if ( getShadow() != null )
				getShadow().dispose();
			setShadow( newVar );
		}
		catch( CDIException e )
		{
			targetRequestFailed( e.getMessage(), null );
		}
		finally
		{
			invalidateValue();
			fireChangeEvent( DebugEvent.STATE );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICastToArray#supportsCastToArray()
	 */
	public boolean supportsCastToArray()
	{
		CDebugTarget target = (CDebugTarget)getDebugTarget().getAdapter( CDebugTarget.class );
		return ( target != null && isEditable() && hasChildren() );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#hasChildren()
	 */
	public boolean hasChildren()
	{
		if ( !isEnabled() )
			return false;
		boolean result = false;
		try
		{
			InternalVariable var = getInternalVariable();
			if ( var != null )
				result = var.hasChildren();
		}
		catch( CDIException e )
		{
			logError( e );
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#isEditable()
	 */
	public boolean isEditable()
	{
		if ( !isEnabled() )
			return false;
		boolean result = false;
		try
		{
			InternalVariable var = getInternalVariable();
			if ( var != null )
				result = var.isEditable();
		}
		catch( CDIException e )
		{
			logError( e );
		}
		return result;
	}

	protected String getQualifiedName() throws DebugException
	{
		String result = null;
		try
		{
			result = getInternalVariable().getQualifiedName();
		}
		catch( CDIException e )
		{
			requestFailed( CoreModelMessages.getString( "CVariable.4" ), e ); //$NON-NLS-1$
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#getType()
	 */
	public ICType getType() throws DebugException
	{
		ICType type = null;
		if ( isEnabled() )
		{
			try
			{
				InternalVariable iv = getInternalVariable();
				if ( iv != null )
					type = iv.getType();
			}
			catch( CDIException e )
			{
				requestFailed( CoreModelMessages.getString( "CVariable.5" ), e ); //$NON-NLS-1$
			}
		}
		return type;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#isEnabled()
	 */
	public boolean isEnabled()
	{
		return ( canEnableDisable() ) ? fIsEnabled : true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#setEnabled(boolean)
	 */
	public void setEnabled( boolean enabled ) throws DebugException
	{
		setEnabled0( enabled );
		fireChangeEvent( DebugEvent.STATE );
	}

	private synchronized void setEnabled0( boolean enabled )
	{
		if ( fOriginal != null )
			fOriginal.invalidate();
		if ( getShadow() != null )
			getShadow().invalidate();
		fIsEnabled = enabled;
		invalidateValue();
	}

	private void invalidateValue()
	{
		if ( fValue != null )
		{
			fValue.dispose();
			fValue = null;
		}
	}

	public boolean isArgument()
	{
		return ( fOriginal != null ) ? ( fOriginal.getCDIVariableObject() instanceof ICDIArgumentObject ) : false;
	}

	protected boolean sameVariableObject( ICDIVariableObject object )
	{
		return ( object != null && fOriginal != null ) ? ( object.equals( fOriginal.getCDIVariableObject() ) ) : false;
	}

	private boolean enableVariableBookkeeping()
	{
		boolean result = false;
		try
		{
			result = getLaunch().getLaunchConfiguration().getAttribute( ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_ENABLE_VARIABLE_BOOKKEEPING, false );
		}
		catch( CoreException e )
		{
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#canEnableDisable()
	 */
	public boolean canEnableDisable()
	{
		return ( getParent() instanceof CStackFrame );
	}

	protected boolean isSameVariable( ICDIVariable cdiVar )
	{
		return ( ( getShadow() != null && getShadow().isSameVariable( cdiVar ) ) ||
				 ( fOriginal != null && fOriginal.isSameVariable( cdiVar ) ) );
	}

	private InternalVariable getInternalVariable()
	{
		return ( getShadow() != null ) ? getShadow() : fOriginal;
	}

	protected int sizeof()
	{
		return getInternalVariable().sizeof();
	}

	public boolean hasErrors() {
		return ( getShadow() != null ) ? getShadow().hasErrors() : fOriginal.hasErrors();
	}
}

Back to the top