Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3aae15330ea56479c285855c429738370caf88db (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.jsdt.internal.corext.template.java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.jsdt.core.CompletionProposal;
import org.eclipse.wst.jsdt.core.CompletionRequestor;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.IJavaScriptUnit;
import org.eclipse.wst.jsdt.core.IType;
import org.eclipse.wst.jsdt.core.ITypeHierarchy;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.core.Signature;
import org.eclipse.wst.jsdt.core.compiler.IProblem;
import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin;

/**
 * A completion requester to collect informations on local variables.
 * This class is used for guessing variable names like arrays, collections, etc.
 */
final class CompilationUnitCompletion extends CompletionRequestor {

	/**
	 * Describes a local variable (including parameters) inside the method where
	 * code completion was invoked. Special predicates exist to query whether
	 * a variable can be iterated over. 
	 */
	public final class Variable {
		private static final int UNKNOWN= 0, NONE= 0;
		private static final int ARRAY= 1;
		private static final int COLLECTION= 2;
		private static final int ITERABLE= 4;
		
		/**
		 * The name of the local variable.
		 */
		private final String name;
		
		/**
		 * The signature of the local variable's type.
		 */
		private final String signature;
		
		/* lazily computed properties */
		private int fType= UNKNOWN;
		private int fChecked= NONE;
		private String[] fMemberTypes;
		
		private Variable(String name, String signature) {
			this.name= name;
			this.signature= signature;
		}

		/**
		 * Returns the name of the variable.
		 * 
		 * @return the name of the variable
		 */
		public String getName() {
			return name;
		}
		
		/**
		 * Returns <code>true</code> if the type of the local variable is an
		 * array type.
		 * 
		 * @return <code>true</code> if the receiver's type is an array,
		 *         <code>false</code> if not
		 */
		public boolean isArray() {
			
			try
			{
				if (fType == UNKNOWN && (fChecked & ARRAY) == 0 && Signature.getTypeSignatureKind(signature) == Signature.ARRAY_TYPE_SIGNATURE)
					fType= ARRAY;
			} catch(IllegalArgumentException iae) {/* Ignore bad signature: assume not array */}
			fChecked |= ARRAY;
			return fType == ARRAY;
		}

		/**
		 * Returns <code>true</code> if the receiver's type is a subclass of
		 * <code>java.util.Collection</code>, <code>false</code> otherwise.
		 * 
		 * @return <code>true</code> if the receiver's type is a subclass of
		 *         <code>java.util.Collection</code>, <code>false</code>
		 *         otherwise
		 */
		public boolean isCollection() {
			// Collection extends Iterable
			if ((fType == UNKNOWN || fType == ITERABLE) && (fChecked & COLLECTION) == 0 && isSubtypeOf("java.util.Collection")) //$NON-NLS-1$
				fType= COLLECTION;
			fChecked |= COLLECTION;
			return fType == COLLECTION;
		}
		
		/**
		 * Returns <code>true</code> if the receiver's type is a subclass of
		 * <code>java.lang.Iterable</code>, <code>false</code> otherwise.
		 * 
		 * @return <code>true</code> if the receiver's type is a subclass of
		 *         <code>java.lang.Iterable</code>, <code>false</code>
		 *         otherwise
		 */
		public boolean isIterable() {
			if (fType == UNKNOWN && (fChecked & ITERABLE) == 0 && isSubtypeOf("java.lang.Iterable")) //$NON-NLS-1$
				fType= ITERABLE;
			fChecked |= ITERABLE;
			return fType == ITERABLE || fType == COLLECTION; // Collection extends Iterable
		}

		/**
		 * Returns <code>true</code> if the receiver's type is an implementor
		 * of <code>interfaceName</code>.
		 * 
		 * @param supertype the fully qualified name of the interface
		 * @return <code>true</code> if the receiver's type implements the
		 *         type named <code>interfaceName</code>
		 */
		private boolean isSubtypeOf(String supertype) {
			String implementorName= SignatureUtil.stripSignatureToFQN(signature);
			if (implementorName.length() == 0)
				return false;
			
			boolean qualified= supertype.indexOf('.') != -1;
			
			// try cheap test first
			if (implementorName.equals(supertype) || !qualified && Signature.getSimpleName(implementorName).equals(supertype))
				return true;

			if (fUnit == null)
				return false;

			IJavaScriptProject project= fUnit.getJavaScriptProject();

			try {
				IType sub= project.findType(implementorName);
				if (sub == null)
					return false;
				
				if (qualified) {
					IType sup= project.findType(supertype);
					if (sup == null)
						return false;
					ITypeHierarchy hierarchy= sub.newSupertypeHierarchy(null);
					return hierarchy.contains(sup);
				} else {
					ITypeHierarchy hierarchy= sub.newSupertypeHierarchy(null);
					IType[] allTypes= hierarchy.getAllClasses();
					for (int i= 0; i < allTypes.length; i++) {
						IType type= allTypes[i];
						if (type.getElementName().equals(supertype))
							return true;
					}
				}

			} catch (JavaScriptModelException e) {
				// ignore and return false
			}			
			
			return false;
		}
		
		private IType[] getSupertypes(String supertype) {
			IType[] empty= new IType[0];
			String implementorName= SignatureUtil.stripSignatureToFQN(signature);
			if (implementorName.length() == 0)
				return empty;
			
			boolean qualified= supertype.indexOf('.') != -1;

			if (fUnit == null)
				return empty;
			
			IJavaScriptProject project= fUnit.getJavaScriptProject();
			
			try {
				IType sub= project.findType(implementorName);
				if (sub == null)
					return empty;
				
				if (qualified) {
					IType sup= project.findType(supertype);
					if (sup == null)
						return empty;
					return new IType[] {sup};
				} else {
					ITypeHierarchy hierarchy= sub.newSupertypeHierarchy(null);
					IType[] allTypes= hierarchy.getAllClasses();
					List matches= new ArrayList();
					for (int i= 0; i < allTypes.length; i++) {
						IType type= allTypes[i];
						if (type.getElementName().equals(supertype))
							matches.add(type);
					}
					return (IType[]) matches.toArray(new IType[matches.size()]);
				}
				
			} catch (JavaScriptModelException e) {
				// ignore and return false
			}			
			
			return empty;
		}

		/**
		 * Returns the signature of the member type.
		 * 
		 * @return the signature of the member type
		 */
		public String getMemberTypeSignature() {
			return getMemberTypeSignatures()[0];
		}
		
		/**
		 * Returns the signatures of all member type bounds.
		 * 
		 * @return the signatures of all member type bounds
		 */
		public String[] getMemberTypeSignatures() {
			if (isArray()) {
				return new String[] {Signature.createArraySignature(Signature.getElementType(signature), Signature.getArrayCount(signature) - 1)};
			} else if (fUnit != null && (isIterable() || isCollection())) {
				if (fMemberTypes == null) {
					try {
						try {
							TypeParameterResolver util= new TypeParameterResolver(this);
							fMemberTypes= util.computeBinding("java.lang.Iterable", 0); //$NON-NLS-1$
						} catch (JavaScriptModelException e) {
							try {
								TypeParameterResolver util= new TypeParameterResolver(this);
								fMemberTypes= util.computeBinding("java.util.Collection", 0); //$NON-NLS-1$
							} catch (JavaScriptModelException x) {
								fMemberTypes= new String[0];
							}
						}
					} catch (IndexOutOfBoundsException e) {
						fMemberTypes= new String[0];
					}
				}
				if (fMemberTypes.length > 0)
					return fMemberTypes;
			}
			return new String[] {Signature.createTypeSignature("java.lang.Object", true)}; //$NON-NLS-1$
		}
		
		/**
		 * Returns the type names of all member type bounds, as they would be 
		 * appear when referenced in the current compilation unit.
		 * 
		 * @return type names of all member type bounds
		 */
		public String[] getMemberTypeNames() {
			String[] signatures= getMemberTypeSignatures();
			String[] names= new String[signatures.length];
			
			for (int i= 0; i < signatures.length; i++) {
				String sig= signatures[i];
				String local= (String) fLocalTypes.get(Signature.getElementType(sig));
				int dim= Signature.getArrayCount(sig);
				if (local != null && dim > 0) {
					StringBuffer array= new StringBuffer(local);
					for (int j= 0; j < dim; j++)
						array.append("[]"); //$NON-NLS-1$
					local= array.toString();
				}
				if (local != null)
					names[i]= local;
				else
					names[i]= Signature.getSimpleName(Signature.getSignatureSimpleName(sig));
			}
			return names;
		}

		/**
		 * Returns the type arguments of the declared type of the variable. Returns
		 * an empty array if it is not a parameterized type.
		 * 
		 * @param type the fully qualified type name of which to match a type argument  
		 * @param index the index of the type parameter in the type
		 * @return the type bounds for the specified type argument in this local variable
		 * 
		 */
		public String[] getTypeArgumentBoundSignatures(String type, int index) {
			List all= new ArrayList();
			IType[] supertypes= getSupertypes(type);
			if (fUnit != null) {
				for (int i= 0; i < supertypes.length; i++) {
					try {
						TypeParameterResolver util= new TypeParameterResolver(this);
						String[] result= util.computeBinding(supertypes[i].getFullyQualifiedName(), index);
						all.addAll(Arrays.asList(result));
					} catch (JavaScriptModelException e) {
					} catch (IndexOutOfBoundsException e) {
					}
				}
			}
			if (all.isEmpty())
				return new String[] {Signature.createTypeSignature("java.lang.Object", true)}; //$NON-NLS-1$
			return (String[]) all.toArray(new String[all.size()]);
		}

		/*
		 * @see java.lang.Object#toString()
		 */
		public String toString() {
			String type;
			switch (fType) {
				case ITERABLE:
					type= "ITERABLE"; //$NON-NLS-1$
					break;
				case COLLECTION:
					type= "COLLECTION"; //$NON-NLS-1$
					break;
				case ARRAY:
					type= "ARRAY"; //$NON-NLS-1$
					break;
				default:
					type= "UNKNOWN"; //$NON-NLS-1$
					break;
			}
			return "LocalVariable [name=\"" + name + "\" signature=\"" + signature + "\" type=\"" + type + "\" member=\"" + getMemberTypeSignature() + "\"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
		}
	}
	
	/**
	 * Given a java type, a resolver computes the bounds of type variables
	 * declared in a super type, considering any type constraints along the
	 * inheritance path.
	 */
	private final class TypeParameterResolver {
		private static final String OBJECT_SIGNATURE= "Ljava.lang.Object;"; //$NON-NLS-1$
		
		private final ITypeHierarchy fHierarchy;
		private final Variable fVariable;
		private final IType fType;
		private final List fBounds= new ArrayList();

		/**
		 * Creates a new type parameter resolver to compute the bindings of type
		 * parameters for the declared type of <code>variable</code>. For any
		 * super type of the type of <code>variable</code>, calling
		 * {@link #computeBinding(IType, int) computeBinding} will find the type
		 * bounds of type variables in the super type, considering any type
		 * constraints along the inheritance path.
		 * 
		 * @param variable the local variable under investigation
		 * @throws JavaScriptModelException if the type of <code>variable</code>
		 *         cannot be found
		 */
		public TypeParameterResolver(Variable variable) throws JavaScriptModelException {
			String typeName= SignatureUtil.stripSignatureToFQN(variable.signature);
			IJavaScriptProject project= fUnit.getJavaScriptProject();
			fType= project.findType(typeName);
			fHierarchy= fType.newSupertypeHierarchy(null);
			fVariable= variable;
		}
		
		/**
		 * Given a type parameter of <code>superType</code> at position
		 * <code>index</code>, this method computes and returns the (lower)
		 * type bound(s) of that parameter for an instance of <code>fType</code>.
		 * <p>
		 * <code>superType</code> must be a super type of <code>fType</code>,
		 * and <code>superType</code> must have at least
		 * <code>index + 1</code> type parameters.
		 * </p>
		 * 
		 * @param superType the qualified type name of the super type to compute
		 *        the type parameter binding for
		 * @param index the index into the list of type parameters of
		 *        <code>superType</code>
		 * @throws JavaScriptModelException if any java model operation fails
		 * @throws IndexOutOfBoundsException if the index is not valid
		 */
		public String[] computeBinding(String superType, int index) throws JavaScriptModelException, IndexOutOfBoundsException {
			IJavaScriptProject project= fUnit.getJavaScriptProject();
			IType type= project.findType(superType);
			if (type == null)
				throw new JavaScriptModelException(new CoreException(new Status(IStatus.ERROR, JavaScriptPlugin.getPluginId(), IStatus.OK, "No such type", null))); //$NON-NLS-1$
			return computeBinding(type, index);
		}

		/**
		 * Given a type parameter of <code>superType</code> at position
		 * <code>index</code>, this method computes and returns the (lower)
		 * type bound(s) of that parameter for an instance of <code>fType</code>.
		 * <p>
		 * <code>superType</code> must be a super type of <code>fType</code>,
		 * and <code>superType</code> must have at least
		 * <code>index + 1</code> type parameters.
		 * </p>
		 * 
		 * @param superType the super type to compute the type parameter binding
		 *        for
		 * @param index the index into the list of type parameters of
		 *        <code>superType</code>
		 * @throws JavaScriptModelException if any java model operation fails
		 * @throws IndexOutOfBoundsException if the index is not valid
		 */
		public String[] computeBinding(IType superType, int index) throws JavaScriptModelException, IndexOutOfBoundsException {
			initBounds();
			return (String[]) fBounds.toArray(new String[fBounds.size()]);
		}
		
		/**
		 * Clears the collected type bounds and initializes it with
		 * <code>java.lang.Object</code>.
		 */
		private void initBounds() {
			fBounds.clear();
			fBounds.add(OBJECT_SIGNATURE); 
		}

		/**
		 * Returns <code>true</code> if <code>subTypeSignature</code>
		 * describes a type which is a true sub type of the type described by
		 * <code>superTypeSignature</code>.
		 * 
		 * @param subTypeSignature the potential subtype's signature
		 * @param superTypeSignature the potential supertype's signature
		 * @return <code>true</code> if the inheritance relationship holds
		 */
		private boolean isTrueSubtypeOf(String subTypeSignature, String superTypeSignature) {
			// try cheap test first
			if (subTypeSignature.equals(superTypeSignature))
				return true;
			
			if (SignatureUtil.isJavaLangObject(subTypeSignature))
				return false; // Object has no super types
			
			if (Signature.getTypeSignatureKind(subTypeSignature) != Signature.BASE_TYPE_SIGNATURE && SignatureUtil.isJavaLangObject(superTypeSignature)) 
				return true;
			
			IJavaScriptProject project= fUnit.getJavaScriptProject();
			
			try {
				
				if ((Signature.getTypeSignatureKind(subTypeSignature) & Signature.CLASS_TYPE_SIGNATURE) == 0)
					return false;
				IType subType= project.findType(SignatureUtil.stripSignatureToFQN(subTypeSignature));
				if (subType == null)
					return false;
				
				if ((Signature.getTypeSignatureKind(superTypeSignature) & Signature.CLASS_TYPE_SIGNATURE) == 0)
					return false;
				IType superType= project.findType(SignatureUtil.stripSignatureToFQN(superTypeSignature));
				if (superType == null)
					return false;
				
				ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(null);
				IType[] types= hierarchy.getAllSuperclasses(subType);
				
				for (int i= 0; i < types.length; i++)
					if (types[i].equals(superType))
						return true;
			} catch (JavaScriptModelException e) {
				// ignore and return false
			}			
			
			return false;
		}
		
		/**
		 * Returns <code>true</code> if <code>signature</code> is a concrete type signature,
		 * <code>false</code> if it is a type variable.
		 * 
		 * @param signature the signature to check
		 * @param context the context inside which to resolve the type
		 * @throws JavaScriptModelException if finding the type fails
		 */
		private boolean isConcreteType(String signature, IType context) throws JavaScriptModelException {
			// try and resolve otherwise
			if (context.isBinary()) {
				return fUnit.getJavaScriptProject().findType(SignatureUtil.stripSignatureToFQN(signature)) != null;
			} else {
				return context.resolveType(SignatureUtil.stripSignatureToFQN(signature)) != null;
			}
		}
	}
	
	private IJavaScriptUnit fUnit;

	private List fLocalVariables= new ArrayList();
	private List fFields= new ArrayList();
	private Map fLocalTypes= new HashMap();

	private boolean fError;

	/**
	 * Creates a compilation unit completion.
	 * 
	 * @param unit the compilation unit, may be <code>null</code>.
	 */
	CompilationUnitCompletion(IJavaScriptUnit unit) {
		reset(unit);
		setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
		setIgnored(CompletionProposal.KEYWORD, true);
		setIgnored(CompletionProposal.LABEL_REF, true);
		setIgnored(CompletionProposal.METHOD_DECLARATION, true);
		setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
		setIgnored(CompletionProposal.METHOD_REF, true);
		setIgnored(CompletionProposal.PACKAGE_REF, true);
		setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
		setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
		setIgnored(CompletionProposal.TYPE_REF, true);
	}
	
	/**
	 * Resets the completion requester.
	 * 
	 * @param unit the compilation unit, may be <code>null</code>.
	 */
	private void reset(IJavaScriptUnit unit) {
		fUnit= unit;
		fLocalVariables.clear();
		fFields.clear();
		fLocalTypes.clear();
		
		if (fUnit != null) {
			try {
				IType[] cuTypes= fUnit.getAllTypes();
				for (int i= 0; i < cuTypes.length; i++) {
					String fqn= cuTypes[i].getFullyQualifiedName();
					String sig= Signature.createTypeSignature(fqn, true);
					fLocalTypes.put(sig, cuTypes[i].getElementName());
				}
			} catch (JavaScriptModelException e) {
				// ignore
			}
		}
		fError= false;
	}

	/*
	 * @see org.eclipse.wst.jsdt.core.CompletionRequestor#accept(org.eclipse.wst.jsdt.core.CompletionProposal)
	 */
	public void accept(CompletionProposal proposal) {
		
		String name= String.valueOf(proposal.getCompletion());
		String signature = ( proposal.getSignature() != null ? String.valueOf( proposal.getSignature() ) : "" ); 
		
		switch (proposal.getKind()) {
			
			case CompletionProposal.LOCAL_VARIABLE_REF:
				// collect local variables
				fLocalVariables.add(new Variable(name, signature));
				break;
			case CompletionProposal.FIELD_REF:
				// collect local variables
				fFields.add(new Variable(name, signature));
				break;
				
			default:
				break;
		}
	}
	
	/*
	 * @see org.eclipse.wst.jsdt.core.CompletionRequestor#completionFailure(org.eclipse.wst.jsdt.core.compiler.IProblem)
	 */
	public void completionFailure(IProblem problem) {
		fError= true;
	}

	/**
	 * Tests if the code completion process produced errors.
	 * 
	 * @return <code>true</code> if there are errors, <code>false</code>
	 *         otherwise
	 */
	public boolean hasErrors() {
		return fError;
	}

	/**
	 * Returns all local variable names.
	 * 
	 * @return all local variable names
	 */
	public String[] getLocalVariableNames() {
		String[] names= new String[fLocalVariables.size()];
		int i= 0;
		for (ListIterator iterator= fLocalVariables.listIterator(fLocalVariables.size()); iterator.hasPrevious();) {
			Variable localVariable= (Variable) iterator.previous();
			names[i++]= localVariable.getName();
		}		
		return names;
	}	
	
	/**
	 * Returns all field names.
	 * 
	 * @return all field names
	 * 
	 */
	public String[] getFieldNames() {
		String[] names= new String[fFields.size()];
		int i= 0;
		for (ListIterator iterator= fFields.listIterator(fFields.size()); iterator.hasPrevious();) {
			Variable field= (Variable)iterator.previous();
			names[i++]= field.getName();
		}		
		return names;
	}	

	/**
	 * Returns all local arrays in the order that they appear.
	 * 
	 * @return all local arrays
	 */
	public Variable[] findLocalArrays() {
		List arrays= new ArrayList();

		for (ListIterator iterator= fLocalVariables.listIterator(fLocalVariables.size()); iterator.hasPrevious();) {
			Variable localVariable= (Variable) iterator.previous();

			if (localVariable.isArray())
				arrays.add(localVariable);
		}

		return (Variable[]) arrays.toArray(new Variable[arrays.size()]);
	}
	
	/**
	 * Returns all local variables implementing or extending
	 * <code>clazz</code> in the order that they appear.
	 * 
	 * @param clazz the fully qualified type name of the class to match
	 * @return all local variables matching <code>clazz</code>
	 */
	public Variable[] findLocalVariables(String clazz) {
		List matches= new ArrayList();
		
		for (ListIterator iterator= fLocalVariables.listIterator(fLocalVariables.size()); iterator.hasPrevious();) {
			Variable localVariable= (Variable) iterator.previous();
			
			if (localVariable.isSubtypeOf(clazz))
				matches.add(localVariable);
		}
		
		return (Variable[]) matches.toArray(new Variable[matches.size()]);
	}
	
	/**
	 * Returns all local variables implementing or extending
	 * <code>clazz</code> in the order that they appear.
	 * 
	 * @param clazz the fully qualified type name of the class to match
	 * @return all local variables matching <code>clazz</code>
	 */
	public Variable[] findFieldVariables(String clazz) {
		List matches= new ArrayList();
		
		for (ListIterator iterator= fFields.listIterator(fFields.size()); iterator.hasPrevious();) {
			Variable localVariable= (Variable)iterator.previous();
			
			if (localVariable.isSubtypeOf(clazz))
				matches.add(localVariable);
		}
		
		return (Variable[]) matches.toArray(new Variable[matches.size()]);
	}

	/**
	 * Returns all local variables implementing <code>java.lang.Iterable</code>
	 * <em>and</em> all local arrays, in the order that they appear. That is, 
	 * the returned variables can be used within the <code>foreach</code> 
	 * language construct.
	 * 
	 * @return all local <code>Iterable</code>s and arrays
	 */
	public Variable[] findLocalIterables() {
		List iterables= new ArrayList();

		for (ListIterator iterator= fLocalVariables.listIterator(fLocalVariables.size()); iterator.hasPrevious();) {
			Variable localVariable= (Variable) iterator.previous();

			if (localVariable.isArray() || localVariable.isIterable())			
				iterables.add(localVariable);
		}

		return (Variable[]) iterables.toArray(new Variable[iterables.size()]);
	}

}

Back to the top