Java Code Examples for org.eclipse.jdt.core.Flags#isVarargs()

The following examples show how to use org.eclipse.jdt.core.Flags#isVarargs() . 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: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.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 StringBuilder appendUnboundedParameterList(StringBuilder 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;
	try {
		parameterNames = methodProposal.findParameterNames(null);
	} catch (Exception e) {
		JavaLanguageServerPlugin.logException(e.getMessage(), e);
		parameterNames = CompletionEngine.createDefaultParameterNames(Signature.getParameterCount(signature));
		methodProposal.setParameterNames(parameterNames);
	}
	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 2
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static List<ParameterInfo> createParameterInfoList(IMethod method) {
	try {
		String[] typeNames= method.getParameterTypes();
		String[] oldNames= method.getParameterNames();
		List<ParameterInfo> result= new ArrayList<ParameterInfo>(typeNames.length);
		for (int i= 0; i < oldNames.length; i++){
			ParameterInfo parameterInfo;
			if (i == oldNames.length - 1 && Flags.isVarargs(method.getFlags())) {
				String varargSignature= typeNames[i];
				int arrayCount= Signature.getArrayCount(varargSignature);
				String baseSignature= Signature.getElementType(varargSignature);
				if (arrayCount > 1)
					baseSignature= Signature.createArraySignature(baseSignature, arrayCount - 1);
				parameterInfo= new ParameterInfo(Signature.toString(baseSignature) + ParameterInfo.ELLIPSIS, oldNames[i], i);
			} else {
				parameterInfo= new ParameterInfo(Signature.toString(typeNames[i]), oldNames[i], i);
			}
			result.add(parameterInfo);
		}
		return result;
	} catch(JavaModelException e) {
		JavaPlugin.log(e);
		return new ArrayList<ParameterInfo>(0);
	}
}
 
Example 3
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 4
Source File: JavaMethodParameterCodeMining.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
private String getParameterLabel(IMethod method, ITypeBinding calledTypeBinding) throws JavaModelException {
	ParameterMiningLabelBuilder label = new ParameterMiningLabelBuilder();
	if (showType) {
		String paramType = "";
		if (calledTypeBinding.isParameterizedType()) {
			// ex : List<String>
			ITypeBinding typeArgument = calledTypeBinding.getTypeArguments()[parameterIndex];
			paramType = typeArgument.getName();
		} else {
			paramType = method.getParameterTypes()[parameterIndex];
			paramType = Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(paramType)));
			// replace [] with ... when varArgs
			if (parameterIndex == method.getParameterTypes().length - 1 && Flags.isVarargs(method.getFlags())) {
				paramType = paramType.substring(0, paramType.length() - 2) + "...";
			}
		}
		label.addParameterInfo(paramType);
	}
	if (showName) {
		String paramName = method.getParameterNames()[parameterIndex];
		if (!isArgNumber(paramName, method)) {
			label.addParameterInfo(paramName);
		}
	}
	return label.toString();

}
 
