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

The following examples show how to use org.eclipse.jdt.core.IMethod#getNumberOfParameters() . 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: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IJavaElement caseJvmOperation(JvmOperation object) {
	IJavaElement parent = getDeclaringTypeElement(object);
	if (parent instanceof IType) {
		IType type = (IType) parent;
		try {
			for(IMethod method: type.getMethods()) {
				if (!method.isConstructor()) {
					if (object.getSimpleName().equals(method.getElementName()) && object.getParameters().size() == method.getNumberOfParameters()) {
						boolean match = doParametersMatch(object, method, type);
						if (match)
							return method;
					}
				}
			}
		}
		catch (JavaModelException e) {
			return (isExactMatchOnly) ? null : parent;
		}
	}
	return (isExactMatchOnly) ? null : parent;
}
 
Example 2
Source File: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private boolean doParametersMatch(JvmExecutable object, IMethod method, IType declaringType)
		throws JavaModelException {
	int numberOfParameters = method.getNumberOfParameters();
	String[] parameterTypes = method.getParameterTypes();
	boolean match = true;
	for (int i = 0; i < numberOfParameters && match; i++) {
		JvmFormalParameter formalParameter = object.getParameters().get(i);
		String parameterType = parameterTypes[i];
		String readable = toQualifiedRawTypeName(parameterType, declaringType);
		String qualifiedParameterType = getQualifiedParameterType(formalParameter);
		if (!readable.equals(qualifiedParameterType)) {
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=387576
			if (qualifiedParameterType != null && qualifiedParameterType.endsWith("$") && qualifiedParameterType.startsWith(readable)) {
				for(int c = readable.length(); c < qualifiedParameterType.length() && match; c++) {
					if (qualifiedParameterType.charAt(c) != '$') {
						match = false;
					}
				}
			} else {
				match = false;
			}
		}
	}
	return match;
}
 
Example 3
Source File: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IJavaElement caseJvmConstructor(JvmConstructor object) {
	IJavaElement parent = getDeclaringTypeElement(object);
	if (parent instanceof IType) {
		IType type = (IType) parent;
		try {
			for(IMethod method: type.getMethods()) {
				if (method.isConstructor() && object.getParameters().size() == method.getNumberOfParameters()) {
					boolean match = doParametersMatch(object, method, type);
					if (match)
						return method;
				}
			}
		}
		catch (JavaModelException e) {
			return (isExactMatchOnly) ? null : parent;
		}
	}
	return (isExactMatchOnly) ? null : parent;
}
 
Example 4
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean sameParams(IMethod method, IMethod method2) {

		if (method.getNumberOfParameters() != method2.getNumberOfParameters()) {
			return false;
		}

		String[] params = method.getParameterTypes();
		String[] params2 = method2.getParameterTypes();

		for (int i = 0; i < params.length; i++) {
			String t1 = Signature.getSimpleName(Signature.toString(params[i]));
			String t2 = Signature.getSimpleName(Signature.toString(params2[i]));
			if (!t1.equals(t2)) {
				return false;
			}
		}
		return true;
	}
 
Example 5
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean sameParams(IMethod method, IMethod method2) {

		if (method.getNumberOfParameters() != method2.getNumberOfParameters())
			return false;

		String[] params= method.getParameterTypes();
		String[] params2= method2.getParameterTypes();

		for (int i= 0; i < params.length; i++) {
			String t1= Signature.getSimpleName(Signature.toString(params[i]));
			String t2= Signature.getSimpleName(Signature.toString(params2[i]));
			if (!t1.equals(t2)) {
				return false;
			}
		}
		return true;
	}
 
Example 6
Source File: MethodOverrideTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tests if a method is a subsignature of another method.
 * @param overriding overriding method (m1)
 * @param overridden overridden method (m2)
 * @return <code>true</code> iff the method <code>m1</code> is a subsignature of the method <code>m2</code>.
 * 		This is one of the requirements for m1 to override m2.
 * 		Accessibility and return types are not taken into account.
 * 		Note that subsignature is <em>not</em> symmetric!
 * @throws JavaModelException if a problem occurs
 */
public boolean isSubsignature(IMethod overriding, IMethod overridden) throws JavaModelException {
	if (!overridden.getElementName().equals(overriding.getElementName())) {
		return false;
	}
	int nParameters= overridden.getNumberOfParameters();
	if (nParameters != overriding.getNumberOfParameters()) {
		return false;
	}

	if (!hasCompatibleTypeParameters(overriding, overridden)) {
		return false;
	}

	return nParameters == 0 || hasCompatibleParameterTypes(overriding, overridden);
}
 
Example 7
Source File: JavaElementDelegateJunitLaunch.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsJUnitTestMethod(IType type) throws JavaModelException {
	for (IMethod method : type.getMethods()) {
		int flags = method.getFlags();
		if (Modifier.isPublic(flags) && !Modifier.isStatic(flags) &&
				method.getNumberOfParameters() == 0 && Signature.SIG_VOID.equals(method.getReturnType()) &&
				method.getElementName().startsWith("test")) { //$NON-NLS-1$
			return true;
		}
		IAnnotation annotation= method.getAnnotation("Test"); //$NON-NLS-1$
		if (annotation.exists())
			return true;
	}
	return false;
}
 
