Search in sources :

Example 11 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTClassOrInterfaceType node, Object data) {
    super.visit(node, data);
    String typeName = node.getImage();
    if (node.isAnonymousClass()) {
        QualifiableNode parent = node.getFirstParentOfAnyType(ASTAllocationExpression.class, ASTEnumConstant.class);
        if (parent != null) {
            typeName = parent.getQualifiedName().toString();
        }
    }
    populateType(node, typeName, node.getArrayDepth());
    ASTTypeArguments typeArguments = node.getFirstChildOfType(ASTTypeArguments.class);
    if (typeArguments != null) {
        final JavaTypeDefinition[] boundGenerics = new JavaTypeDefinition[typeArguments.jjtGetNumChildren()];
        for (int i = 0; i < typeArguments.jjtGetNumChildren(); ++i) {
            boundGenerics[i] = ((TypeNode) typeArguments.jjtGetChild(i)).getTypeDefinition();
        }
        node.setTypeDefinition(JavaTypeDefinition.forClass(node.getType(), boundGenerics));
    }
    return data;
}
Also used : ASTTypeArguments(net.sourceforge.pmd.lang.java.ast.ASTTypeArguments) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) QualifiableNode(net.sourceforge.pmd.lang.ast.QualifiableNode)

Example 12 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTTypeParameters node, Object data) {
    super.visit(node, data);
    if (node.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
        TypeNode parent = (TypeNode) node.jjtGetParent();
        final JavaTypeDefinition[] boundGenerics = new JavaTypeDefinition[node.jjtGetNumChildren()];
        for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
            boundGenerics[i] = ((TypeNode) node.jjtGetChild(i)).getTypeDefinition();
        }
        parent.setTypeDefinition(JavaTypeDefinition.forClass(parent.getType(), boundGenerics));
    }
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 13 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class MethodTypeResolution method checkSubtypeability.

public static boolean checkSubtypeability(MethodType method, MethodType subtypeableMethod) {
    List<JavaTypeDefinition> subtypeableParams = subtypeableMethod.getParameterTypes();
    List<JavaTypeDefinition> methodParams = method.getParameterTypes();
    // If we come from third-phase, both are varargs, otherwhise, treat all as fixed-arity
    if (!method.getMethod().isVarArgs() || !subtypeableMethod.getMethod().isVarArgs()) {
        for (int index = 0; index < subtypeableParams.size(); ++index) {
            if (!isSubtypeable(methodParams.get(index), subtypeableParams.get(index))) {
                return false;
            }
        }
    } else {
        final int maxSize = Math.max(subtypeableParams.size(), methodParams.size());
        for (int index = 0; index < maxSize; ++index) {
            if (!isSubtypeable(method.getArgTypeIncludingVararg(index), subtypeableMethod.getArgTypeIncludingVararg(index))) {
                return false;
            }
        }
    }
    return true;
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)

Example 14 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition 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)

Example 15 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class MethodTypeResolution method getMethodExplicitTypeArugments.

public static List<JavaTypeDefinition> getMethodExplicitTypeArugments(Node node) {
    ASTMemberSelector memberSelector = node.getFirstChildOfType(ASTMemberSelector.class);
    if (memberSelector == null) {
        return Collections.emptyList();
    }
    ASTTypeArguments typeArguments = memberSelector.getFirstChildOfType(ASTTypeArguments.class);
    if (typeArguments == null) {
        return Collections.emptyList();
    }
    List<JavaTypeDefinition> result = new ArrayList<>();
    for (int childIndex = 0; childIndex < typeArguments.jjtGetNumChildren(); ++childIndex) {
        result.add(((TypeNode) typeArguments.jjtGetChild(childIndex)).getTypeDefinition());
    }
    return result;
}
Also used : ASTTypeArguments(net.sourceforge.pmd.lang.java.ast.ASTTypeArguments) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ArrayList(java.util.ArrayList) ASTMemberSelector(net.sourceforge.pmd.lang.java.ast.ASTMemberSelector) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)

Aggregations

JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)19 ArrayList (java.util.ArrayList)11 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 Test (org.junit.Test)6 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)5 List (java.util.List)4 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)4 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)3 Method (java.lang.reflect.Method)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTTypeArguments (net.sourceforge.pmd.lang.java.ast.ASTTypeArguments)2 Bound (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound)2 Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)2 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)2 SuperClassA2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2)2 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1