Search in sources :

Example 1 with ResolutionFailedException

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException in project pmd by pmd.

the class MethodTypeResolution method selectMethodsSecondPhase.

/**
 * Look for methods be method conversion.
 * https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.3
 */
public static List<MethodType> selectMethodsSecondPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
    // TODO: check if explicit type arguments are applicable to the type parameter bounds
    List<MethodType> selectedMethods = new ArrayList<>();
    final int argCount = argList == null ? 0 : argList.jjtGetNumChildren();
    for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
        MethodType methodType = methodsToSearch.get(methodIndex);
        if (!methodType.isParameterized()) {
            throw new ResolutionFailedException();
        }
        // vararg methods are considered fixed arity here, see 3rd phase
        if (getArity(methodType.getMethod()) == argCount) {
            // check method convertability of each argument to the corresponding parameter
            boolean methodIsApplicable = true;
            // try each arguments if it's method convertible
            for (int argIndex = 0; argIndex < argCount; ++argIndex) {
                if (!isMethodConvertible(methodType.getParameterTypes().get(argIndex), (ASTExpression) argList.jjtGetChild(argIndex))) {
                    methodIsApplicable = false;
                    break;
                }
            // TODO: add unchecked conversion in an else if branch
            }
            if (methodIsApplicable) {
                selectedMethods.add(methodType);
            }
        }
    }
    return selectedMethods;
}
Also used : ArrayList(java.util.ArrayList) ResolutionFailedException(net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ResolutionFailedException

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException in project pmd by pmd.

the class MethodTypeResolution method selectMethodsThirdPhase.

/**
 * Look for methods considering varargs as well.
 * https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.4
 */
public static List<MethodType> selectMethodsThirdPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
    // TODO: check if explicit type arguments are applicable to the type parameter bounds
    List<MethodType> selectedMethods = new ArrayList<>();
    for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
        MethodType methodType = methodsToSearch.get(methodIndex);
        if (!methodType.isParameterized()) {
            throw new ResolutionFailedException();
        }
        // if we reach here and the method is not a vararg, then we didn't find a resolution in earlier phases
        if (methodType.isVararg()) {
            // check subtypeability of each argument to the corresponding parameter
            boolean methodIsApplicable = true;
            List<JavaTypeDefinition> methodParameters = methodType.getParameterTypes();
            JavaTypeDefinition varargComponentType = methodType.getVarargComponentType();
            if (argList == null) {
                // There are no arguments, make sure the method has only a vararg
                methodIsApplicable = getArity(methodType.getMethod()) == 1;
            } else {
                // try each arguments if it's method convertible
                for (int argIndex = 0; argIndex < argList.jjtGetNumChildren(); ++argIndex) {
                    JavaTypeDefinition parameterType = argIndex < methodParameters.size() - 1 ? methodParameters.get(argIndex) : varargComponentType;
                    if (!isMethodConvertible(parameterType, (ASTExpression) argList.jjtGetChild(argIndex))) {
                        methodIsApplicable = false;
                        break;
                    }
                // TODO: If k != n, or if k = n and An cannot be converted by method invocation conversion to
                // Sn[], then the type which is the erasure (ยง4.6) of Sn is accessible at the point of invocation.
                // TODO: add unchecked conversion in an else if branch
                }
            }
            if (methodIsApplicable) {
                selectedMethods.add(methodType);
            }
        } else {
            // TODO: Remove check for vararg here, once we can detect and use return types of method calls
            LOG.log(Level.FINE, "Method {0} couldn't be resolved", String.valueOf(methodType));
        }
    }
    return selectedMethods;
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ArrayList(java.util.ArrayList) ResolutionFailedException(net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ArrayList (java.util.ArrayList)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)2 ResolutionFailedException (net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException)2 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)1