Example 8
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		resolveParamNames.start();
		
		// Use the handle to find the top level type and then use the path the traverse to the correct nested type.
		//
		IType type = findTypeByHandleIdentifier();

		List<JvmFormalParameter> parameters = executable.getParameters();
		if (type != null) {
			IMethod javaMethod = findJavaMethod(type);
			int numberOfParameters = javaMethod.getNumberOfParameters();
			if (numberOfParameters != 0) {
				try {
					setParameterNames(javaMethod, parameters);
					return;
				} catch (JavaModelException ex) {
					if (!ex.isDoesNotExist())
						log.warn("IMethod.getParameterNames failed", ex);
				}
			}
		}

		// We generally should not ever get here.
		//
		synthesizeNames(parameters);
	} finally {
		resolveParamNames.stop();
	}
}
 
Example 9
Source File: ParameterGuesser.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isMethodToSuggest(IMethod method) {
	try {
		String methodName= method.getElementName();
		return method.getNumberOfParameters() == 0 && !Signature.SIG_VOID.equals(method.getReturnType())
			&& (methodName.startsWith("get") || methodName.startsWith("is"));    //$NON-NLS-1$//$NON-NLS-2$
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 10
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isRenameProhibited(final IMethod method) throws CoreException {
	if (method.getElementName().equals("toString") //$NON-NLS-1$
			&& (method.getNumberOfParameters() == 0) && (method.getReturnType().equals("Ljava.lang.String;") //$NON-NLS-1$
					|| method.getReturnType().equals("QString;") //$NON-NLS-1$
					|| method.getReturnType().equals("Qjava.lang.String;"))) {
		return true;
	} else {
		return false;
	}
}
 
Example 11
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkVarargs() throws JavaModelException {
	RefactoringStatus result= checkOriginalVarargs();
	if (result != null)
		return result;

	if (fRippleMethods != null) {
		for (int iRipple= 0; iRipple < fRippleMethods.length; iRipple++) {
			IMethod rippleMethod= fRippleMethods[iRipple];
			if (! JdtFlags.isVarargs(rippleMethod))
				continue;

			// Vararg method can override method that takes an array as last argument
			fOldVarargIndex= rippleMethod.getNumberOfParameters() - 1;
			List<ParameterInfo> notDeletedInfos= getNotDeletedInfos();
			for (int i= 0; i < notDeletedInfos.size(); i++) {
				ParameterInfo info= notDeletedInfos.get(i);
				if (fOldVarargIndex != -1 && info.getOldIndex() == fOldVarargIndex && ! info.isNewVarargs()) {
					String rippleMethodType= rippleMethod.getDeclaringType().getFullyQualifiedName('.');
					String message= Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_ripple_cannot_convert_vararg, new Object[] { BasicElementLabels.getJavaElementName(info.getNewName()), BasicElementLabels.getJavaElementName(rippleMethodType)});
					return RefactoringStatus.createFatalErrorStatus(message, JavaStatusContext.create(rippleMethod));
				}
			}
		}
	}

	return null;
}
 
Example 12
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isRenameProhibited(final IMethod method) throws CoreException {
	if (method.getElementName().equals("toString") //$NON-NLS-1$
			&& (method.getNumberOfParameters() == 0) && (method.getReturnType().equals("Ljava.lang.String;") //$NON-NLS-1$
					|| method.getReturnType().equals("QString;") //$NON-NLS-1$
					|| method.getReturnType().equals("Qjava.lang.String;"))) //$NON-NLS-1$
		return true;
	else
		return false;
}
 
Example 13
Source File: ParameterGuesser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isMethodToSuggest(IMethod method) {
	try {
		String methodName= method.getElementName();
		return method.getNumberOfParameters() == 0 && !Signature.SIG_VOID.equals(method.getReturnType())
			&& (methodName.startsWith("get") || methodName.startsWith("is"));    //$NON-NLS-1$//$NON-NLS-2$
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 14
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 15
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		final IJavaElement[] children= type.getChildren();
		for (int index= 0; index < children.length; index++) {
			final IMember child= (IMember) children[index];
			final int flags= child.getFlags();
			final boolean isPrivate= Flags.isPrivate(flags);
			final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
			final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
			if (child instanceof IType) {
				if (stub)
					appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
			} else if (child instanceof IField) {
				if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
					appendFieldDeclaration((IField) child);
			} else if (child instanceof IMethod) {
				final IMethod method= (IMethod) child;
				final String name= method.getElementName();
				if (method.getDeclaringType().isEnum()) {
					final int count= method.getNumberOfParameters();
					if (count == 0 && "values".equals(name)) //$NON-NLS-1$
						continue;
					if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					if (method.isConstructor())
						continue;
				}
				boolean skip= !stub || name.equals("<clinit>"); //$NON-NLS-1$
				if (method.isConstructor())
					skip= false;
				skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
				if (!skip)
					appendMethodDeclaration(method);
			}
			fBuffer.append("\n"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}