Merge branch 'pr-162' into pmd/5.5.x
Closes #162 (rebased onto pmd/5.4.x)
This commit is contained in:
@ -182,15 +182,14 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
|
|
||||||
Set<NameDeclaration> result = new HashSet<>();
|
Set<NameDeclaration> result = new HashSet<>();
|
||||||
if (occurrence.isMethodOrConstructorInvocation()) {
|
if (occurrence.isMethodOrConstructorInvocation()) {
|
||||||
|
final boolean hasAuxclasspath = getEnclosingScope(SourceFileScope.class).hasAuxclasspath();
|
||||||
for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
|
for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
|
||||||
if (mnd.getImage().equals(occurrence.getImage())) {
|
if (mnd.getImage().equals(occurrence.getImage())) {
|
||||||
List<TypedNameDeclaration> parameterTypes = determineParameterTypes(mnd);
|
List<TypedNameDeclaration> parameterTypes = determineParameterTypes(mnd);
|
||||||
List<TypedNameDeclaration> argumentTypes = determineArgumentTypes(occurrence, parameterTypes);
|
List<TypedNameDeclaration> argumentTypes = determineArgumentTypes(occurrence, parameterTypes);
|
||||||
|
|
||||||
if (!mnd.isVarargs()
|
if (!mnd.isVarargs() && occurrence.getArgumentCount() == mnd.getParameterCount()
|
||||||
&& occurrence.getArgumentCount() == mnd.getParameterCount()
|
&& (!hasAuxclasspath || parameterTypes.equals(argumentTypes))) {
|
||||||
&& (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath() || parameterTypes
|
|
||||||
.equals(argumentTypes))) {
|
|
||||||
result.add(mnd);
|
result.add(mnd);
|
||||||
} else if (mnd.isVarargs()) {
|
} else if (mnd.isVarargs()) {
|
||||||
int varArgIndex = parameterTypes.size() - 1;
|
int varArgIndex = parameterTypes.size() - 1;
|
||||||
@ -201,11 +200,12 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
// or the calling method has enough arguments to fill in
|
// or the calling method has enough arguments to fill in
|
||||||
// the parameters before the vararg
|
// the parameters before the vararg
|
||||||
if ((varArgIndex == 0 || argumentTypes.size() >= varArgIndex)
|
if ((varArgIndex == 0 || argumentTypes.size() >= varArgIndex)
|
||||||
&& (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath() || parameterTypes
|
&& (!hasAuxclasspath || parameterTypes
|
||||||
.subList(0, varArgIndex).equals(argumentTypes.subList(0, varArgIndex)))) {
|
.subList(0, varArgIndex).equals(argumentTypes.subList(0, varArgIndex)))) {
|
||||||
|
|
||||||
if (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath()) {
|
if (!hasAuxclasspath) {
|
||||||
result.add(mnd);
|
result.add(mnd);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean sameType = true;
|
boolean sameType = true;
|
||||||
@ -319,8 +319,7 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MethodNameDeclaration mnd = new MethodNameDeclaration(methodDeclarator);
|
return new MethodNameDeclaration(methodDeclarator);
|
||||||
return mnd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -332,9 +331,16 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
* @return List of types
|
* @return List of types
|
||||||
*/
|
*/
|
||||||
private List<TypedNameDeclaration> determineParameterTypes(MethodNameDeclaration mnd) {
|
private List<TypedNameDeclaration> determineParameterTypes(MethodNameDeclaration mnd) {
|
||||||
List<TypedNameDeclaration> parameterTypes = new ArrayList<>();
|
List<ASTFormalParameter> parameters = mnd.getMethodNameDeclaratorNode()
|
||||||
List<ASTFormalParameter> parameters = mnd.getMethodNameDeclaratorNode().findDescendantsOfType(
|
.findDescendantsOfType(ASTFormalParameter.class);
|
||||||
ASTFormalParameter.class);
|
if (parameters.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TypedNameDeclaration> parameterTypes = new ArrayList<>(parameters.size());
|
||||||
|
SourceFileScope fileScope = getEnclosingScope(SourceFileScope.class);
|
||||||
|
Map<String, Node> qualifiedTypeNames = fileScope.getQualifiedTypeNames();
|
||||||
|
|
||||||
for (ASTFormalParameter p : parameters) {
|
for (ASTFormalParameter p : parameters) {
|
||||||
String typeImage = p.getTypeNode().getTypeImage();
|
String typeImage = p.getTypeNode().getTypeImage();
|
||||||
// typeImage might be qualified/unqualified. If it refers to a type,
|
// typeImage might be qualified/unqualified. If it refers to a type,
|
||||||
@ -342,8 +348,8 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
// we should normalize the name here.
|
// we should normalize the name here.
|
||||||
// It might also refer to a type, that is imported.
|
// It might also refer to a type, that is imported.
|
||||||
typeImage = qualifyTypeName(typeImage);
|
typeImage = qualifyTypeName(typeImage);
|
||||||
Node declaringNode = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().get(typeImage);
|
Node declaringNode = qualifiedTypeNames.get(typeImage);
|
||||||
Class<?> resolvedType = this.getEnclosingScope(SourceFileScope.class).resolveType(typeImage);
|
Class<?> resolvedType = fileScope.resolveType(typeImage);
|
||||||
if (resolvedType == null) {
|
if (resolvedType == null) {
|
||||||
resolvedType = resolveGenericType(p, typeImage);
|
resolvedType = resolveGenericType(p, typeImage);
|
||||||
}
|
}
|
||||||
@ -388,107 +394,110 @@ public class ClassScope extends AbstractJavaScope {
|
|||||||
*/
|
*/
|
||||||
private List<TypedNameDeclaration> determineArgumentTypes(JavaNameOccurrence occurrence,
|
private List<TypedNameDeclaration> determineArgumentTypes(JavaNameOccurrence occurrence,
|
||||||
List<TypedNameDeclaration> parameterTypes) {
|
List<TypedNameDeclaration> parameterTypes) {
|
||||||
List<TypedNameDeclaration> argumentTypes = new ArrayList<>();
|
|
||||||
Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
|
|
||||||
ASTArgumentList arguments = null;
|
ASTArgumentList arguments = null;
|
||||||
Node nextSibling = null;
|
Node nextSibling;
|
||||||
if (occurrence.getLocation() instanceof ASTPrimarySuffix) {
|
if (occurrence.getLocation() instanceof ASTPrimarySuffix) {
|
||||||
nextSibling = getNextSibling(occurrence.getLocation());
|
nextSibling = getNextSibling(occurrence.getLocation());
|
||||||
} else {
|
} else {
|
||||||
nextSibling = getNextSibling(occurrence.getLocation().jjtGetParent());
|
nextSibling = getNextSibling(occurrence.getLocation().jjtGetParent());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextSibling != null) {
|
if (nextSibling != null) {
|
||||||
arguments = nextSibling.getFirstDescendantOfType(ASTArgumentList.class);
|
arguments = nextSibling.getFirstDescendantOfType(ASTArgumentList.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments != null) {
|
if (arguments == null) {
|
||||||
for (int i = 0; i < arguments.jjtGetNumChildren(); i++) {
|
return Collections.emptyList();
|
||||||
Node argument = arguments.jjtGetChild(i);
|
}
|
||||||
Node child = null;
|
|
||||||
boolean isMethodCall = false;
|
List<TypedNameDeclaration> argumentTypes = new ArrayList<>(arguments.jjtGetNumChildren());
|
||||||
if (argument.jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetNumChildren() > 0
|
Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
|
||||||
&& argument.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0) {
|
|
||||||
child = argument.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
for (int i = 0; i < arguments.jjtGetNumChildren(); i++) {
|
||||||
isMethodCall = argument.jjtGetChild(0).jjtGetNumChildren() > 1;
|
Node argument = arguments.jjtGetChild(i);
|
||||||
|
Node child = null;
|
||||||
|
boolean isMethodCall = false;
|
||||||
|
if (argument.jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetNumChildren() > 0
|
||||||
|
&& argument.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0) {
|
||||||
|
child = argument.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
||||||
|
isMethodCall = argument.jjtGetChild(0).jjtGetNumChildren() > 1;
|
||||||
|
}
|
||||||
|
TypedNameDeclaration type = null;
|
||||||
|
if (child instanceof ASTName && !isMethodCall) {
|
||||||
|
ASTName name = (ASTName) child;
|
||||||
|
Scope s = name.getScope();
|
||||||
|
final JavaNameOccurrence nameOccurrence = new JavaNameOccurrence(name, name.getImage());
|
||||||
|
while (s != null) {
|
||||||
|
if (s.contains(nameOccurrence)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
s = s.getParent();
|
||||||
}
|
}
|
||||||
TypedNameDeclaration type = null;
|
if (s != null) {
|
||||||
if (child instanceof ASTName && !isMethodCall) {
|
Map<VariableNameDeclaration, List<NameOccurrence>> vars = s
|
||||||
ASTName name = (ASTName) child;
|
.getDeclarations(VariableNameDeclaration.class);
|
||||||
Scope s = name.getScope();
|
for (VariableNameDeclaration d : vars.keySet()) {
|
||||||
final JavaNameOccurrence nameOccurrence = new JavaNameOccurrence(name, name.getImage());
|
// in case of simple lambda expression, the type
|
||||||
while (s != null) {
|
// might be unknown
|
||||||
if (s.contains(nameOccurrence)) {
|
if (d.getImage().equals(name.getImage()) && d.getTypeImage() != null) {
|
||||||
|
String typeName = d.getTypeImage();
|
||||||
|
typeName = qualifyTypeName(typeName);
|
||||||
|
Node declaringNode = qualifiedTypeNames.get(typeName);
|
||||||
|
type = new SimpleTypedNameDeclaration(typeName,
|
||||||
|
this.getEnclosingScope(SourceFileScope.class).resolveType(typeName),
|
||||||
|
determineSuper(declaringNode));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
s = s.getParent();
|
|
||||||
}
|
|
||||||
if (s != null) {
|
|
||||||
Map<VariableNameDeclaration, List<NameOccurrence>> vars = s
|
|
||||||
.getDeclarations(VariableNameDeclaration.class);
|
|
||||||
for (VariableNameDeclaration d : vars.keySet()) {
|
|
||||||
// in case of simple lambda expression, the type might be unknown
|
|
||||||
if (d.getImage().equals(name.getImage()) && d.getTypeImage() != null) {
|
|
||||||
String typeName = d.getTypeImage();
|
|
||||||
typeName = qualifyTypeName(typeName);
|
|
||||||
Node declaringNode = qualifiedTypeNames.get(typeName);
|
|
||||||
type = new SimpleTypedNameDeclaration(typeName, this.getEnclosingScope(
|
|
||||||
SourceFileScope.class).resolveType(typeName), determineSuper(declaringNode));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (child instanceof ASTLiteral) {
|
|
||||||
ASTLiteral literal = (ASTLiteral) child;
|
|
||||||
if (literal.isCharLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("char", literal.getType());
|
|
||||||
} else if (literal.isStringLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("String", literal.getType());
|
|
||||||
} else if (literal.isFloatLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("float", literal.getType());
|
|
||||||
} else if (literal.isDoubleLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("double", literal.getType());
|
|
||||||
} else if (literal.isIntLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("int", literal.getType());
|
|
||||||
} else if (literal.isLongLiteral()) {
|
|
||||||
type = new SimpleTypedNameDeclaration("long", literal.getType());
|
|
||||||
} else if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
|
|
||||||
type = new SimpleTypedNameDeclaration("boolean", Boolean.TYPE);
|
|
||||||
}
|
|
||||||
} else if (child instanceof ASTAllocationExpression
|
|
||||||
&& child.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
|
|
||||||
ASTClassOrInterfaceType classInterface = (ASTClassOrInterfaceType) child.jjtGetChild(0);
|
|
||||||
type = convertToSimpleType(classInterface);
|
|
||||||
}
|
|
||||||
if (type == null && !parameterTypes.isEmpty()) {
|
|
||||||
// replace the unknown type with the correct parameter type
|
|
||||||
// of the method.
|
|
||||||
// in case the argument is itself a method call, we can't
|
|
||||||
// determine the result type of the called
|
|
||||||
// method. Therefore the parameter type is used.
|
|
||||||
// This might cause confusion, if method overloading is
|
|
||||||
// used.
|
|
||||||
|
|
||||||
// the method might be vararg, so, there might be more
|
|
||||||
// arguments than parameterTypes
|
|
||||||
if (parameterTypes.size() > i) {
|
|
||||||
type = parameterTypes.get(i);
|
|
||||||
} else {
|
|
||||||
type = parameterTypes.get(parameterTypes.size() - 1); // last
|
|
||||||
// parameter
|
|
||||||
// is
|
|
||||||
// the
|
|
||||||
// vararg
|
|
||||||
// type
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type != null && type.getType() == null) {
|
} else if (child instanceof ASTLiteral) {
|
||||||
Class<?> typeBound = resolveGenericType(argument, type.getTypeImage());
|
ASTLiteral literal = (ASTLiteral) child;
|
||||||
if (typeBound != null) {
|
if (literal.isCharLiteral()) {
|
||||||
type = new SimpleTypedNameDeclaration(type.getTypeImage(), typeBound);
|
type = new SimpleTypedNameDeclaration("char", literal.getType());
|
||||||
}
|
} else if (literal.isStringLiteral()) {
|
||||||
|
type = new SimpleTypedNameDeclaration("String", literal.getType());
|
||||||
|
} else if (literal.isFloatLiteral()) {
|
||||||
|
type = new SimpleTypedNameDeclaration("float", literal.getType());
|
||||||
|
} else if (literal.isDoubleLiteral()) {
|
||||||
|
type = new SimpleTypedNameDeclaration("double", literal.getType());
|
||||||
|
} else if (literal.isIntLiteral()) {
|
||||||
|
type = new SimpleTypedNameDeclaration("int", literal.getType());
|
||||||
|
} else if (literal.isLongLiteral()) {
|
||||||
|
type = new SimpleTypedNameDeclaration("long", literal.getType());
|
||||||
|
} else if (literal.jjtGetNumChildren() == 1
|
||||||
|
&& literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
|
||||||
|
type = new SimpleTypedNameDeclaration("boolean", Boolean.TYPE);
|
||||||
}
|
}
|
||||||
argumentTypes.add(type);
|
} else if (child instanceof ASTAllocationExpression
|
||||||
|
&& child.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
|
||||||
|
ASTClassOrInterfaceType classInterface = (ASTClassOrInterfaceType) child.jjtGetChild(0);
|
||||||
|
type = convertToSimpleType(classInterface);
|
||||||
}
|
}
|
||||||
|
if (type == null && !parameterTypes.isEmpty()) {
|
||||||
|
// replace the unknown type with the correct parameter type
|
||||||
|
// of the method.
|
||||||
|
// in case the argument is itself a method call, we can't
|
||||||
|
// determine the result type of the called
|
||||||
|
// method. Therefore the parameter type is used.
|
||||||
|
// This might cause confusion, if method overloading is
|
||||||
|
// used.
|
||||||
|
|
||||||
|
// the method might be vararg, so, there might be more
|
||||||
|
// arguments than parameterTypes
|
||||||
|
if (parameterTypes.size() > i) {
|
||||||
|
type = parameterTypes.get(i);
|
||||||
|
} else {
|
||||||
|
// last parameter is the vararg type
|
||||||
|
type = parameterTypes.get(parameterTypes.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type != null && type.getType() == null) {
|
||||||
|
Class<?> typeBound = resolveGenericType(argument, type.getTypeImage());
|
||||||
|
if (typeBound != null) {
|
||||||
|
type = new SimpleTypedNameDeclaration(type.getTypeImage(), typeBound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
argumentTypes.add(type);
|
||||||
}
|
}
|
||||||
return argumentTypes;
|
return argumentTypes;
|
||||||
}
|
}
|
||||||
|
@ -191,7 +191,7 @@ public class JavaNameOccurrence implements NameOccurrence {
|
|||||||
* @return return true if image equal to 'this' or 'super'.
|
* @return return true if image equal to 'this' or 'super'.
|
||||||
*/
|
*/
|
||||||
public boolean isThisOrSuper() {
|
public boolean isThisOrSuper() {
|
||||||
return image != null && (image.equals(THIS) || image.equals(SUPER));
|
return THIS.equals(image) || SUPER.equals(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user