Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28087ca00e7eb5255ce86a22bd01b424238f1047 (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
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060419   132905 cbrealey@ca.ibm.com - Chris Brealey
 * 20060711   149411 cbrealey@ca.ibm.com - Chris Brealey
 * 20070102   196371 sengpl@ca.ibm.com - Seng Phung-Lu
 *******************************************************************************/
package org.eclipse.jst.ws.internal.conformance;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;

/**
 * @author cbrealey
 * A JDTResolver provides several convenience methods for
 * navigating references between Java classes, methods,
 * fields and properties. A JDTResolver is constructed
 * for a Java project whose effective classpath will
 * govern the scope of search operations performed by the
 * JDTResolver whenever it needs to convert a signature
 * into a JDT IType object.
 */
public class JDTResolver
{
	/**
	 * Creates a new JDTResolver for the given project.
	 * @param project The project providing the context
	 * @param monitor The progress monitor for this resolver
	 * to use, or null if progress monitoring is not desired.
	 * within which this resolver will search for classes.
	 */
	public JDTResolver ( IProject project, IProgressMonitor monitor )
	{
		javaProject_ = JavaCore.create(project);
		monitor_ = monitor;
	}
	
	/**
	 * Returns true if and only if the given JDT IType is an interface.
	 * @param jdtType The type to analyze.
	 * @return True if and only if the given JDT IType is an interface.
	 * @throws JavaModelException If the JDT model fails to
	 * analyze the given type.
	 */
	public boolean isInterface ( IType jdtType )
	throws JavaModelException
	{
		return jdtType.isInterface();
	}
	
	/**
	 * Returns true if and only if the given JDT IType is an abstract class.
	 * @param jdtType The type to analyze.
	 * @return True if and only if the given JDT IType is an abstract class.
	 * @throws JavaModelException If the JDT model fails to
	 * analyze the given type.
	 */
	public boolean isAbstract ( IType jdtType )
	throws JavaModelException
	{
		return Flags.isAbstract(jdtType.getFlags());
	}
	
	/**
	 * Returns true if and only if instances of the given JDT IType
	 * can be instantiated via a public default constructor. The class
	 * must have an explicit public default constructor, or have no
	 * explicit constructors at all to meet this criteria.
	 * @param jdtType The type to analyze.
	 * @return True if and only if instances of the type are
	 * public default constructable.
	 * @throws JavaModelException If the JDT model fails to
	 * analyze the given type.
	 */
	public boolean isConstructable ( IType jdtType )
	throws JavaModelException
	{
		IMethod[] methods = jdtType.getMethods();
		int numberOfConstructors = 0;
		for (int m=0; m<methods.length; m++)
		{
			if (methods[m].isConstructor())
			{
				numberOfConstructors++;
				if (methods[m].getNumberOfParameters() == 0 && Flags.isPublic(methods[m].getFlags()))
				{
					return true;
				}
			}
		}
		return numberOfConstructors == 0;
	}
	
	/**
	 * Returns true if and only if the given fully qualified
	 * type name is a type from the standard JDK, that is,
	 * if the given type name belongs under the "java" or
	 * "javax" packages.
	 * @param typeName The name of the type to check.
	 * @return True if and only if the type is a non-primitive,
	 * Java standard type.
	 */
	public boolean isStandardType ( String typeName )
	{
		return typeName.startsWith("java.") || typeName.startsWith("javax.");
	}
	
	/**
	 * Returns true if and only if the given type name is a
	 * Java primitive type.
	 * @param typeName The name of the type to check.
	 * @return True if and only if the type is a Java primitive type.
	 */
	public boolean isPrimitiveType ( String typeName )
	{
		for (int i=0; i<primitiveTypes_.length; i++)
		{
			if (primitiveTypes_[i].equals(typeName))
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns true if and only if the given fully qualified
	 * type name is a Java wrapper type.
	 * @param typeName The name of the type to check.
	 * @return True if and only if the type is a Java wrapper type.
	 */
	public boolean isWrapperType ( String typeName )
	{
		for (int i=0; i<wrapperTypes_.length; i++)
		{
			if (wrapperTypes_[i].equals(typeName))
			{
				return true;
			}
		}
		return false;
	}
		
	/**
	 * Returns true if and only if the given fully qualified
	 * type name is a Java primitive or standard type supported
	 * by the JAX-RPC specification.
	 * @param typeName The name of the type to check.
	 * @return True if and only if the type is a Java wrapper type.
	 */
	public boolean isJAXRPCStandardType ( String typeName )
	{
		for (int i=0; i<jaxrpcTypes_.length; i++)
		{
			if (jaxrpcTypes_[i].equals(typeName))
			{
				return true;
			}
		}
		if (isWrapperType(typeName) && !"java.lang.Character".equals(typeName))
		{
			return true;
		}
		if (isPrimitiveType(typeName) && !"char".equals(typeName))
		{
			return true;
		}
		return false;
	}
		
	/**
	 * Returns an array of zero or more JDT IType objects
	 * for the public fields belonging to the given type
	 * @param jdtType The type to analyze.
	 * @param jdtSuperTypes Any supertypes to analyze,
	 * or null to analyze only the <code>jdtType</code>.
	 * @return An array of zero or more public field types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given types to satisfy this request.
	 */
	public IField[] getPublicFields ( IType jdtType, IType[] jdtSuperClasses )
	throws JavaModelException
	{
		List fieldsList = new LinkedList();
		harvestPublicFields(jdtType,fieldsList);
		if (jdtSuperClasses != null)
		{
			for (int t=0; t<jdtSuperClasses.length; t++)
			{
				harvestPublicFields(jdtSuperClasses[t],fieldsList);
			}
		}
		return (IField[])fieldsList.toArray(new IField[0]);
	}
	
	/**
	 * Returns an array of zero or more JDT IMethod objects
	 * for the public methods belonging to the given type.
	 * @param jdtType The type to analyze.
	 * @param jdtSuperTypes Any supertypes to analyze,
	 * or null to analyze only the <code>jdtType</code>.
	 * @return An array of zero or more public methods.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given types to satisfy this request.
	 */
	public IMethod[] getPublicMethods ( IType jdtType, IType[] jdtSuperClasses )
	throws JavaModelException
	{
		List methodsList = new LinkedList();
		harvestPublicMethods(jdtType,methodsList);
		if (jdtSuperClasses != null)
		{
			for (int t=0; t<jdtSuperClasses.length; t++)
			{
				harvestPublicMethods(jdtSuperClasses[t],methodsList);
			}
		}
		return (IMethod[])methodsList.toArray(new IMethod[0]);
	}
	
	/**
	 * Returns an array of zero or more JDT IType objects
	 * for the public properties (getters and/or setters)
	 * belonging to the given type.
	 * @param jdtType The type to analyze.
	 * @param jdtSuperTypes Any supertypes to analyze,
	 * or null to analyze only the <code>jdtType</code>.
	 * @return An array of zero or more public property types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given types to satisfy this request.
	 */
	public IJavaBeanProperty[] getPublicProperties ( IType jdtType, IType[] jdtSuperClasses )
	throws JavaModelException
	{
		IMethod[] methods = getPublicMethods(jdtType,jdtSuperClasses);
		Map properties = new HashMap();
		for (int m=0; m<methods.length; m++)
		{
			String name = getGetterName(methods[m]);
			if (name != null)
			{
				JavaBeanProperty property = (JavaBeanProperty)properties.get(name);
				if (property == null)
				{
					property = new JavaBeanProperty();
					property.setName(name);
					properties.put(name,property);
				}
				property.setGetter(methods[m]);
			}
			else
			{
				name = getSetterName(methods[m]);
				if (name != null)
				{
					JavaBeanProperty property = (JavaBeanProperty)properties.get(name);
					if (property == null)
					{
						property = new JavaBeanProperty();
						property.setName(name);
						properties.put(name,property);
					}
					property.setSetter(methods[m]);
				}
			}
		}
		return (IJavaBeanProperty[])properties.values().toArray(new JavaBeanProperty[0]);
	}
	
	/**
	 * Returns the JDT IType object for the given field.
	 * @param jdtField The field to analyze.
	 * @return The field type.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given field to satisfy this request.
	 */
	public IType getFieldType ( IField jdtField )
	throws JavaModelException
	{
		IJavaElement elem = jdtField.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String signature = jdtField.getTypeSignature();
		String typeName = getTypeNameFromSignature(signature);
		return findType(typeName,ancestor);
	}
	
	/**
	 * Returns the type name for the given field.
	 * @param jdtField The field to analyze.
	 * @return The field type name.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given field to satisfy this request.
	 */
	public String getFieldTypeName ( IField jdtField )
	throws JavaModelException
	{
		IJavaElement elem = jdtField.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String signature = jdtField.getTypeSignature();
		String typeName = getTypeNameFromSignature(signature);
		return resolveType(typeName,ancestor);
	}
	
	/**
	 * Returns the JDT IType object for the given bean property.
	 * @param javaBeanProperty The bean property to analyze.
	 * @return The property type.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given bean property to satisfy this request.
	 */
	public IType getPropertyType ( IJavaBeanProperty javaBeanProperty )
	throws JavaModelException
	{
		IMethod method = javaBeanProperty.getGetter();
		if (method != null)
		{
			return getReturnType(method);
		}
		else
		{
			method = javaBeanProperty.getSetter();
			if (method != null)
			{
				IType[] parameters = getParameterTypes(method);
				if (parameters.length > 0)
				{
					//
					// It's the last parameter's type we want,
					// for indexed or non-indexed setters.
					//
					return parameters[parameters.length-1];
				}
			}
		}
		return null;
	}
	
	/**
	 * Returns the type name for the given bean property.
	 * @param javaBeanProperty The bean property to analyze.
	 * @return The property type name.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given bean property to satisfy this request.
	 */
	public String getPropertyTypeName ( IJavaBeanProperty javaBeanProperty )
	throws JavaModelException
	{
		IMethod method = javaBeanProperty.getGetter();
		if (method != null)
		{
			return getReturnTypeName(method);
		}
		else
		{
			method = javaBeanProperty.getSetter();
			if (method != null)
			{
				String[] parameterTypeNames = getParameterTypeNames(method);
				if (parameterTypeNames.length > 0)
				{
					return parameterTypeNames[0];
				}
			}
		}
		return null;
	}
	
	/**
	 * Returns the JDT IType object for the return type
	 * of the given method, or null if the method is void.
	 * @param jdtMethod The method to analyze.
	 * @return The method return type, or null if none.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public IType getReturnType ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String signature = jdtMethod.getReturnType();
		String typeName = getTypeNameFromSignature(signature);
		return findType(typeName,ancestor);
	}
	
	/**
	 * Returns the type name for the return type
	 * of the given method, or null if the method is void.
	 * @param jdtMethod The method to analyze.
	 * @return The method return type name, or null if none.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public String getReturnTypeName ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String signature = jdtMethod.getReturnType();
		String typeName = getTypeNameFromSignature(signature);
		return resolveType(typeName,ancestor);
	}
	
	/**
	 * Returns an array of zero or more JDT IType objects
	 * for the parameters of the given method.
	 * @param jdtMethod The method to analyze.
	 * @return An array of zero or more parameter types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public IType[] getParameterTypes ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String[] signatures = jdtMethod.getParameterTypes();
		IType[] types = new IType[signatures.length];
		for (int s=0; s<signatures.length; s++)
		{
			String typeName = getTypeNameFromSignature(signatures[s]);
			types[s] = findType(typeName,ancestor);
		}
		return types;
	}
	
	/**
	 * Returns an array of zero or more type names
	 * for the parameters of the given method.
	 * @param jdtMethod The method to analyze.
	 * @return An array of zero or more parameter type names.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public String[] getParameterTypeNames ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String[] signatures = jdtMethod.getParameterTypes();
		String[] typeNames = new String[signatures.length];
		for (int s=0; s<signatures.length; s++)
		{
			String typeName = getTypeNameFromSignature(signatures[s]);
			typeNames[s] = resolveType(typeName,ancestor);
		}
		return typeNames;
	}
	
	/**
	 * Returns an array of zero or more JDT IType objects
	 * for the exceptions thrown by the given method.
	 * @param jdtMethod The method to analyze.
	 * @return An array of zero or more exception types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public IType[] getExceptionTypes ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String[] signatures = jdtMethod.getExceptionTypes();
		IType[] types = new IType[signatures.length];
		for (int s=0; s<signatures.length; s++)
		{
			String typeName = getTypeNameFromSignature(signatures[s]);
			types[s] = findType(typeName,ancestor);
		}
		return types;
	}
	
	/**
	 * Returns an array of zero or more type names
	 * for the exceptions thrown by the given method.
	 * @param jdtMethod The method to analyze.
	 * @return An array of zero or more exception type names.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given method to satisfy this request.
	 */
	public String[] getExceptionTypeNames ( IMethod jdtMethod )
	throws JavaModelException
	{
		IJavaElement elem = jdtMethod.getAncestor(IJavaElement.TYPE);
		IType ancestor = elem instanceof IType ? (IType)elem : null;
		String[] signatures = jdtMethod.getExceptionTypes();
		String[] typeNames = new String[signatures.length];
		for (int s=0; s<signatures.length; s++)
		{
			String typeName = getTypeNameFromSignature(signatures[s]);
			typeNames[s] = resolveType(typeName,ancestor);
		}
		return typeNames;
	}
	
	/**
	 * Returns an array of zero or more types representing
	 * the superclasses, if any, of the given IType in bottom-up
	 * order excluding java.lang.Object.
	 * @param jdtType The type to analyze.
	 * @return An array of zero or more superclass types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given type to satisfy this request.
	 */
	public IType[] getSuperClasses ( IType jdtType )
	throws JavaModelException
	{
		return getSuperClasses(jdtType,"java.lang.Object");
	}
	
	/**
	 * Returns an array of zero or more types representing
	 * the superclasses, if any, of the given IType.
	 * Under normal circumstances, java.lang.Object is included.
	 * @param jdtType The type to analyze.
	 * @param stopClassName The name of a stop class used to limit
	 * the superclasses returned to the caller. If the stop class
	 * is null or names a class not found in the hierarchy, all
	 * superclasses are returned. Otherwise, only superclasses up
	 * to but excluding the stop class are returned.
	 * @return An array of zero or more superclass types.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given type to satisfy this request.
	 */
	public IType[] getSuperClasses ( IType jdtType, String stopClassName )
	throws JavaModelException
	{
		ITypeHierarchy hierarchy = jdtType.newSupertypeHierarchy(monitor_);
		IType[] allSuperClasses = hierarchy.getAllSuperclasses(jdtType);
		List superClasses = new LinkedList();
		for (int i=0; i<allSuperClasses.length; i++)
		{
			if (allSuperClasses[i].getFullyQualifiedName().equals(stopClassName))
				break;
			superClasses.add(allSuperClasses[i]);
		}
		return (IType[])superClasses.toArray(new IType[0]);
	}
	
	/**
	 * Extracts the qualified type name from a JDT signature.
	 * @param signature The signature to examine.
	 * @return The qualified type name from the signature.
	 */
	public String getTypeNameFromSignature ( String signature )
	{

		String packageName = Signature.getSignatureQualifier(signature);
		String baseName = Signature.getSignatureSimpleName(signature);
		if (baseName.endsWith("[]")){
			baseName = baseName.substring(0, baseName.indexOf("["));
		}
			
		String typeName = (packageName.trim().equals("")?"":packageName+".") + baseName;
		return typeName;
	}
	
	/**
	 * Attempts to resolve an unresolved type name in the
	 * context of the given ancestor type (ie. the type
	 * acting as the context within which the type name
	 * should be resolved).
	 * @param typeName The qualified name of the type to find.
	 * @param ancestor The type providing the context within
	 * which to look up the type.
	 * @return The qualified name, or the original type name
	 * if it could not be resolved.
	 */
	public String resolveType ( String typeName, IType ancestor )
	throws JavaModelException
	{
		if (ancestor != null)
		{
			String[][] matches = ancestor.resolveType(typeName);
			if (matches != null && matches.length > 0)
			{
				StringBuffer qname = new StringBuffer();
				int n = matches[0].length;
				for (int j=0; j<n-1; j++)
				{
					qname.append(matches[0][j]).append(".");
				}
				if (n >= 0)
				{
					qname.append(matches[0][n-1]);
				}
				return qname.toString();
			}
		}
		return typeName;
	}

	/**
	 * Attempts to find a JDT IType object for the given
	 * resolved or unresolved type name in the context of
	 * the given ancestor type (ie. the type acting as the
	 * context within which the type name should be resolved).
	 * @param typeName The qualified name of the type to find.
	 * @param ancestor The type providing the context within
	 * which to look up the type.
	 * @return The IType object of the given qualified name,
	 * or null if no type could be found in the workspace.
	 */
	public IType findType ( String typeName, IType ancestor )
	throws JavaModelException
	{
		IType type = javaProject_.findType(typeName);
		if (type == null && ancestor != null)
		{
			String[][] matches = ancestor.resolveType(typeName);
			if (matches != null)
			{
				int i=0;
				while (type == null && i<matches.length)
				{
					StringBuffer qname = new StringBuffer();
					int n = matches[i].length;
					for (int j=0; j<n-1; j++)
					{
						qname.append(matches[i][j]).append(".");
					}
					if (n >= 0)
					{
						qname.append(matches[i][n-1]);
					}
					type = javaProject_.findType(qname.toString());
					i++;
				}
			}
		}
		return type;
	}

	/**
	 * Finds all public, non-constructor IMethods belonging
	 * to the given IType and adds them to the given list.
	 * @param jdtType The type whose methods are to be harvested.
	 * @param list The list to which the harvested methods will be added.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given type to satisfy this request.
	 */
	public void harvestPublicMethods ( IType jdtType, List list )
	throws JavaModelException
	{
		IMethod[] methods = jdtType.getMethods();
		for (int m=0; m<methods.length; m++)
		{
			if (!methods[m].isConstructor() && Flags.isPublic(methods[m].getFlags()))
			{
				list.add(methods[m]);
			}
		}
	}
	
	/**
	 * Finds all public IFields belonging
	 * to the given IType and adds them to the given list.
	 * @param jdtType The type whose fields are to be harvested.
	 * @param list The list to which the harvested fields will be added.
	 * @throws JavaModelException If the JDT engine fails to
	 * analyze the given type to satisfy this request.
	 */
	public void harvestPublicFields ( IType jdtType, List list )
	throws JavaModelException
	{
		IField[] fields = jdtType.getFields();
		for (int f=0; f<fields.length; f++)
		{
			if (Flags.isPublic(fields[f].getFlags()))
			{
				list.add(fields[f]);
			}
		}
	}
	
	/**
	 * If the given method is a getter according to the Bean spec
	 * (eg. "MyType getMyProperty ()" or "boolean isMyProperty ()")
	 * return the corresponding bean property name (eg. "myProperty"),
	 * otherwise, return null.
	 * @param method The method to check.
	 * @return The property name or none if the method is not a getter.
	 */
	public String getGetterName ( IMethod method )
	{
		try
		{
			String methodName = method.getElementName();
			if (methodName.startsWith("get") && methodName.length() > 3)
			{
				String signature = method.getSignature();
				String returnTypeName = Signature.getReturnType(signature);
				//
				// Getters must not be void.
				//
				if (returnTypeName != null && returnTypeName != Signature.SIG_VOID)
				{
					String[] parameterSignatures = method.getParameterTypes();
					//
					// Non-indexed getters must have zero parameters.
					//
					if (parameterSignatures.length == 0)
					{
						return methodName.substring(3,4).toLowerCase() + methodName.substring(4);
					}
					//
					// Indexed getters must have exactly one parameter of type "int".
					//
					else if (parameterSignatures.length == 1)
					{
						String indexTypeName = getTypeNameFromSignature(parameterSignatures[0]);
						if (indexTypeName.equals("int"))
						{
							return methodName.substring(3,4).toLowerCase() + methodName.substring(4);
						}
					}
				}
			}
			else if (methodName.startsWith("is") && methodName.length() > 2)
			{
				String signature = method.getSignature();
				String returnTypeName = Signature.getReturnType(signature);
				//
				// "is" getters must be boolean.
				//
				if (Signature.SIG_BOOLEAN.equals(returnTypeName))
				{
					//
					// "is" getters must have no parameters.
					//
					if (method.getParameterTypes().length == 0)
					{
						return methodName.substring(2,3).toLowerCase() + methodName.substring(3);
					}
				}
			}
		}
		catch (JavaModelException e)
		{
			return null;
		}
		return null;
	}
	
	/**
	 * If the given method is a setter according to the Bean spec
	 * (eg. "void setMyProperty (MyType)" return the corresponding
	 * bean property name (eg. "myProperty"), otherwise, return null.
	 * @param method The method to check.
	 * @return The property name or none if the method is not a setter.
	 */
	public String getSetterName ( IMethod method )
	{
		try
		{
			String methodName = method.getElementName();
			if (methodName.startsWith("set") && methodName.length() > 3)
			{
				String signature = method.getSignature();
				String returnTypeName = Signature.getReturnType(signature);
				//
				// Setters must be void.
				//
				if (Signature.SIG_VOID.equals(returnTypeName))
				{
					String[] parameterSignatures = method.getParameterTypes();
					//
					// Non-indexed setters must have exactly one parameter.
					//
					if (parameterSignatures.length == 1)
					{
						return methodName.substring(3,4).toLowerCase() + methodName.substring(4);
					}
					//
					// Indexed setters must have exactly two parameters,
					// the first of which must be "int".
					//
					else if (parameterSignatures.length == 2)
					{
						if (Signature.SIG_INT.equals(parameterSignatures[0]))
						{
							return methodName.substring(3,4).toLowerCase() + methodName.substring(4);
						}
					}
				}
			}
		}
		catch (JavaModelException e)
		{
			return null;
		}
		return null;
	}
	
	/*
	 * The Java project that governs the search scope for this resolver.
	 */
	private IJavaProject javaProject_;
	
	/*
	 * The progress monitor to use, if any. 
	 */
	private IProgressMonitor monitor_;
	
	/*
	 * The set of Java wrapper types.
	 */
	private static String[] wrapperTypes_ = {
		"java.lang.Character",
		"java.lang.Boolean",
		"java.lang.Byte",
		"java.lang.Short",
		"java.lang.Integer",
		"java.lang.Long",
		"java.lang.Float",
		"java.lang.Double"
	};

	/*
	 * The set of Java primitive types.
	 */
	private static String[] primitiveTypes_ = {
		"char",
		"boolean",
		"byte",
		"short",
		"int",
		"long",
		"float",
		"double"
	};

	/*
	 * The set of JAX-RPC supported Java standard types,
	 * excluding primitives and wrapper types.
	 */
	private static String[] jaxrpcTypes_ = {
		"boolean",
		"byte",
		"short",
		"int",
		"long",
		"float",
		"double",
		"java.lang.String",
		"java.lang.Boolean",
		"java.lang.Byte",
		"java.lang.Short",
		"java.lang.Integer",
		"java.lang.Long",
		"java.lang.Float",
		"java.lang.Double",
		"java.util.Date",
		"java.util.Calendar",
		"java.math.BigInteger",
		"java.math.BigDecimal",
		"java.net.URI",
		"javax.xml.namespace.QName"
	};
}

Back to the top