Java Code Examples for org.eclipse.jdt.core.Signature#getParameterTypes()

The following examples show how to use org.eclipse.jdt.core.Signature#getParameterTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Disassembler.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private int getLocalIndex(final int startingSlot, final int index, final char[] methodDescriptor) {
	int slot = startingSlot;
	final char[][] types = Signature.getParameterTypes(methodDescriptor);
	for (int i = 0; i < index; i++) {
		final char[] type = types[i];
		switch(type.length) {
			case 1 :
				switch(type[0]) {
					case 'D' :
					case 'J' :
						slot += 2;
						break;
					default :
						slot++;
				}
				break;
			default :
				slot++;
		}
	}
	return slot;
}
 
Example 2
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IMethod findJavaMethod(IType type) {
	// Locate the method from its signature.
	//
	String[] parameterTypes = Signature.getParameterTypes(new BindingKey("Lx;.x(" + signature + ")").toSignature());
	IMethod javaMethod = type.getMethod(name, parameterTypes);

	// If the method doesn't exist and this is a nested type...
	//
	if (!javaMethod.exists() && type.getDeclaringType() != null) {
		// This special case handles what appears to be a JDT bug 
		// that sometimes it knows when there are implicit constructor arguments for nested types and sometimes it doesn't.
		// Infer one more initial parameter type and locate the method based on that.
		//
		String[] augmented = new String[parameterTypes.length + 1];
		System.arraycopy(parameterTypes, 0, augmented, 1, parameterTypes.length);
		String first = Signature.createTypeSignature(type.getDeclaringType().getFullyQualifiedName(), true);
		augmented[0] = first;
		javaMethod = type.getMethod(name, augmented);
	}
	return javaMethod;
}
 
Example 3
Source File: SignatureUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Takes a method signature
 * <code>[&lt; typeVariableName : formalTypeDecl &gt;] ( paramTypeSig1* ) retTypeSig</code>
 * and returns it with any parameter signatures filtered through
 * <code>getLowerBound</code> and the return type filtered through
 * <code>getUpperBound</code>. Any preceding formal type variable
 * declarations are removed.
 * <p>
 * TODO this is a temporary workaround for
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=83600
 * </p>
 *
 * @param signature the method signature to convert
 * @return the signature with no bounded types
 */
public static char[] unboundedSignature(char[] signature) {
	if (signature == null || signature.length < 2)
		return signature;

	final boolean BUG_83600= true;
	// XXX the signatures from CompletionRequestor contain a superfluous '+'
	// before type parameters to parameter types
	if (BUG_83600) {
		signature= fix83600(signature);
	}

	StringBuffer res= new StringBuffer("("); //$NON-NLS-1$
	char[][] parameters= Signature.getParameterTypes(signature);
	for (int i= 0; i < parameters.length; i++) {
		char[] param= parameters[i];
		res.append(getLowerBound(param));
	}
	res.append(')');
	res.append(getUpperBound(Signature.getReturnType(signature)));
	return res.toString().toCharArray();
}
 
Example 4
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Appends the parameter list to <code>buffer</code>.
 *
 * @param buffer the buffer to append to
 * @param methodProposal the method proposal
 * @return the modified <code>buffer</code>
 */
private StyledString appendUnboundedParameterList(StyledString buffer, CompletionProposal methodProposal) {
	// TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
	// gets fixed.
	char[] signature= SignatureUtil.fix83600(methodProposal.getSignature());
	char[][] parameterNames= methodProposal.findParameterNames(null);
	char[][] parameterTypes= Signature.getParameterTypes(signature);

	for (int i= 0; i < parameterTypes.length; i++)
		parameterTypes[i]= createTypeDisplayName(SignatureUtil.getLowerBound(parameterTypes[i]));

	if (Flags.isVarargs(methodProposal.getFlags())) {
		int index= parameterTypes.length - 1;
		parameterTypes[index]= convertToVararg(parameterTypes[index]);
	}
	return appendParameterSignature(buffer, parameterTypes, parameterNames);
}
 
