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;
}
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;
}
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);
}
}
}
}
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;
}
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");
}
}
}
}
}
Aggregations