/
CompletionElementNotifier.java
228 lines (198 loc) · 9.8 KB
/
CompletionElementNotifier.java
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
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Corporation and others.
*
* 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
*******************************************************************************/
package org.eclipse.jdt.internal.codeassist;
import java.util.Map;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnAnnotationOfType;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnArgumentName;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnFieldName;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnFieldType;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnImportReference;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnKeyword;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnKeyword2;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnMethodName;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnMethodReturnType;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnMethodTypeParameter;
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnPackageReference;
import org.eclipse.jdt.internal.compiler.SourceElementNotifier;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
public class CompletionElementNotifier extends SourceElementNotifier {
private ASTNode assistNode;
public CompletionElementNotifier(
CompletionUnitStructureRequestor requestor,
boolean reportLocalDeclarations,
ASTNode assistNode) {
super(requestor, reportLocalDeclarations);
this.assistNode = assistNode;
}
protected char[][][] getArguments(Argument[] arguments) {
int argumentLength = arguments.length;
char[][] argumentTypes = new char[argumentLength][];
char[][] argumentNames = new char[argumentLength][];
int argumentCount = 0;
next : for (int i = 0; i < argumentLength; i++) {
Argument argument = arguments[i];
if (argument instanceof CompletionOnArgumentName && argument.name.length == 0) continue next;
argumentTypes[argumentCount] = CharOperation.concatWith(argument.type.getParameterizedTypeName(), '.');
argumentNames[argumentCount++] = argument.name;
}
if (argumentCount < argumentLength) {
System.arraycopy(argumentTypes, 0, argumentTypes = new char[argumentCount][], 0, argumentCount);
System.arraycopy(argumentNames, 0, argumentNames = new char[argumentCount][], 0, argumentCount);
}
return new char[][][] {argumentTypes, argumentNames};
}
@Override
protected char[][] getInterfaceNames(TypeDeclaration typeDeclaration) {
char[][] interfaceNames = null;
int superInterfacesLength = 0;
TypeReference[] superInterfaces = typeDeclaration.superInterfaces;
if (superInterfaces != null) {
superInterfacesLength = superInterfaces.length;
interfaceNames = new char[superInterfacesLength][];
} else {
if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
// see PR 3442
QualifiedAllocationExpression alloc = typeDeclaration.allocation;
if (alloc != null && alloc.type != null) {
superInterfaces = new TypeReference[] { alloc.type};
superInterfacesLength = 1;
interfaceNames = new char[1][];
}
}
}
if (superInterfaces != null) {
int superInterfaceCount = 0;
next: for (int i = 0; i < superInterfacesLength; i++) {
TypeReference superInterface = superInterfaces[i];
if (superInterface instanceof CompletionOnKeyword) continue next;
if (CompletionUnitStructureRequestor.hasEmptyName(superInterface, this.assistNode)) continue next;
interfaceNames[superInterfaceCount++] = CharOperation.concatWith(superInterface.getParameterizedTypeName(), '.');
}
if (superInterfaceCount == 0) return null;
if (superInterfaceCount < superInterfacesLength) {
System.arraycopy(interfaceNames, 0, interfaceNames = new char[superInterfaceCount][], 0, superInterfaceCount);
}
}
return interfaceNames;
}
@Override
protected char[] getSuperclassName(TypeDeclaration typeDeclaration) {
TypeReference superclass = typeDeclaration.superclass;
if (superclass instanceof CompletionOnKeyword) return null;
if (CompletionUnitStructureRequestor.hasEmptyName(superclass, this.assistNode)) return null;
return superclass != null ? CharOperation.concatWith(superclass.getParameterizedTypeName(), '.') : null;
}
@Override
protected char[][] getThrownExceptions(AbstractMethodDeclaration methodDeclaration) {
char[][] thrownExceptionTypes = null;
TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions;
if (thrownExceptions != null) {
int thrownExceptionLength = thrownExceptions.length;
int thrownExceptionCount = 0;
thrownExceptionTypes = new char[thrownExceptionLength][];
next : for (int i = 0; i < thrownExceptionLength; i++) {
TypeReference thrownException = thrownExceptions[i];
if (thrownException instanceof CompletionOnKeyword) continue next;
if (CompletionUnitStructureRequestor.hasEmptyName(thrownException, this.assistNode)) continue next;
thrownExceptionTypes[thrownExceptionCount++] =
CharOperation.concatWith(thrownException.getParameterizedTypeName(), '.');
}
if (thrownExceptionCount == 0) return null;
if (thrownExceptionCount < thrownExceptionLength) {
System.arraycopy(thrownExceptionTypes, 0, thrownExceptionTypes = new char[thrownExceptionCount][], 0, thrownExceptionCount);
}
}
return thrownExceptionTypes;
}
@Override
protected char[][] getTypeParameterBounds(TypeParameter typeParameter) {
TypeReference firstBound = typeParameter.type;
TypeReference[] otherBounds = typeParameter.bounds;
char[][] typeParameterBounds = null;
if (firstBound != null) {
if (otherBounds != null) {
int otherBoundsLength = otherBounds.length;
char[][] boundNames = new char[otherBoundsLength+1][];
int boundCount = 0;
if (!CompletionUnitStructureRequestor.hasEmptyName(firstBound, this.assistNode)) {
boundNames[boundCount++] = CharOperation.concatWith(firstBound.getParameterizedTypeName(), '.');
}
for (int j = 0; j < otherBoundsLength; j++) {
TypeReference otherBound = otherBounds[j];
if (!CompletionUnitStructureRequestor.hasEmptyName(otherBound, this.assistNode)) {
boundNames[boundCount++] =
CharOperation.concatWith(otherBound.getParameterizedTypeName(), '.');
}
}
if (boundCount == 0) {
boundNames = CharOperation.NO_CHAR_CHAR;
} else if (boundCount < otherBoundsLength + 1){
System.arraycopy(boundNames, 0, boundNames = new char[boundCount][], 0, boundCount);
}
typeParameterBounds = boundNames;
} else {
if (!CompletionUnitStructureRequestor.hasEmptyName(firstBound, this.assistNode)) {
typeParameterBounds = new char[][] { CharOperation.concatWith(firstBound.getParameterizedTypeName(), '.')};
} else {
typeParameterBounds = CharOperation.NO_CHAR_CHAR;
}
}
} else {
typeParameterBounds = CharOperation.NO_CHAR_CHAR;
}
return typeParameterBounds;
}
@Override
protected void notifySourceElementRequestor(AbstractMethodDeclaration methodDeclaration, TypeDeclaration declaringType, ImportReference currentPackage) {
if (methodDeclaration instanceof CompletionOnMethodReturnType) return;
if (methodDeclaration instanceof CompletionOnMethodTypeParameter) return;
if (methodDeclaration instanceof CompletionOnMethodName) return;
super.notifySourceElementRequestor(methodDeclaration, declaringType, currentPackage);
}
@Override
public void notifySourceElementRequestor(CompilationUnitDeclaration parsedUnit, int sourceStart, int sourceEnd, boolean reportReference, HashtableOfObjectToInt sourceEndsMap, Map nodesToCategoriesMap) {
super.notifySourceElementRequestor(parsedUnit, sourceStart, sourceEnd, reportReference, sourceEndsMap, nodesToCategoriesMap);
}
@Override
protected void notifySourceElementRequestor(FieldDeclaration fieldDeclaration, TypeDeclaration declaringType) {
if (fieldDeclaration instanceof CompletionOnFieldType) return;
if (fieldDeclaration instanceof CompletionOnFieldName) return;
super.notifySourceElementRequestor(fieldDeclaration, declaringType);
}
@Override
protected void notifySourceElementRequestor(ImportReference importReference, boolean isPackage) {
if (importReference instanceof CompletionOnKeyword2) return;
if (importReference instanceof CompletionOnImportReference ||
importReference instanceof CompletionOnPackageReference) {
if (importReference.tokens[importReference.tokens.length - 1].length == 0) return;
}
super.notifySourceElementRequestor(importReference, isPackage);
}
@Override
protected void notifySourceElementRequestor(TypeDeclaration typeDeclaration, boolean notifyTypePresence, TypeDeclaration declaringType, ImportReference currentPackage) {
if (typeDeclaration instanceof CompletionOnAnnotationOfType) return;
super.notifySourceElementRequestor(typeDeclaration, notifyTypePresence, declaringType, currentPackage);
}
}