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