Java Code Examples for org.eclipse.jdt.core.IMethod#getParameterNames()

The following examples show how to use org.eclipse.jdt.core.IMethod#getParameterNames() . 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: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
	int nParams= binding.getParameterTypes().length;
	if (nParams > 0) {
		try {
			IMethod method= (IMethod)binding.getMethodDeclaration().getJavaElement();
			if (method != null) {
				String[] parameterNames= method.getParameterNames();
				if (parameterNames.length == nParams) {
					return suggestArgumentNamesWithProposals(project, parameterNames);
				}
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	String[][] names= new String[nParams][];
	for (int i= 0; i < names.length; i++) {
		names[i]= new String[] { "arg" + i }; //$NON-NLS-1$
	}
	return names;
}
 
Example 2
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected String resolve(TemplateContext context) {
	IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
	if (element == null)
		return null;

	IMethod method= (IMethod) element;

	try {
		String[] arguments= method.getParameterNames();
		StringBuffer buffer= new StringBuffer();

		for (int i= 0; i < arguments.length; i++) {
			if (i > 0)
				buffer.append(", "); //$NON-NLS-1$
			buffer.append(arguments[i]);
		}

		return buffer.toString();

	} catch (JavaModelException e) {
		return null;
	}
}
 
Example 3
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 4
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Proposes a JSNI method of the form
 * <code>this.property[x] = newPropertyValue</code> if the java method has two
 * parameters and the first is an integral type.
 */
private void maybeProposeIndexedPropertyWrite(IJavaProject project,
    IMethod method, String propertyName, int invocationOffset,
    int indentationUnits, boolean isStatic,
    List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {
  String[] parameterNames = method.getParameterNames();
  if (parameterNames.length != 2) {
    return;
  }

  String indexParameterType = method.getParameterTypes()[0];
  if (isIndexType(indexParameterType)) {
    String expression = createJsIndexedPropertyWriteExpression(propertyName,
        parameterNames[0], parameterNames[1], isStatic);
    String code = createJsniBlock(project, expression, indentationUnits);
    proposals.add(createProposal(method.getFlags(), code, invocationOffset,
        numCharsFilled, numCharsToOverwrite, expression));
  }
}
 
Example 5
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Proposes a getter that is assumed to delegate to a method with the same
 * name as the java method.
 */
private void proposeGetterDelegate(IJavaProject project, IMethod method,
    int invocationOffset, int indentationUnits, boolean isStatic,
    List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {
  String methodName = method.getElementName();
  String[] parameterNames = method.getParameterNames();
  String expression = "return "
      + createJsMethodInvocationExpression(methodName, isStatic,
          parameterNames);
  String code = createJsniBlock(project, expression, indentationUnits);
  proposals.add(createProposal(method.getFlags(), code, invocationOffset,
      numCharsFilled, numCharsToOverwrite, expression));
}
 
Example 6
Source File: BrowseWriteToJavaDialog.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSetterOrDataSelected() {
	if (selection.getFirstElement() instanceof IMethod) {
		IMethod method = (IMethod)selection.getFirstElement();
		try {
			return method.getParameterNames().length == 1;
		} catch (Exception ex) {
			BonitaStudioLog.error(ex);
			return false;
		}
	} else if (selection.getFirstElement() instanceof String) {
		return true;
	} else {
		return false;
	}
}
 
Example 7
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) {
	int nParams= binding.getParameterTypes().length;

	if (nParams > 0) {
		try {
			IMethod method= (IMethod)binding.getMethodDeclaration().getJavaElement();
			if (method != null) {
				String[] paramNames= method.getParameterNames();
				if (paramNames.length == nParams) {
					String[] namesArray= EMPTY;
					ArrayList<String> newNames= new ArrayList<String>(paramNames.length);
					// Ensure that the code generation preferences are respected
					for (int i= 0; i < paramNames.length; i++) {
						String curr= paramNames[i];
						String baseName= NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, method.getJavaProject());
						if (!curr.equals(baseName)) {
							// make the existing name the favorite
							newNames.add(curr);
						} else {
							newNames.add(suggestArgumentName(project, curr, namesArray));
						}
						namesArray= newNames.toArray(new String[newNames.size()]);
					}
					return namesArray;
				}
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	String[] names= new String[nParams];
	for (int i= 0; i < names.length; i++) {
		names[i]= "arg" + i; //$NON-NLS-1$
	}
	return names;
}
 
Example 8
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
	if (binding == null)
		return null;

	ITypeBinding[] parameterTypes= binding.getParameterTypes();
	if (parameterTypes.length != arguments.size()) // beware of guessed method bindings
		return null;

	int index= arguments.indexOf(assignedExpression);
	if (index == -1)
		return null;

	ITypeBinding expressionBinding= assignedExpression.resolveTypeBinding();
	if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
		return null;

	try {
		IJavaElement javaElement= binding.getJavaElement();
		if (javaElement instanceof IMethod) {
			IMethod method= (IMethod)javaElement;
			if (method.getOpenable().getBuffer() != null) { // avoid dummy names and lookup from Javadoc
				String[] parameterNames= method.getParameterNames();
				if (index < parameterNames.length) {
					return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
				}
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return null;
}
 
Example 9
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private void proposeSetters(IJavaProject project, IMethod method,
    int invocationOffset, int indentationUnits, boolean isStatic,
    List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {

  proposeSetterDelegate(project, method, invocationOffset, indentationUnits,
      isStatic, proposals, numCharsFilled, numCharsToOverwrite);

  String[] parameterNames = method.getParameterNames();
  String methodName = method.getElementName();
  String propertyName = methodName;
  if (methodName.startsWith("set")) {
    propertyName = computePropertyNameFromAccessorMethodName("set",
        methodName);
    if (propertyName.length() > 0) {
      String expression = createJsMethodInvocationExpression(propertyName,
          isStatic, parameterNames);
      String code = createJsniBlock(project, expression, indentationUnits);
      proposals.add(createProposal(method.getFlags(), code, invocationOffset,
          numCharsFilled, numCharsToOverwrite, expression));
    }
  }

  maybeProposePropertyWrite(project, method, propertyName, invocationOffset,
      indentationUnits, isStatic, proposals,
      numCharsFilled, numCharsToOverwrite);

  maybeProposeIndexedPropertyWrite(project, method, propertyName,
      invocationOffset, indentationUnits, isStatic, proposals,
      numCharsFilled, numCharsToOverwrite);
}
 
Example 10
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Proposes a setter that is assumed to delegate to a method with the same
 * name as the java method.
 */
private void proposeSetterDelegate(IJavaProject project, IMethod method,
    int invocationOffset, int indentationUnits, boolean isStatic,
    List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {
  String[] parameterNames = method.getParameterNames();
  String expression = createJsMethodInvocationExpression(
      method.getElementName(), isStatic, parameterNames);
  String code = createJsniBlock(project, expression, indentationUnits);
  proposals.add(createProposal(method.getFlags(), code, invocationOffset,
      numCharsFilled, numCharsToOverwrite, expression));
}
 
Example 11
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private void proposeGetters(IJavaProject project, IMethod method,
    int invocationOffset, int indentationUnits, boolean isStatic,
    List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {

  proposeGetterDelegate(project, method, invocationOffset, indentationUnits,
      isStatic, proposals, numCharsFilled, numCharsToOverwrite);

  // Maybe propose bean-style getter.
  String methodName = method.getElementName();
  String propertyName = methodName;
  if (methodName.startsWith("get")) {
    propertyName = computePropertyNameFromAccessorMethodName("get",
        methodName);
  } else if (methodName.startsWith("is")) {
    propertyName = computePropertyNameFromAccessorMethodName("is", methodName);
  }

  String[] parameterNames = method.getParameterNames();
  if (propertyName != methodName && propertyName.length() > 0) {
    String expression = "return "
        + createJsMethodInvocationExpression(propertyName, isStatic,
            parameterNames);
    String code = createJsniBlock(project, expression, indentationUnits);
    proposals.add(createProposal(method.getFlags(), code, invocationOffset,
        numCharsFilled, numCharsToOverwrite, expression));
  }

  maybeProposePropertyRead(project, method, propertyName, invocationOffset,
      indentationUnits, isStatic, proposals,
      numCharsFilled, numCharsToOverwrite);

  maybeProposeIndexedPropertyRead(project, method, invocationOffset,
      indentationUnits, proposals, propertyName, parameterNames, isStatic,
      numCharsFilled, numCharsToOverwrite);
}
 
Example 12
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 13
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Proposes a JSNI method of the form
 * <code>this.property = newPropertyValue</code> if the java method has a
 * single parameter.
 */
private void maybeProposePropertyWrite(IJavaProject project, IMethod method,
    String propertyName, int invocationOffset, int indentationUnits,
    boolean isStatic, List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {
  String[] parameterNames = method.getParameterNames();
  if (parameterNames.length == 1 && propertyName.length() > 0) {
    String expression = createJsPropertyWriteExpression(propertyName,
        parameterNames[0], isStatic);

    String code = createJsniBlock(project, expression, indentationUnits);
    proposals.add(createProposal(method.getFlags(), code, invocationOffset,
        numCharsFilled, numCharsToOverwrite, expression));
  }
}
 
Example 14
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Proposes a JSNI method of the form <code>return this.property</code> if the
 * java method has no parameters.
 */
private void maybeProposePropertyRead(IJavaProject project, IMethod method,
    String propertyName, int invocationOffset, int indentationUnits,
    boolean isStatic, List<ICompletionProposal> proposals, int numCharsFilled, 
    int numCharsToOverwrite) throws JavaModelException {
  String[] parameterNames = method.getParameterNames();
  if (parameterNames.length == 0 && propertyName.length() > 0) {
    String expression = "return "
        + createJsPropertyReadExpression(propertyName, isStatic);

    String code = createJsniBlock(project, expression, indentationUnits);
    proposals.add(createProposal(method.getFlags(), code, invocationOffset,
        numCharsFilled, numCharsToOverwrite, expression));
  }
}
 
Example 15
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected void setParameterNames(IMethod javaMethod, List<JvmFormalParameter> parameters)
		throws JavaModelException {
	String[] parameterNames = javaMethod.getParameterNames();
	int size = parameters.size();
	if (size != parameterNames.length) {
		throw new IllegalStateException("unmatching arity for java method "+javaMethod.toString()+" and "+getExecutable().getIdentifier());
	}
	for (int i = 0; i < parameterNames.length; i++) {
		String string = parameterNames[i];
		parameters.get(i).setName(string);
	}
}
 
Example 16
Source File: JavaSetterContentProvider.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean hasChildren(final Object element) {
    if (element instanceof IMethod) {
        final IMethod method = (IMethod) element;
        try {
            return method.getParameterNames().length == 0;
        } catch (final Exception ex) {
            BonitaStudioLog.error(ex);
            return true;
        }
    }
    return true;
}
 
Example 17
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Constructor.
 * @param operation the operation.
 * @throws JavaModelException if the parameters cannot be retreived.
 */
JdtFormalParameterList(IMethod operation) throws JavaModelException {
	this.nb = operation.getNumberOfParameters();
	this.names = operation.getParameterNames();
	this.types = new String[this.nb];
	final ILocalVariable[] unresolvedParameters = operation.getParameters();
	for (int i = 0; i < this.nb; ++i) {
		this.types[i] = resolve(operation, unresolvedParameters[i].getTypeSignature());
	}
}
 
Example 18
Source File: JavaSetterOperatorEditor.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSetterOrDataSelected(final ITreeSelection selection) {
    if (selection.getFirstElement() instanceof IMethod) {
        final IMethod method = (IMethod) selection.getFirstElement();
        try {
            return method.getParameterNames().length == 1;
        } catch (final Exception ex) {
            BonitaStudioLog.error(ex);
            return false;
        }
    } else if (selection.getFirstElement() instanceof String) {
        return true;
    } else {
        return false;
    }
}
 
Example 19
Source File: MethodFilter.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
public boolean match(IMethod method) {
	try {
		if (pattern.declaringQualification != null) {
			if (!matchesName(this.pattern.declaringQualification,
					method.getDeclaringType().getPackageFragment().getElementName().toCharArray()))
				return false;
		}

		if (pattern.declaringSimpleName != null) {
			if (!matchesName(this.pattern.declaringSimpleName,
					method.getDeclaringType().getTypeQualifiedName().toCharArray()))
				return false;
		}

		int argsLength = method.getParameterNames().length;
		if (!match(method.getElementName(), argsLength)) {
			return false;
		}
		if (this.pattern.parameterSimpleNames != null) {
			// Check parameters names
			String[] parameterNames = method.getParameterNames();
			for (String parameterName : parameterNames) {
				if (!matchParam(argsLength, parameterName)) {
					return false;
				}
			}
		}
		return true;
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 20
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static String createMethodContents(IType newType,
    ImportManagerAdapter imports, IMethodBinding overridableSyncMethod,
    boolean addComments) throws CoreException, JavaModelException {
  StringBuilder sb = new StringBuilder();

  IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();

  if (addComments) {
    String lineDelimiter = "\n"; // OK, since content is formatted afterwards

    // Don't go through CodeGeneration type since it can't deal with delegates
    String comment = StubUtility.getMethodComment(
        newType.getCompilationUnit(), newType.getFullyQualifiedName(),
        syncMethod.getElementName(), NO_STRINGS, NO_STRINGS,
        Signature.SIG_VOID, NO_STRINGS, syncMethod, true, lineDelimiter);
    if (comment != null) {
      sb.append(comment);
      sb.append(lineDelimiter);
    }
  }

  // Expand the type parameters
  ITypeParameter[] typeParameters = syncMethod.getTypeParameters();
  ITypeBinding[] typeParameterBindings = overridableSyncMethod.getTypeParameters();
  if (typeParameters.length > 0) {
    sb.append("<");
    for (int i = 0; i < typeParameters.length; ++i) {
      sb.append(typeParameters[i].getElementName());
      ITypeBinding typeParameterBinding = typeParameterBindings[i];
      ITypeBinding[] typeBounds = typeParameterBinding.getTypeBounds();
      if (typeBounds.length > 0) {
        sb.append(" extends ");
        for (int j = 0; j < typeBounds.length; ++j) {
          if (j != 0) {
            sb.append(" & ");
          }
          expandTypeBinding(typeBounds[j], sb, imports);
        }
      }
    }
    sb.append(">");
  }

  // Default to a void return type for the async method
  sb.append("void ");

  // Expand the method name
  sb.append(overridableSyncMethod.getName());

  // Expand the arguments
  sb.append("(");
  String[] parameterNames = syncMethod.getParameterNames();
  ITypeBinding[] parameterTypes = overridableSyncMethod.getParameterTypes();
  for (int i = 0; i < parameterNames.length; ++i) {
    if (i != 0) {
      sb.append(", ");
    }

    expandTypeBinding(parameterTypes[i], sb, imports);
    sb.append(" ");
    sb.append(parameterNames[i]);
  }

  if (parameterNames.length > 0) {
    sb.append(", ");
  }

  sb.append(imports.addImport(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME));
  sb.append("<");
  ITypeBinding syncReturnType = overridableSyncMethod.getReturnType();
  if (syncReturnType.isPrimitive()) {
    String wrapperTypeName = JavaASTUtils.getWrapperTypeName(syncReturnType.getName());
    sb.append(imports.addImport(wrapperTypeName));
  } else {
    expandTypeBinding(syncReturnType, sb, imports);
  }
  sb.append("> ");
  sb.append(StringUtilities.computeUniqueName(parameterNames, "callback"));
  sb.append(");");

  return sb.toString();
}