Example 5
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 6
Source File: JavaDocLocations.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException {
	buf.append(meth.getElementName());

	/*
	 * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs.
	 * This breaks all clients that directly create such URLs.
	 * We can't know what format is required, so we just guess by the project's compiler compliance.
	 */
	boolean is18OrHigher = JavaModelUtil.is18OrHigher(meth.getJavaProject());
	buf.append(is18OrHigher ? '-' : '(');
	String[] params = meth.getParameterTypes();
	IType declaringType = meth.getDeclaringType();
	boolean isVararg = Flags.isVarargs(meth.getFlags());
	int lastParam = params.length - 1;
	for (int i = 0; i <= lastParam; i++) {
		if (i != 0) {
			buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String curr = Signature.getTypeErasure(params[i]);
		String fullName = JavaModelUtil.getResolvedTypeName(curr, declaringType);
		if (fullName == null) { // e.g. a type parameter "QE;"
			fullName = Signature.toString(Signature.getElementType(curr));
		}
		if (fullName != null) {
			buf.append(fullName);
			int dim = Signature.getArrayCount(curr);
			if (i == lastParam && isVararg) {
				dim--;
			}
			while (dim > 0) {
				buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$
				dim--;
			}
			if (i == lastParam && isVararg) {
				buf.append("..."); //$NON-NLS-1$
			}
		}
	}
	buf.append(is18OrHigher ? '-' : ')');
}
 
Example 7
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException {
	buf.append(meth.getElementName());

	/*
	 * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs.
	 * This breaks all clients that directly create such URLs.
	 * We can't know what format is required, so we just guess by the project's compiler compliance.
	 */
	boolean is18OrHigher= JavaModelUtil.is18OrHigher(meth.getJavaProject());
	buf.append(is18OrHigher ? '-' : '(');
	String[] params= meth.getParameterTypes();
	IType declaringType= meth.getDeclaringType();
	boolean isVararg= Flags.isVarargs(meth.getFlags());
	int lastParam= params.length - 1;
	for (int i= 0; i <= lastParam; i++) {
		if (i != 0) {
			buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String curr= Signature.getTypeErasure(params[i]);
		String fullName= JavaModelUtil.getResolvedTypeName(curr, declaringType);
		if (fullName == null) { // e.g. a type parameter "QE;"
			fullName= Signature.toString(Signature.getElementType(curr));
		}
		if (fullName != null) {
			buf.append(fullName);
			int dim= Signature.getArrayCount(curr);
			if (i == lastParam && isVararg) {
				dim--;
			}
			while (dim > 0) {
				buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$
				dim--;
			}
			if (i == lastParam && isVararg) {
				buf.append("..."); //$NON-NLS-1$
			}
		}
	}
	buf.append(is18OrHigher ? '-' : ')');
}
 
Example 8
Source File: BinaryMethod.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void toStringName(StringBuffer buffer, int flags) {
	buffer.append(getElementName());
	buffer.append('(');
	String[] parameters = getParameterTypes();
	int length;
	if (parameters != null && (length = parameters.length) > 0) {
		boolean isVarargs = Flags.isVarargs(flags);
		for (int i = 0; i < length; i++) {
			try {
				if (i < length - 1) {
					buffer.append(Signature.toString(parameters[i]));
					buffer.append(", "); //$NON-NLS-1$
				} else if (isVarargs) {
					// remove array from signature
					String parameter = parameters[i].substring(1);
					buffer.append(Signature.toString(parameter));
					buffer.append(" ..."); //$NON-NLS-1$
				} else {
					buffer.append(Signature.toString(parameters[i]));
				}
			} catch (IllegalArgumentException e) {
				// parameter signature is malformed
				buffer.append("*** invalid signature: "); //$NON-NLS-1$
				buffer.append(parameters[i]);
			}
		}
	}
	buffer.append(')');
	if (this.occurrenceCount > 1) {
		buffer.append("#"); //$NON-NLS-1$
		buffer.append(this.occurrenceCount);
	}
}
 
Example 9
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the formal parameters for the given operation.
 *
 * @param parameterBuilder the code builder.
 * @param operation the operation that describes the formal parameters.
 * @return the parameters.
 * @throws JavaModelException if the Java model is invalid.
 * @throws IllegalArgumentException if the signature is not syntactically correct.
 */
protected IFormalParameterBuilder[] createFormalParametersWith(
		ParameterBuilder parameterBuilder,
		IMethod operation) throws JavaModelException, IllegalArgumentException {
	final boolean isVarargs = Flags.isVarargs(operation.getFlags());
	final ILocalVariable[] rawParameters = operation.getParameters();
	final FormalParameterProvider parameters = getFormalParameterProvider(operation);
	final int len = parameters.getFormalParameterCount();
	final IFormalParameterBuilder[] paramBuilders = new IFormalParameterBuilder[len];
	for (int i = 0; i < len; ++i) {
		final ILocalVariable rawParameter = rawParameters[i];
		final IAnnotation annotation = getAnnotation(rawParameter, DefaultValue.class.getName());
		final String defaultValue = (annotation != null) ? extractDefaultValue(operation, annotation) : null;
		final boolean isV = isVarargs && i == len - 1;
		String type = parameters.getFormalParameterType(i, isV);
		if (isV && type.endsWith("[]")) { //$NON-NLS-1$
			type = type.substring(0, type.length() - 2);
		}
		final IFormalParameterBuilder sarlParameter = parameterBuilder.addParameter(parameters.getFormalParameterName(i));
		sarlParameter.setParameterType(type);
		if (defaultValue != null) {
			sarlParameter.getDefaultValue().setExpression(defaultValue);
		}
		if (isV) {
			sarlParameter.setVarArg(true);
		}
		paramBuilders[i] = sarlParameter;
	}
	return paramBuilders;
}
 
Example 10
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isVarargs(IMethod method) throws JavaModelException{
	return Flags.isVarargs(method.getFlags());
}
 
Example 11
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isVarargs(IMethod method) throws JavaModelException{
	return Flags.isVarargs(method.getFlags());
}