/** * Get the functional method defined by the type, if any. */ public static Optional<MethodUsage> getFunctionalMethod(ResolvedReferenceTypeDeclaration typeDeclaration) { //We need to find all abstract methods Set<MethodUsage> methods = typeDeclaration.getAllMethods().stream() .filter(m -> m.getDeclaration().isAbstract()) // Remove methods inherited by Object: // Consider the case of Comparator which define equals. It would be considered a functional method. .filter(m -> !declaredOnObject(m)) .collect(Collectors.toSet()); if (methods.size() == 1) { return Optional.of(methods.iterator().next()); } else { return Optional.empty(); } }
protected MethodUsage toMethodUsage(MethodReferenceExpr methodReferenceExpr) { if (!(methodReferenceExpr.getScope() instanceof TypeExpr)) { throw new UnsupportedOperationException(); } TypeExpr typeExpr = (TypeExpr) methodReferenceExpr.getScope(); if (!(typeExpr.getType() instanceof com.github.javaparser.ast.type.ClassOrInterfaceType)) { throw new UnsupportedOperationException(typeExpr.getType().getClass().getCanonicalName()); } ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) typeExpr.getType(); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName().getId(), typeSolver); if (!typeDeclarationSymbolReference.isSolved()) { throw new UnsupportedOperationException(); } List<MethodUsage> methodUsages = ((ResolvedReferenceTypeDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration()).getAllMethods().stream().filter(it -> it.getName().equals(methodReferenceExpr.getIdentifier())).collect(Collectors.toList()); switch (methodUsages.size()) { case 0: throw new UnsupportedOperationException(); case 1: return methodUsages.get(0); default: throw new UnsupportedOperationException(); } }
protected MethodUsage toMethodUsage(MethodReferenceExpr methodReferenceExpr) { if (!(methodReferenceExpr.getScope() instanceof TypeExpr)) { throw new UnsupportedOperationException(); } TypeExpr typeExpr = (TypeExpr) methodReferenceExpr.getScope(); if (!(typeExpr.getType() instanceof com.github.javaparser.ast.type.ClassOrInterfaceType)) { throw new UnsupportedOperationException(typeExpr.getType().getClass().getCanonicalName()); } ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) typeExpr.getType(); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName().getId()); if (!typeDeclarationSymbolReference.isSolved()) { throw new UnsupportedOperationException(); } List<MethodUsage> methodUsages = ((ResolvedReferenceTypeDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration()).getAllMethods().stream().filter(it -> it.getName().equals(methodReferenceExpr.getIdentifier())).collect(Collectors.toList()); switch (methodUsages.size()) { case 0: throw new UnsupportedOperationException(); case 1: return methodUsages.get(0); default: throw new UnsupportedOperationException(); } }
protected MethodUsage toMethodUsage(MethodReferenceExpr methodReferenceExpr) { if (!(methodReferenceExpr.getScope() instanceof TypeExpr)) { throw new UnsupportedOperationException(); } TypeExpr typeExpr = (TypeExpr) methodReferenceExpr.getScope(); if (!(typeExpr.getType() instanceof com.github.javaparser.ast.type.ClassOrInterfaceType)) { throw new UnsupportedOperationException(typeExpr.getType().getClass().getCanonicalName()); } ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) typeExpr.getType(); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName().getId(), typeSolver); if (!typeDeclarationSymbolReference.isSolved()) { throw new UnsupportedOperationException(); } List<MethodUsage> methodUsages = ((ResolvedReferenceTypeDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration()).getAllMethods().stream().filter(it -> it.getName().equals(methodReferenceExpr.getIdentifier())).collect(Collectors.toList()); switch (methodUsages.size()) { case 0: throw new UnsupportedOperationException(); case 1: return methodUsages.get(0); default: throw new UnsupportedOperationException(); } }
if (scopeType instanceof ResolvedReferenceTypeDeclaration) { ResolvedReferenceTypeDeclaration scopeRefType = scopeType.asReferenceType(); if (scopeRefType.getAllMethods().stream().anyMatch(m -> m.getName().equals(rightName))) { return NameCategory.EXPRESSION_NAME;