Search in sources :

Example 6 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class MethodTypeResolution method produceInitialConstraints.

public static List<Constraint> produceInitialConstraints(Method method, ASTArgumentList argList, List<Variable> variables) {
    List<Constraint> result = new ArrayList<>();
    Type[] methodParameters = method.getGenericParameterTypes();
    TypeVariable<Method>[] methodTypeParameters = method.getTypeParameters();
    // TODO: add support for variable arity methods
    for (int i = 0; i < methodParameters.length; i++) {
        int typeParamIndex = -1;
        if (methodParameters[i] instanceof TypeVariable) {
            typeParamIndex = JavaTypeDefinition.getGenericTypeIndex(methodTypeParameters, ((TypeVariable<?>) methodParameters[i]).getName());
        }
        if (typeParamIndex != -1) {
            // TODO: we are cheating here, it should be a contraint of the form 'var -> expression' not 'var->type'
            result.add(new Constraint(((TypeNode) argList.jjtGetChild(i)).getTypeDefinition(), variables.get(typeParamIndex), LOOSE_INVOCATION));
        }
    }
    return result;
}
Also used : Type(java.lang.reflect.Type) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)

Example 7 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class NodeInfoPanelController method getAttributes.

/**
 * Gets the XPath attributes of the node for display within a listview.
 */
private static ObservableList<String> getAttributes(Node node) {
    ObservableList<String> result = FXCollections.observableArrayList();
    AttributeAxisIterator attributeAxisIterator = new AttributeAxisIterator(node);
    while (attributeAxisIterator.hasNext()) {
        Attribute attribute = attributeAxisIterator.next();
        result.add(attribute.getName() + " = " + ((attribute.getValue() != null) ? attribute.getStringValue() : "null"));
    }
    if (node instanceof TypeNode) {
        result.add("typeof() = " + ((TypeNode) node).getType());
    }
    Collections.sort(result);
    return result;
}
Also used : Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AttributeAxisIterator(net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator)

Example 8 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class UnusedImportsRule method check.

protected void check(Node node) {
    if (imports.isEmpty()) {
        return;
    }
    ImportWrapper candidate = getImportWrapper(node);
    Iterator<ImportWrapper> it = imports.iterator();
    while (it.hasNext()) {
        ImportWrapper i = it.next();
        if (i.matches(candidate)) {
            it.remove();
            return;
        }
    }
    if (TypeNode.class.isAssignableFrom(node.getClass()) && ((TypeNode) node).getType() != null) {
        Class<?> c = ((TypeNode) node).getType();
        if (c.getPackage() != null) {
            candidate = new ImportWrapper(c.getPackage().getName(), null);
            if (imports.contains(candidate)) {
                imports.remove(candidate);
            }
        }
    }
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode)

Example 9 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class AbstractJUnitRule method doesNodeContainJUnitAnnotation.

private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
    List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
    for (ASTAnnotation annotation : annotations) {
        Node annotationTypeNode = annotation.jjtGetChild(0);
        TypeNode annotationType = (TypeNode) annotationTypeNode;
        if (annotationType.getType() == null) {
            ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
            if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
                return true;
            }
        } else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
            return true;
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode)

Example 10 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class ClassTypeResolver method rollupTypeBinaryNumericPromotion.

// Roll up the type based on type of the first and second child nodes using
// Binary Numeric Promotion per JLS 5.6.2
private void rollupTypeBinaryNumericPromotion(TypeNode typeNode) {
    Node node = typeNode;
    if (node.jjtGetNumChildren() >= 2) {
        Node child1 = node.jjtGetChild(0);
        Node child2 = node.jjtGetChild(1);
        if (child1 instanceof TypeNode && child2 instanceof TypeNode) {
            Class<?> type1 = ((TypeNode) child1).getType();
            Class<?> type2 = ((TypeNode) child2).getType();
            if (type1 != null && type2 != null) {
                // it, only affects ASTAdditiveExpression
                if ("java.lang.String".equals(type1.getName()) || "java.lang.String".equals(type2.getName())) {
                    populateType(typeNode, "java.lang.String");
                } else if ("boolean".equals(type1.getName()) || "boolean".equals(type2.getName())) {
                    populateType(typeNode, "boolean");
                } else if ("double".equals(type1.getName()) || "double".equals(type2.getName())) {
                    populateType(typeNode, "double");
                } else if ("float".equals(type1.getName()) || "float".equals(type2.getName())) {
                    populateType(typeNode, "float");
                } else if ("long".equals(type1.getName()) || "long".equals(type2.getName())) {
                    populateType(typeNode, "long");
                } else {
                    populateType(typeNode, "int");
                }
            } else if (type1 != null || type2 != null) {
                // it, only affects ASTAdditiveExpression
                if (type1 != null && "java.lang.String".equals(type1.getName()) || type2 != null && "java.lang.String".equals(type2.getName())) {
                    populateType(typeNode, "java.lang.String");
                }
            }
        }
    }
}
Also used : QualifiableNode(net.sourceforge.pmd.lang.ast.QualifiableNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Aggregations

TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)15 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)11 Node (net.sourceforge.pmd.lang.ast.Node)6 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)4 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)3 QualifiableNode (net.sourceforge.pmd.lang.ast.QualifiableNode)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)2 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1