Example 5
Source File: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resolves the member described by the receiver and returns it if found.
 * Returns <code>null</code> if no corresponding member can be found.
 *
 * @return the resolved member or <code>null</code> if none is found
 * @throws JavaModelException if accessing the java model fails
 */
@Override
protected IMember resolveMember() throws JavaModelException {
	char[] declarationSignature= fProposal.getDeclarationSignature();
	String typeName= SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type= fJavaProject.findType(typeName);
	if (type != null) {
		String name= String.valueOf(fProposal.getName());
		String[] parameters= Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(fProposal.getSignature())));
		for (int i= 0; i < parameters.length; i++) {
			parameters[i]= SignatureUtil.getLowerBound(parameters[i]);
		}
		boolean isConstructor= fProposal.isConstructor();

		return findMethod(name, parameters, isConstructor, type);
	}

	return null;
}
 
Example 6
Source File: JavaElementFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void consumeMethod(char[] selector, char[] signature) {
	if (!(this.element instanceof IType)) return;
	String[] parameterTypes = Signature.getParameterTypes(new String(signature));
	IType type = (IType) this.element;
	IMethod method = type.getMethod(new String(selector), parameterTypes);
	IMethod[] methods = type.findMethods(method);
	if (methods.length > 0)
		this.element = methods[0];
}
 
Example 7
Source File: KeyToSignature.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void consumeParameterizedGenericMethod() {
	this.typeArguments = this.arguments;
	int typeParametersSize = this.arguments.size();
	if (typeParametersSize > 0) {
		int sigLength = this.signature.length();
		char[] methodSignature = new char[sigLength];
		this.signature.getChars(0, sigLength, methodSignature, 0);
		char[][] typeParameterSigs = Signature.getTypeParameters(methodSignature);
		if (typeParameterSigs.length != typeParametersSize)
			return;
		this.signature = new StringBuffer();

		// type parameters
		for (int i = 0; i < typeParametersSize; i++)
			typeParameterSigs[i] = CharOperation.concat(Signature.C_TYPE_VARIABLE,Signature.getTypeVariable(typeParameterSigs[i]), Signature.C_SEMICOLON);
		int paramStart = CharOperation.indexOf(Signature.C_PARAM_START, methodSignature);
		char[] typeParametersString = CharOperation.subarray(methodSignature, 0, paramStart);
		this.signature.append(typeParametersString);

		// substitute parameters
		this.signature.append(Signature.C_PARAM_START);
		char[][] parameters = Signature.getParameterTypes(methodSignature);
		for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++)
			substitute(parameters[i], typeParameterSigs, typeParametersSize);
		this.signature.append(Signature.C_PARAM_END);

		// substitute return type
		char[] returnType = Signature.getReturnType(methodSignature);
		substitute(returnType, typeParameterSigs, typeParametersSize);

		// substitute exceptions
		char[][] exceptions = Signature.getThrownExceptionTypes(methodSignature);
		for (int i = 0, exceptionsLength = exceptions.length; i < exceptionsLength; i++) {
			this.signature.append(Signature.C_EXCEPTION_START);
			substitute(exceptions[i], typeParameterSigs, typeParametersSize);
		}

	}
}
 
Example 8
Source File: MatchLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
IMethod createBinaryMethodHandle(IType type, char[] methodSelector, char[][] argumentTypeNames) {
	ClassFileReader reader = MatchLocator.classFileReader(type);
	if (reader != null) {
		IBinaryMethod[] methods = reader.getMethods();
		if (methods != null) {
			int argCount = argumentTypeNames == null ? 0 : argumentTypeNames.length;
			nextMethod : for (int i = 0, methodsLength = methods.length; i < methodsLength; i++) {
				IBinaryMethod binaryMethod = methods[i];
				char[] selector = binaryMethod.isConstructor() ? type.getElementName().toCharArray() : binaryMethod.getSelector();
				if (CharOperation.equals(selector, methodSelector)) {
					char[] signature = binaryMethod.getGenericSignature();
					if (signature == null) signature = binaryMethod.getMethodDescriptor();
					char[][] parameterTypes = Signature.getParameterTypes(signature);
					if (argCount != parameterTypes.length) continue nextMethod;
					if (argumentTypeNames != null) {
						for (int j = 0; j < argCount; j++) {
							char[] parameterTypeName = ClassFileMatchLocator.convertClassFileFormat(parameterTypes[j]);
							if (!CharOperation.endsWith(Signature.toCharArray(Signature.getTypeErasure(parameterTypeName)), CharOperation.replaceOnCopy(argumentTypeNames[j], '$', '.')))
								continue nextMethod;
							parameterTypes[j] = parameterTypeName;
						}
					}
					return (IMethod) createMethodHandle(type, new String(selector), CharOperation.toStrings(parameterTypes));
				}
			}
		}
	}
	return null;
}
 
