org.eclipse.jdt.core.dom.MethodReference Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.MethodReference. 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: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static String[] getUniqueParameterNames(MethodReference methodReference, IMethodBinding functionalMethod) throws JavaModelException {
	String[] parameterNames = ((IMethod) functionalMethod.getJavaElement()).getParameterNames();
	List<String> oldNames = new ArrayList<>(Arrays.asList(parameterNames));
	String[] newNames = new String[oldNames.size()];
	List<String> excludedNames = new ArrayList<>(ASTNodes.getVisibleLocalVariablesInScope(methodReference));

	for (int i = 0; i < oldNames.size(); i++) {
		String paramName = oldNames.get(i);
		List<String> allNamesToExclude = new ArrayList<>(excludedNames);
		allNamesToExclude.addAll(oldNames.subList(0, i));
		allNamesToExclude.addAll(oldNames.subList(i + 1, oldNames.size()));
		if (allNamesToExclude.contains(paramName)) {
			String newParamName = createName(paramName, allNamesToExclude);
			excludedNames.add(newParamName);
			newNames[i] = newParamName;
		} else {
			newNames[i] = paramName;
		}
	}
	return newNames;
}
 
Example #2
Source File: SurroundWithTryCatchAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void endVisit(CompilationUnit node) {
	ASTNode enclosingNode = null;
	if (!getStatus().hasFatalError() && hasSelectedNodes()) {
		enclosingNode= getEnclosingNode(getFirstSelectedNode());
	}

	super.endVisit(node);
	if (enclosingNode != null && !getStatus().hasFatalError()) {
		fExceptions = ExceptionAnalyzer.perform(enclosingNode, getSelection());
		if (fExceptions == null || fExceptions.length == 0) {
			if (enclosingNode instanceof MethodReference) {
				invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
			} else {
				fExceptions = new ITypeBinding[] { node.getAST().resolveWellKnownType("java.lang.Exception") }; //$NON-NLS-1$
			}
		}
	}
}
 
Example #3
Source File: ExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private boolean handleMethodReference(MethodReference node) {
	if (!isSelected(node)) {
		return false;
	}
	if (!fEnclosingNode.equals(node)) {
		return false;
	}
	IMethodBinding referredMethodBinding = node.resolveMethodBinding();
	if (referredMethodBinding == null) {
		return false;
	}
	IMethodBinding functionalMethod = QuickAssistProcessor.getFunctionalMethodForMethodReference(node);
	if (functionalMethod == null || functionalMethod.isGenericMethod()) { // generic lambda expressions are not allowed
		return false;
	}
	return handleExceptions(referredMethodBinding, node);
}
 
Example #4
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the functional interface method being implemented by the given method
 * reference.
 *
 * @param methodReference
 *            the method reference to get the functional method
 * @return the functional interface method being implemented by
 *         <code>methodReference</code> or <code>null</code> if it could not be
 *         derived
 */
public static IMethodBinding getFunctionalMethodForMethodReference(MethodReference methodReference) {
	ITypeBinding targetTypeBinding = ASTNodes.getTargetType(methodReference);
	if (targetTypeBinding == null) {
		return null;
	}

	IMethodBinding functionalMethod = targetTypeBinding.getFunctionalInterfaceMethod();
	if (functionalMethod.isSynthetic()) {
		functionalMethod = Bindings.findOverriddenMethodInType(functionalMethod.getDeclaringClass(), functionalMethod);
	}
	return functionalMethod;
}
 
Example #5
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isTypeReferenceToInstanceMethod(MethodReference methodReference) {
	if (methodReference instanceof TypeMethodReference) {
		return true;
	}
	if (methodReference instanceof ExpressionMethodReference) {
		Expression expression = ((ExpressionMethodReference) methodReference).getExpression();
		if (expression instanceof Name) {
			IBinding nameBinding = ((Name) expression).resolveBinding();
			if (nameBinding != null && nameBinding instanceof ITypeBinding) {
				return true;
			}
		}
	}
	return false;
}
 
Example #6
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static SimpleName getMethodInvocationName(MethodReference methodReference) {
	SimpleName name = null;
	if (methodReference instanceof ExpressionMethodReference) {
		name = ((ExpressionMethodReference) methodReference).getName();
	} else if (methodReference instanceof TypeMethodReference) {
		name = ((TypeMethodReference) methodReference).getName();
	} else if (methodReference instanceof SuperMethodReference) {
		name = ((SuperMethodReference) methodReference).getName();
	}
	return name;
}
 
Example #7
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
private MethodRef visit(MethodReference node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	MethodRef methodRef = new MethodRef(startLine, endLine, node);
	return methodRef;
}
 
Example #8
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the type to which an inlined variable initializer should be cast, or
 * <code>null</code> if no cast is necessary.
 * 
 * @param initializer the initializer expression of the variable to inline
 * @param reference the reference to the variable (which is to be inlined)
 * @return a type binding to which the initializer should be cast, or <code>null</code> iff no cast is necessary
 * @since 3.6
 */
public static ITypeBinding getExplicitCast(Expression initializer, Expression reference) {
	ITypeBinding initializerType= initializer.resolveTypeBinding();
	ITypeBinding referenceType= reference.resolveTypeBinding();
	if (initializerType == null || referenceType == null)
		return null;
	
	if (initializerType.isPrimitive() && referenceType.isPrimitive() && ! referenceType.isEqualTo(initializerType)) {
		return referenceType;
		
	} else if (initializerType.isPrimitive() && ! referenceType.isPrimitive()) { // initializer is autoboxed
		ITypeBinding unboxedReferenceType= Bindings.getUnboxedTypeBinding(referenceType, reference.getAST());
		if (!unboxedReferenceType.isEqualTo(initializerType))
			return unboxedReferenceType;
		else if (needsExplicitBoxing(reference))
			return referenceType;
		
	} else if (! initializerType.isPrimitive() && referenceType.isPrimitive()) { // initializer is autounboxed
		ITypeBinding unboxedInitializerType= Bindings.getUnboxedTypeBinding(initializerType, reference.getAST());
		if (!unboxedInitializerType.isEqualTo(referenceType))
			return referenceType;
		
	} else if (initializerType.isRawType() && referenceType.isParameterizedType()) {
		return referenceType; // don't lose the unchecked conversion

	} else if (initializer instanceof LambdaExpression || initializer instanceof MethodReference) {
		if (isTargetAmbiguous(reference, isExplicitlyTypedLambda(initializer))) {
			return referenceType;
		} else {
			ITypeBinding targetType= getTargetType(reference);
			if (targetType == null || targetType != referenceType) {
				return referenceType;
			}
		}

	} else if (! TypeRules.canAssign(initializerType, referenceType)) {
		if (!Bindings.containsTypeVariables(referenceType))
			return referenceType;
	}
	
	return null;
}