KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > SimilarElementsRequestor


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.ui.text.correction;
12
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.jdt.core.CompletionProposal;
16 import org.eclipse.jdt.core.CompletionRequestor;
17 import org.eclipse.jdt.core.Flags;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.JavaModelException;
20 import org.eclipse.jdt.core.Signature;
21
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.Javadoc;
25 import org.eclipse.jdt.core.dom.Name;
26 import org.eclipse.jdt.core.dom.QualifiedName;
27
28 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
29 import org.eclipse.jdt.internal.corext.util.TypeFilter;
30
31 public class SimilarElementsRequestor extends CompletionRequestor {
32
33     public static final int CLASSES= 1 << 1;
34     public static final int INTERFACES= 1 << 2;
35     public static final int ANNOTATIONS= 1 << 3;
36     public static final int ENUMS= 1 << 4;
37     public static final int VARIABLES= 1 << 5;
38     public static final int PRIMITIVETYPES= 1 << 6;
39     public static final int VOIDTYPE= 1 << 7;
40     public static final int REF_TYPES= CLASSES | INTERFACES | ENUMS | ANNOTATIONS;
41     public static final int REF_TYPES_AND_VAR= REF_TYPES | VARIABLES;
42     public static final int ALL_TYPES= PRIMITIVETYPES | REF_TYPES_AND_VAR;
43
44     private static final String JavaDoc[] PRIM_TYPES= { "boolean", "byte", "char", "short", "int", "long", "float", "double" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
45

46     private int fKind;
47     private String JavaDoc fName;
48
49     private HashSet JavaDoc fResult;
50
51     public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
52         int pos= name.getStartPosition();
53         int nArguments= -1;
54
55         String JavaDoc identifier= ASTNodes.getSimpleNameIdentifier(name);
56         String JavaDoc returnType= null;
57         ICompilationUnit preparedCU= null;
58
59         try {
60             if (name.isQualifiedName()) {
61                 pos= ((QualifiedName) name).getName().getStartPosition();
62             } else {
63                 pos= name.getStartPosition() + 1; // first letter must be included, other
64
}
65             Javadoc javadoc= (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
66             if (javadoc != null) {
67                 preparedCU= createPreparedCU(cu, javadoc, name.getStartPosition());
68                 cu= preparedCU;
69             }
70
71             SimilarElementsRequestor requestor= new SimilarElementsRequestor(identifier, kind, nArguments, returnType);
72             requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
73             requestor.setIgnored(CompletionProposal.KEYWORD, true);
74             requestor.setIgnored(CompletionProposal.LABEL_REF, true);
75             requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
76             requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
77             requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
78             requestor.setIgnored(CompletionProposal.METHOD_REF, true);
79             requestor.setIgnored(CompletionProposal.FIELD_REF, true);
80             requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
81             requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
82             requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
83             requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
84             requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
85             return requestor.process(cu, pos);
86         } finally {
87             if (preparedCU != null) {
88                 preparedCU.discardWorkingCopy();
89             }
90         }
91     }
92
93     private static ICompilationUnit createPreparedCU(ICompilationUnit cu, Javadoc comment, int wordStart) throws JavaModelException {
94         int startpos= comment.getStartPosition();
95         boolean isTopLevel= comment.getParent().getParent() instanceof CompilationUnit;
96         char[] content= (char[]) cu.getBuffer().getCharacters().clone();
97         if (isTopLevel && (wordStart + 6 < content.length)) {
98             content[startpos++]= 'i'; content[startpos++]= 'm'; content[startpos++]= 'p';
99             content[startpos++]= 'o'; content[startpos++]= 'r'; content[startpos++]= 't';
100         }
101         if (wordStart < content.length) {
102             for (int i= startpos; i < wordStart; i++) {
103                 content[i]= ' ';
104             }
105         }
106
107         /*
108          * Explicitly create a new non-shared working copy.
109          */

110         ICompilationUnit newCU= cu.getWorkingCopy(null);
111         newCU.getBuffer().setContents(content);
112         return newCU;
113     }
114
115
116     /**
117      * Constructor for SimilarElementsRequestor.
118      */

119     private SimilarElementsRequestor(String JavaDoc name, int kind, int nArguments, String JavaDoc preferredType) {
120         super();
121         fName= name;
122         fKind= kind;
123
124         fResult= new HashSet JavaDoc();
125     }
126
127     private void addResult(SimilarElement elem) {
128         fResult.add(elem);
129     }
130
131     private SimilarElement[] process(ICompilationUnit cu, int pos) throws JavaModelException {
132         try {
133             cu.codeComplete(pos, this);
134             processKeywords();
135             return (SimilarElement[]) fResult.toArray(new SimilarElement[fResult.size()]);
136         } finally {
137             fResult.clear();
138         }
139     }
140
141     private boolean isKind(int kind) {
142         return (fKind & kind) != 0;
143     }
144
145     /**
146      * Method addPrimitiveTypes.
147      */

148     private void processKeywords() {
149         if (isKind(PRIMITIVETYPES)) {
150             for (int i= 0; i < PRIM_TYPES.length; i++) {
151                 if (NameMatcher.isSimilarName(fName, PRIM_TYPES[i])) {
152                     addResult(new SimilarElement(PRIMITIVETYPES, PRIM_TYPES[i], 50));
153                 }
154             }
155         }
156         if (isKind(VOIDTYPE)) {
157             String JavaDoc voidType= "void"; //$NON-NLS-1$
158
if (NameMatcher.isSimilarName(fName, voidType)) {
159                 addResult(new SimilarElement(PRIMITIVETYPES, voidType, 50));
160             }
161         }
162     }
163
164     private static final int getKind(int flags, char[] typeNameSig) {
165         if (Signature.getTypeSignatureKind(typeNameSig) == Signature.TYPE_VARIABLE_SIGNATURE) {
166             return VARIABLES;
167         }
168         if (Flags.isAnnotation(flags)) {
169             return ANNOTATIONS;
170         }
171         if (Flags.isInterface(flags)) {
172             return INTERFACES;
173         }
174         if (Flags.isEnum(flags)) {
175             return ENUMS;
176         }
177         return CLASSES;
178     }
179
180
181     private void addType(char[] typeNameSig, int flags, int relevance) {
182         int kind= getKind(flags, typeNameSig);
183         if (!isKind(kind)) {
184             return;
185         }
186         String JavaDoc fullName= new String JavaDoc(Signature.toCharArray(Signature.getTypeErasure(typeNameSig)));
187         if (TypeFilter.isFiltered(fullName)) {
188             return;
189         }
190         if (NameMatcher.isSimilarName(fName, Signature.getSimpleName(fullName))) {
191             addResult(new SimilarElement(kind, fullName, relevance));
192         }
193     }
194
195
196     /* (non-Javadoc)
197      * @see org.eclipse.jdt.core.CompletionRequestor#accept(org.eclipse.jdt.core.CompletionProposal)
198      */

199     public void accept(CompletionProposal proposal) {
200         if (proposal.getKind() == CompletionProposal.TYPE_REF) {
201             addType(proposal.getSignature(), proposal.getFlags(), proposal.getRelevance());
202         }
203     }
204 }
205
Popular Tags