Example 9
Source File: InternalCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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, '.');  // fully qualified name
		// even if the type arguments used in the method have been substituted,
		// extract the original type arguments only, since thats what we want to compare with the class
		// type variables (Substitution might have happened when the constructor is coming from another
		// CU and not the current one).
		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;
	}
}
 
Example 10
Source File: CompletionRequestorWrapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private char[][] getParameterTypes(char[] methodSignature) {
	char[][] parameterQualifiedTypes = Signature.getParameterTypes(methodSignature);
	int length = parameterQualifiedTypes == null ? 0 : parameterQualifiedTypes.length;
	char[][] parameterPackages = new char[length][];
	for(int i = 0; i < length; i++) {
		parameterPackages[i] = Signature.getSignatureSimpleName(parameterQualifiedTypes[i]);
	}

	return parameterPackages;
}
 
Example 11
Source File: CompletionRequestorWrapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private char[][] getParameterPackages(char[] methodSignature) {
	char[][] parameterQualifiedTypes = Signature.getParameterTypes(methodSignature);
	int length = parameterQualifiedTypes == null ? 0 : parameterQualifiedTypes.length;
	char[][] parameterPackages = new char[length][];
	for(int i = 0; i < length; i++) {
		parameterPackages[i] = Signature.getSignatureQualifier(parameterQualifiedTypes[i]);
	}

	return parameterPackages;
}
 
Example 12
Source File: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if a method equals to the given signature. Parameter types are only
 * compared by the simple name, no resolving for the fully qualified type
 * name is done. Constructors are only compared by parameters, not the name.
 *
 * @param name Name of the method
 * @param paramTypes The type signatures of the parameters e.g.
 *        <code>{"QString;","I"}</code>
 * @param isConstructor Specifies if the method is a constructor
 * @param method the method to be compared with this info's method
 * @param typeVariables a map from type variables to types
 * @param type the given type that declares the method
 * @return Returns <code>true</code> if the method has the given name and
 *         parameter types and constructor state.
 * @throws JavaModelException if the method does not exist or if an exception occurs while accessing its corresponding resource
 */
private boolean isSameMethodSignature(String name, String[] paramTypes, boolean isConstructor, IMethod method, Map<String, char[]> typeVariables, IType type) throws JavaModelException {
	if (isConstructor || name.equals(method.getElementName())) {
		if (isConstructor == method.isConstructor()) {
			String[] otherParams= method.getParameterTypes(); // types may be type variables
			boolean isBinaryConstructorForNonStaticMemberClass=
					method.isBinary()
					&& type.isMember()
					&& !Flags.isStatic(type.getFlags());
			int syntheticParameterCorrection= isBinaryConstructorForNonStaticMemberClass && paramTypes.length == otherParams.length - 1 ? 1 : 0;
			if (paramTypes.length == otherParams.length - syntheticParameterCorrection) {
				fFallbackMatch= method;
				String signature= method.getSignature();
				String[] otherParamsFromSignature= Signature.getParameterTypes(signature); // types are resolved / upper-bounded
				// no need to check method type variables since these are
				// not yet bound when proposing a method
				for (int i= 0; i < paramTypes.length; i++) {
					String ourParamName= computeSimpleTypeName(paramTypes[i], typeVariables);
					String otherParamName1= computeSimpleTypeName(otherParams[i + syntheticParameterCorrection], typeVariables);
					String otherParamName2= computeSimpleTypeName(otherParamsFromSignature[i + syntheticParameterCorrection], typeVariables);

					if (!ourParamName.equals(otherParamName1) && !ourParamName.equals(otherParamName2)) {
						return false;
					}
				}
				return true;
			}
		}
	}
	return false;
}
 
