下面列出了怎么用org.eclipse.jdt.core.dom.MethodReference的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
@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$
}
}
}
}
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);
}
/**
* 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;
}
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;
}
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;
}
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;
}
/**
* 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;
}