KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > TypeInfoUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.NullProgressMonitor;
17
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.Signature;
22 import org.eclipse.jdt.core.dom.SimpleName;
23 import org.eclipse.jdt.core.search.IJavaSearchConstants;
24 import org.eclipse.jdt.core.search.IJavaSearchScope;
25 import org.eclipse.jdt.core.search.SearchEngine;
26 import org.eclipse.jdt.core.search.SearchPattern;
27
28 import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;
29 import org.eclipse.jdt.internal.ui.text.correction.SimilarElementsRequestor;
30
31 public class TypeInfoUtil {
32
33     public static TypeInfo searchTypeInfo(IJavaProject javaProject, SimpleName accessNode, String JavaDoc qualifiedTypeName) throws JavaModelException {
34         boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
35         
36         int typeKinds= SimilarElementsRequestor.ALL_TYPES;
37         if (accessNode != null) {
38             typeKinds= ASTResolving.getPossibleTypeKinds(accessNode, is50OrHigher);
39         }
40         
41         IJavaSearchScope searchScope= SearchEngine.createJavaSearchScope(new IJavaElement[] {javaProject});
42         
43         String JavaDoc typeName= Signature.getSimpleName(qualifiedTypeName);
44     
45         ArrayList JavaDoc typeInfos= new ArrayList JavaDoc();
46         TypeInfoRequestor requestor= new TypeInfoRequestor(typeInfos);
47         new SearchEngine().searchAllTypeNames(null, typeName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, getSearchForConstant(typeKinds), searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
48         
49         for (Iterator JavaDoc iter= typeInfos.iterator(); iter.hasNext();) {
50             TypeInfo info= (TypeInfo)iter.next();
51             
52             if (info.getFullyQualifiedName().equals(qualifiedTypeName)) {
53                 return info;
54             }
55         }
56         return null;
57     }
58     
59     private static int getSearchForConstant(int typeKinds) {
60         final int CLASSES= SimilarElementsRequestor.CLASSES;
61         final int INTERFACES= SimilarElementsRequestor.INTERFACES;
62         final int ENUMS= SimilarElementsRequestor.ENUMS;
63         final int ANNOTATIONS= SimilarElementsRequestor.ANNOTATIONS;
64     
65         switch (typeKinds & (CLASSES | INTERFACES | ENUMS | ANNOTATIONS)) {
66             case CLASSES: return IJavaSearchConstants.CLASS;
67             case INTERFACES: return IJavaSearchConstants.INTERFACE;
68             case ENUMS: return IJavaSearchConstants.ENUM;
69             case ANNOTATIONS: return IJavaSearchConstants.ANNOTATION_TYPE;
70             case CLASSES | INTERFACES: return IJavaSearchConstants.CLASS_AND_INTERFACE;
71             case CLASSES | ENUMS: return IJavaSearchConstants.CLASS_AND_ENUM;
72             default: return IJavaSearchConstants.TYPE;
73         }
74     }
75     
76
77 }
78
Popular Tags