Example 13
Source File: ParameterGuessingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String[] getParameterTypes() {
	char[] signature= SignatureUtil.fix83600(fProposal.getSignature());
	char[][] types= Signature.getParameterTypes(signature);

	String[] ret= new String[types.length];
	for (int i= 0; i < types.length; i++) {
		ret[i]= new String(Signature.toCharArray(types[i]));
	}
	return ret;
}
 
Example 14
Source File: ParameterGuessingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaElement[][] getAssignableElements() {
	char[] signature= SignatureUtil.fix83600(getProposal().getSignature());
	char[][] types= Signature.getParameterTypes(signature);

	IJavaElement[][] assignableElements= new IJavaElement[types.length][];
	for (int i= 0; i < types.length; i++) {
		assignableElements[i]= fCoreContext.getVisibleElements(new String(types[i]));
	}
	return assignableElements;
}
 
Example 15
Source File: JsniCompletionProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private static String getParamTypesSignature(CompletionProposal proposal) {
  String[] paramTypes = Signature.getParameterTypes(new String(
      proposal.getSignature()));

  // JSNI refs must use /'s to separate qualifier segments
  return StringUtilities.join(paramTypes, "").replace('.', '/');
}
 
Example 16
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Resolves the method described by the receiver and returns it if found.
 * Returns <code>null</code> if no corresponding member can be found.
 *
 * @param proposal
 *            - completion proposal
 * @param javaProject
 *            - Java project
 *
 * @return the resolved method or <code>null</code> if none is found
 * @throws JavaModelException
 *             if accessing the java model fails
 */

public static IMethod resolveMethod(CompletionProposal proposal, IJavaProject javaProject) throws JavaModelException {
	char[] declarationSignature = proposal.getDeclarationSignature();
	String typeName = SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type = javaProject.findType(typeName);
	if (type != null) {
		String name = String.valueOf(proposal.getName());
		if (proposal.getKind() == CompletionProposal.ANNOTATION_ATTRIBUTE_REF) {
			IMethod method = type.getMethod(name, CharOperation.NO_STRINGS);
			if (method.exists()) {
				return method;
			} else {
				return null;
			}
		}
		char[] signature = proposal.getSignature();
		if (proposal instanceof InternalCompletionProposal) {
			Binding binding = ((InternalCompletionProposal) proposal).getBinding();
			if (binding instanceof MethodBinding) {
				MethodBinding methodBinding = (MethodBinding) binding;
				MethodBinding original = methodBinding.original();
				if (original != binding) {
					signature = Engine.getSignature(original);
				}
			}
		}
		String[] parameters = Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(signature)));
		for (int i = 0; i < parameters.length; i++) {
			parameters[i] = SignatureUtil.getLowerBound(parameters[i]);
		}
		boolean isConstructor = proposal.isConstructor();

		return JavaModelUtil.findMethod(name, parameters, isConstructor, type);
	}

	return null;
}
 
Example 17
Source File: SignatureHelpRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public SignatureInformation toSignatureInformation(CompletionProposal methodProposal) {
	SignatureInformation $ = new SignatureInformation();
	StringBuilder desription = descriptionProvider.createMethodProposalDescription(methodProposal);
	$.setLabel(desription.toString());
	$.setDocumentation(this.computeJavaDoc(methodProposal));

	char[] signature = SignatureUtil.fix83600(methodProposal.getSignature());
	char[][] parameterNames = methodProposal.findParameterNames(null);
	char[][] parameterTypes = Signature.getParameterTypes(signature);

	for (int i = 0; i < parameterTypes.length; i++) {
		parameterTypes[i] = Signature.getSimpleName(Signature.toCharArray(SignatureUtil.getLowerBound(parameterTypes[i])));
	}

	if (Flags.isVarargs(methodProposal.getFlags())) {
		int index = parameterTypes.length - 1;
		parameterTypes[index] = convertToVararg(parameterTypes[index]);
	}

	List<ParameterInformation> parameterInfos = new LinkedList<>();
	for (int i = 0; i < parameterTypes.length; i++) {
		StringBuilder builder = new StringBuilder();
		builder.append(parameterTypes[i]);
		builder.append(' ');
		builder.append(parameterNames[i]);

		parameterInfos.add(new ParameterInformation(builder.toString()));
	}

	$.setParameters(parameterInfos);

	return $;
}
 
