package org.eclipse.jdt.internal.codeassist;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.CompletionContext;
import org.eclipse.jdt.core.CompletionFlags;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.NameLookup;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.SourceMapper;
public class InternalCompletionProposal extends CompletionProposal {
private static Object NO_ATTACHED_SOURCE = new Object();
protected CompletionEngine completionEngine;
protected NameLookup nameLookup;
protected char[] declarationPackageName;
protected char[] declarationTypeName;
protected char[] moduleName;
protected char[] packageName;
protected char[] typeName;
protected char[][] parameterPackageNames;
protected char[][] parameterTypeNames;
protected char[] originalSignature;
private boolean hasNoParameterNamesFromIndex = false;
private boolean updateCompletion = false;
protected int accessibility = IAccessRule.K_ACCESSIBLE;
protected boolean isConstructor = false;
private int completionKind;
private int completionLocation;
private int tokenStart = 0;
private int tokenEnd = 0;
private char[] completion = CharOperation.NO_CHAR;
private int replaceStart = 0;
private int replaceEnd = 0;
private int relevance = 1;
private char[] declarationSignature = null;
private char[] declarationKey = null;
private char[] name = null;
private Binding binding = null;
private char[] signature = null;
private char[] key = null;
private CompletionProposal[] requiredProposals;
private int flags = Flags.AccDefault;
private int additionalFlags = CompletionFlags.Default;
private char[][] parameterNames = null;
private boolean parameterNamesComputed = false;
protected char[][] findConstructorParameterNames(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] paramTypeNames){
if(paramTypeNames == null || declaringTypeName == null) return null;
char[][] parameters = null;
int length = paramTypeNames.length;
char[] tName = CharOperation.concat(declaringTypePackageName,declaringTypeName,'.');
Object cachedType = this.completionEngine.typeCache.get(tName);
IType type = null;
if(cachedType != null) {
if(cachedType != NO_ATTACHED_SOURCE && cachedType instanceof BinaryType) {
type = (BinaryType)cachedType;
}
} else {
NameLookup.Answer answer = this.nameLookup.findType(new String(declaringTypeName),
new String(declaringTypePackageName),
false,
NameLookup.ACCEPT_CLASSES & NameLookup.ACCEPT_INTERFACES,
true,
false,
false,
null);
type = answer == null ? null : answer.type;
if(type instanceof BinaryType){
this.completionEngine.typeCache.put(tName, type);
} else {
type = null;
}
}
if(type != null) {
try {
IMethod method = findMethod(type, selector, paramTypeNames);
if (this.hasNoParameterNamesFromIndex) {
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot)type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (packageFragmentRoot.isArchive() ||
this.completionEngine.openedBinaryTypes < getOpenedBinaryTypesThreshold()) {
SourceMapper mapper = ((JavaElement)method).getSourceMapper();
if (mapper != null) {
char[][] paramNames = mapper.getMethodParameterNames(method);
if(paramNames == null) {
if (!packageFragmentRoot.isArchive()) this.completionEngine.openedBinaryTypes++;
IBinaryType info = (IBinaryType) ((BinaryType) type).getElementInfo();
char[] source = mapper.findSource(type, info);
if (source != null){
mapper.mapSource((NamedMember) type, source, info);
}
paramNames = mapper.getMethodParameterNames(method);
}
if(paramNames != null) {
parameters = paramNames;
}
}
}
} else {
IBinaryMethod info = (IBinaryMethod) ((JavaElement)method).getElementInfo();
char[][] argumentNames = info.getArgumentNames();
if (argumentNames != null && argumentNames.length == length) {
parameters = argumentNames;
return parameters;
}
parameters = new char[length][];
String[] params = method.getParameterNames();
for(int i = 0; i< length ; i++){
parameters[i] = params[i].toCharArray();
}
}
} catch(JavaModelException e){
parameters = null;
}
}
if(parameters == null) {
parameters = CompletionEngine.createDefaultParameterNames(length);
}
return parameters;
}
protected char[][] findMethodParameterNames(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] paramTypeNames){
if(paramTypeNames == null || declaringTypeName == null) return null;
char[][] parameters = null;
int length = paramTypeNames.length;
char[] tName = CharOperation.concat(declaringTypePackageName,declaringTypeName,'.');
Object cachedType = this.completionEngine.typeCache.get(tName);
IType type = null;
if(cachedType != null) {
if(cachedType != NO_ATTACHED_SOURCE && cachedType instanceof BinaryType) {
type = (BinaryType)cachedType;
}
} else {
NameLookup.Answer answer = this.nameLookup.findType(new String(declaringTypeName),
new String(declaringTypePackageName),
false,
NameLookup.ACCEPT_CLASSES & NameLookup.ACCEPT_INTERFACES,
true,
false,
false,
null);
type = answer == null ? null : answer.type;
if(type instanceof BinaryType){
this.completionEngine.typeCache.put(tName, type);
} else {
type = null;
}
}
if(type != null) {
try{
IMethod method = findMethod(type, selector, paramTypeNames);
parameters = new char[length][];
String[] params = method.getParameterNames();
for(int i = 0; i< length ; i++){
parameters[i] = params[i].toCharArray();
}
} catch(JavaModelException e){
parameters = null;
}
}
if(parameters == null) {
parameters = CompletionEngine.createDefaultParameterNames(length);
}
return parameters;
}
private IMethod findMethod(IType type, char[] selector, char[][] paramTypeNames) throws JavaModelException {
IMethod method = null;
int startingIndex = 0;
String[] args;
IType enclosingType = type.getDeclaringType();
if (enclosingType != null
&& CharOperation.equals(type.getElementName().toCharArray(), selector)
&& !Flags.isStatic(type.getFlags())) {
args = new String[paramTypeNames.length+1];
startingIndex = 1;
args[0] = Signature.createTypeSignature(enclosingType.getFullyQualifiedName(), true);
} else {
args = new String[paramTypeNames.length];
}
int length = args.length;
for(int i = startingIndex; i< length ; i++){
args[i] = new String(paramTypeNames[i-startingIndex]);
}
method = type.getMethod(new String(selector), args);
IMethod[] methods = type.findMethods(method);
if (methods != null && methods.length > 0) {
method = methods[0];
}
return method;
}
protected char[] getDeclarationPackageName() {
return this.declarationPackageName;
}
protected char[] getDeclarationTypeName() {
return this.declarationTypeName;
}
private int getOpenedBinaryTypesThreshold() {
return JavaModelManager.getJavaModelManager().getOpenableCacheSize() / 10;
}
protected char[] getModuleName() {
return this.moduleName;
}
protected char[] getPackageName() {
return this.packageName;
}
protected char[] getTypeName() {
return this.typeName;
}
protected char[][] getParameterPackageNames() {
return this.parameterPackageNames;
}
protected char[][] getParameterTypeNames() {
return this.parameterTypeNames;
}
protected void setDeclarationPackageName(char[] declarationPackageName) {
this.declarationPackageName = declarationPackageName;
}
protected void setDeclarationTypeName(char[] declarationTypeName) {
this.declarationTypeName = declarationTypeName;
}
protected void setModuleName(char[] moduleName) {
this.moduleName = moduleName;
}
protected void setPackageName(char[] packageName) {
this.packageName = packageName;
}
protected void setTypeName(char[] typeName) {
this.typeName = typeName;
}
protected void setParameterPackageNames(char[][] parameterPackageNames) {
this.parameterPackageNames = parameterPackageNames;
}
protected void setParameterTypeNames(char[][] parameterTypeNames) {
this.parameterTypeNames = parameterTypeNames;
}
protected void setAccessibility(int kind) {
this.accessibility = kind;
}
protected void setIsContructor(boolean isConstructor) {
this.isConstructor = isConstructor;
}
public void setOriginalSignature(char[] originalSignature) {
this.originalSignature = originalSignature;
}
public InternalCompletionProposal(int kind, int completionLocation) {
if ((kind < FIRST_KIND)
|| (kind > LAST_KIND)) {
throw new IllegalArgumentException();
}
if (this.completion == null || completionLocation < 0) {
if(this.completion == null || completionLocation != -1) {
throw new IllegalArgumentException();
}
completionLocation = 0;
}
this.completionKind = kind;
this.completionLocation = completionLocation;
}
@Override
public int getAdditionalFlags() {
return this.additionalFlags;
}
@Override
public void setAdditionalFlags(int additionalFlags) {
this.additionalFlags = additionalFlags;
}
@Override
public int getKind() {
return this.completionKind;
}
@Override
public int getCompletionLocation() {
return this.completionLocation;
}
@Override
public int getTokenStart() {
return this.tokenStart;
}
@Override
public int getTokenEnd() {
return this.tokenEnd;
}
@Override
public void setTokenRange(int startIndex, int endIndex) {
if (startIndex < 0 || endIndex < startIndex) {
throw new IllegalArgumentException();
}
this.tokenStart = startIndex;
this.tokenEnd = endIndex;
}
@Override
public char[] getCompletion() {
if(this.completionKind == METHOD_DECLARATION) {
findParameterNames(null);
if(this.updateCompletion) {
this.updateCompletion = false;
if(this.parameterNames != null) {
int length = this.parameterNames.length;
StringBuffer completionBuffer = new StringBuffer(this.completion.length);
int start = 0;
int end = CharOperation.indexOf('%', this.completion);
completionBuffer.append(this.completion, start, end - start);
for(int i = 0 ; i < length ; i++){
completionBuffer.append(this.parameterNames[i]);
start = end + 1;
end = CharOperation.indexOf('%', this.completion, start);
if(end > -1){
completionBuffer.append(this.completion, start, end - start);
} else {
completionBuffer.append(this.completion, start, this.completion.length - start);
}
}
int nameLength = completionBuffer.length();
this.completion = new char[nameLength];
completionBuffer.getChars(0, nameLength, this.completion, 0);
}
}
}
return this.completion;
}
@Override
public void setCompletion(char[] completion) {
this.completion = completion;
}
@Override
public int getReplaceStart() {
return this.replaceStart;
}
@Override
public int getReplaceEnd() {
return this.replaceEnd;
}
@Override
public void setReplaceRange(int startIndex, int endIndex) {
if (startIndex < 0 || endIndex < startIndex) {
throw new IllegalArgumentException();
}
this.replaceStart = startIndex;
this.replaceEnd = endIndex;
}
@Override
public int getRelevance() {
return this.relevance;
}
@Override
public void setRelevance(int rating) {
if (rating <= 0) {
throw new IllegalArgumentException();
}
this.relevance = rating;
}
@Override
public char[] getDeclarationSignature() {
return this.declarationSignature;
}
@Override
public char[] getDeclarationKey() {
return this.declarationKey;
}
@Override
public void setDeclarationSignature(char[] signature) {
this.declarationSignature = signature;
}
@Override
public void setDeclarationKey(char[] key) {
this.declarationKey = key;
}
@Override
public char[] getName() {
return this.name;
}
@Override
public void setName(char[] name) {
this.name = name;
}
public Binding getBinding() {
return this.binding;
}
public void setBinding(Binding binding) {
this.binding = binding;
}
@Override
public char[] getSignature() {
return this.signature;
}
@Override
public char[] getKey() {
return this.key;
}
@Override
public void setSignature(char[] signature) {
this.signature = signature;
}
@Override
public void setKey(char[] key) {
this.key = key;
}
@Override
public int getFlags() {
return this.flags;
}
@Override
public void setFlags(int flags) {
this.flags = flags;
}
public void setHasNoParameterNamesFromIndex(boolean hasNoParameterNamesFromIndex) {
this.hasNoParameterNamesFromIndex = hasNoParameterNamesFromIndex;
}
@Override
public CompletionProposal[] getRequiredProposals() {
return this.requiredProposals;
}
@Override
public void setRequiredProposals(CompletionProposal[] proposals) {
this.requiredProposals = proposals;
}
@Override
public char[][] findParameterNames(IProgressMonitor monitor) {
if (!this.parameterNamesComputed) {
this.parameterNamesComputed = true;
switch(this.completionKind) {
case ANONYMOUS_CLASS_DECLARATION:
try {
this.parameterNames = findMethodParameterNames(
this.declarationPackageName,
this.declarationTypeName,
CharOperation.lastSegment(this.declarationTypeName, '.'),
Signature.getParameterTypes(this.originalSignature == null ? this.signature : this.originalSignature));
} catch(IllegalArgumentException e) {
if(this.parameterTypeNames != null) {
this.parameterNames = CompletionEngine.createDefaultParameterNames(this.parameterTypeNames.length);
} else {
this.parameterNames = null;
}
}
break;
case ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
try {
this.parameterNames = findConstructorParameterNames(
this.declarationPackageName,
this.declarationTypeName,
CharOperation.lastSegment(this.declarationTypeName, '.'),
Signature.getParameterTypes(this.originalSignature == null ? this.signature : this.originalSignature));
} catch(IllegalArgumentException e) {
if(this.parameterTypeNames != null) {
this.parameterNames = CompletionEngine.createDefaultParameterNames(this.parameterTypeNames.length);
} else {
this.parameterNames = null;
}
}
break;
case METHOD_REF:
case METHOD_REF_WITH_CASTED_RECEIVER:
try {
this.parameterNames = findMethodParameterNames(
this.declarationPackageName,
this.declarationTypeName,
this.name,
Signature.getParameterTypes(this.originalSignature == null ? this.signature : this.originalSignature));
} catch(IllegalArgumentException e) {
if(this.parameterTypeNames != null) {
this.parameterNames = CompletionEngine.createDefaultParameterNames(this.parameterTypeNames.length);
} else {
this.parameterNames = null;
}
}
break;
case CONSTRUCTOR_INVOCATION:
try {
this.parameterNames = findConstructorParameterNames(
this.declarationPackageName,
this.declarationTypeName,
this.name,
Signature.getParameterTypes(this.originalSignature == null ? this.signature : this.originalSignature));
} catch(IllegalArgumentException e) {
if(this.parameterTypeNames != null) {
this.parameterNames = CompletionEngine.createDefaultParameterNames(this.parameterTypeNames.length);
} else {
this.parameterNames = null;
}
}
break;
case METHOD_DECLARATION:
try {
this.parameterNames = findMethodParameterNames(
this.declarationPackageName,
this.declarationTypeName,
this.name,
Signature.getParameterTypes(this.originalSignature == null ? this.signature : this.originalSignature));
} catch(IllegalArgumentException e) {
if(this.parameterTypeNames != null) {
this.parameterNames = CompletionEngine.createDefaultParameterNames(this.parameterTypeNames.length);
} else {
this.parameterNames = null;
}
}
if(this.parameterNames != null) {
this.updateCompletion = true;
}
break;
}
}
return this.parameterNames;
}
@Override
public void setParameterNames(char[][] parameterNames) {
this.parameterNames = parameterNames;
this.parameterNamesComputed = true;
}
@Override
public int getAccessibility() {
return this.accessibility;
}
@Override
public boolean isConstructor() {
return this.isConstructor;
}
private int receiverStart;
private int receiverEnd;
private char[] receiverSignature;
@Override
public char[] getReceiverSignature() {
return this.receiverSignature;
}
@Override
public int getReceiverStart() {
return this.receiverStart;
}
@Override
public int getReceiverEnd() {
return this.receiverEnd;
}
@Override
public void setReceiverSignature(char[] signature) {
this.receiverSignature = signature;
}
@Override
public void setReceiverRange(int startIndex, int endIndex) {
this.receiverStart = startIndex;
this.receiverEnd = endIndex;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append('[');
switch(this.completionKind) {
case CompletionProposal.ANONYMOUS_CLASS_DECLARATION :
buffer.append("ANONYMOUS_CLASS_DECLARATION");
break;
case CompletionProposal.FIELD_REF :
buffer.append("FIELD_REF");
break;
case CompletionProposal.KEYWORD :
buffer.append("KEYWORD");
break;
case CompletionProposal.LABEL_REF :
buffer.append("LABEL_REF");
break;
case CompletionProposal.LOCAL_VARIABLE_REF :
buffer.append("LOCAL_VARIABLE_REF");
break;
case CompletionProposal.METHOD_DECLARATION :
buffer.append("METHOD_DECLARATION");
if(this.isConstructor) {
buffer.append("<CONSTRUCTOR>");
}
break;
case CompletionProposal.METHOD_REF :
buffer.append("METHOD_REF");
if(this.isConstructor) {
buffer.append("<CONSTRUCTOR>");
}
break;
case CompletionProposal.MODULE_DECLARATION :
buffer.append("MODULE_DECLARATION");
break;
case CompletionProposal.MODULE_REF :
buffer.append("MODULE_REF");
break;
case CompletionProposal.PACKAGE_REF :
buffer.append("PACKAGE_REF");
break;
case CompletionProposal.TYPE_REF :
buffer.append("TYPE_REF");
break;
case CompletionProposal.VARIABLE_DECLARATION :
buffer.append("VARIABLE_DECLARATION");
break;
case CompletionProposal.POTENTIAL_METHOD_DECLARATION :
buffer.append("POTENTIAL_METHOD_DECLARATION");
break;
case CompletionProposal.METHOD_NAME_REFERENCE :
buffer.append("METHOD_NAME_REFERENCE");
break;
case CompletionProposal.ANNOTATION_ATTRIBUTE_REF :
buffer.append("ANNOTATION_ATTRIBUTE_REF");
break;
case CompletionProposal.JAVADOC_BLOCK_TAG :
buffer.append("JAVADOC_BLOCK_TAG");
break;
case CompletionProposal.JAVADOC_INLINE_TAG :
buffer.append("JAVADOC_INLINE_TAG");
break;
case CompletionProposal.JAVADOC_FIELD_REF:
buffer.append("JAVADOC_FIELD_REF");
break;
case CompletionProposal.JAVADOC_METHOD_REF :
buffer.append("JAVADOC_METHOD_REF");
break;
case CompletionProposal.JAVADOC_TYPE_REF :
buffer.append("JAVADOC_TYPE_REF");
break;
case CompletionProposal.JAVADOC_PARAM_REF :
buffer.append("JAVADOC_PARAM_REF");
break;
case CompletionProposal.JAVADOC_VALUE_REF :
buffer.append("JAVADOC_VALUE_REF");
break;
case CompletionProposal.FIELD_IMPORT :
buffer.append("FIELD_IMPORT");
break;
case CompletionProposal.METHOD_IMPORT :
buffer.append("METHOD_IMPORT");
break;
case CompletionProposal.TYPE_IMPORT :
buffer.append("TYPE_IMPORT");
break;
case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER :
buffer.append("METHOD_REF_WITH_CASTED_RECEIVER");
break;
case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER :
buffer.append("FIELD_REF_WITH_CASTED_RECEIVER");
break;
case CompletionProposal.CONSTRUCTOR_INVOCATION :
buffer.append("CONSTRUCTOR_INVOCATION");
break;
case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION :
buffer.append("ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION");
break;
default :
buffer.append("PROPOSAL");
break;
}
buffer.append("]{completion:");
if (this.completion != null) buffer.append(this.completion);
buffer.append(", declSign:");
if (this.declarationSignature != null) buffer.append(this.declarationSignature);
buffer.append(", sign:");
if (this.signature != null) buffer.append(this.signature);
buffer.append(", declKey:");
if (this.declarationKey != null) buffer.append(this.declarationKey);
buffer.append(", key:");
if (this.key != null) buffer.append(this.key);
buffer.append(", name:");
if (this.name != null) buffer.append(this.name);
buffer.append(", replace:[");
buffer.append(this.replaceStart);
buffer.append(',');
buffer.append(this.replaceEnd);
buffer.append("], token:[");
buffer.append(this.tokenStart);
buffer.append(',');
buffer.append(this.tokenEnd);
buffer.append("], relevance:");
buffer.append(this.relevance);
buffer.append('}');
return buffer.toString();
}
@Override
public boolean canUseDiamond(CompletionContext coreContext) {
if (this.getKind() != CONSTRUCTOR_INVOCATION) return false;
if (coreContext instanceof InternalCompletionContext) {
InternalCompletionContext internalCompletionContext = (InternalCompletionContext) coreContext;
if (internalCompletionContext.extendedContext == null) return false;
char[] name1 = this.declarationPackageName;
char[] name2 = this.declarationTypeName;
char[] declarationType = CharOperation.concat(name1, name2, '.');
char[] sign = (this.originalSignature != null)? this.originalSignature : getSignature();
if (!(sign == null || sign.length < 2)) {
sign = Signature.removeCapture(sign);
}
char[][] types= Signature.getParameterTypes(sign);
String[] paramTypeNames= new String[types.length];
for (int i= 0; i < types.length; i++) {
paramTypeNames[i]= new String(Signature.toCharArray(types[i]));
}
return internalCompletionContext.extendedContext.canUseDiamond(paramTypeNames,declarationType);
}
else {
return false;
}
}
}