Example 18
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private IJavaElement[][] getAssignableElements(CompletionProposal proposal) {
	char[] signature = SignatureUtil.fix83600(proposal.getSignature());
	char[][] types = Signature.getParameterTypes(signature);

	IJavaElement[][] assignableElements = new IJavaElement[types.length][];
	for (int i = 0; i < types.length; i++) {
		assignableElements[i] = context.getVisibleElements(new String(types[i]));
	}
	return assignableElements;
}
 
Example 19
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Originally copied from
 * org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.getParameterTypes()
 */
private String[] getParameterTypes(CompletionProposal proposal) {
	char[] signature = SignatureUtil.fix83600(proposal.getSignature());
	char[][] types = Signature.getParameterTypes(signature);

	String[] ret = new String[types.length];
	for (int i = 0; i < types.length; i++) {
		ret[i] = new String(Signature.toCharArray(types[i]));
	}
	return ret;
}
 
Example 20
Source File: JavaModelSearch.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean methodSignaturesEqual(IType type2, String methodName,
    String[] paramTypes, boolean isConstructor, IMethod method2) {
  try {
    // Method names must match, unless we're comparing constructors
    if (!isConstructor && !method2.getElementName().equals(methodName)) {
      return false;
    }

    // Only compare ctors to ctors and methods to methods
    if (isConstructor != method2.isConstructor()) {
      return false;
    }

    // Parameter count must match
    String signature2 = method2.getSignature();
    String[] paramTypes2 = Signature.getParameterTypes(signature2);
    if (paramTypes.length != paramTypes2.length) {
      return false;
    }

    // Compare each parameter type
    for (int i = 0; i < paramTypes.length; i++) {
      String paramType = paramTypes[i];
      String paramType2 = paramTypes2[i];

      // Compare array nesting depth ([] = 1, [][] = 2, etc.)
      if (Signature.getArrayCount(paramType) != Signature.getArrayCount(paramType2)) {
        return false;
      }

      // Remove any array nesting and generic type parameters
      paramType = getBaseType(paramType);
      paramType2 = getBaseType(paramType2);

      // Extract the full type names from the signatures
      String paramTypeName = getQualifiedTypeName(paramType);
      String paramTypeName2 = getQualifiedTypeName(paramType2);

      if (isTypeParameter(method2, paramTypeName2)) {
        // Skip parameters whose type is a generic type parameter of the
        // method we're comparing against, or the method's containing class
        continue;

        /*
         * TODO: we're currently not checking the bounds of generic type
         * parameters, so sometimes we may return true here even when the
         * caller's method signature doesn't match the method we're comparing
         * against. We could try to add that logic here, or better still, we
         * could integrate TypeOracle and take advantage of its type searching
         * capabilities.
         */
      }

      // If we run into an unresolved parameter type in the method we're
      // searching, we'll need to resolve that before doing the comparison
      if (paramType2.charAt(0) == Signature.C_UNRESOLVED) {
        paramTypeName2 = resolveTypeName(type2, paramTypeName2);
      }

      // Finally, compare the type names
      if (!paramTypeName.equals(paramTypeName2)) {
        return false;
      }
    }

    // We passed all the checks, so the signatures are equal
    return true;

  } catch (JavaModelException e) {
    CorePluginLog.logError(e,
        "Error comparing method signatures of {0} and {1}", methodName,
        method2.getElementName());
    return false;
  }
}