Merge branch 'pr-162' into pmd/5.5.x

Closes #162 (rebased onto pmd/5.4.x)
This commit is contained in:
Andreas Dangel
2017-01-03 11:45:47 +01:00
2 changed files with 107 additions and 98 deletions

View File

@ -182,15 +182,14 @@ public class ClassScope extends AbstractJavaScope {
Set<NameDeclaration> result = new HashSet<>();
if (occurrence.isMethodOrConstructorInvocation()) {
final boolean hasAuxclasspath = getEnclosingScope(SourceFileScope.class).hasAuxclasspath();
for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
if (mnd.getImage().equals(occurrence.getImage())) {
List<TypedNameDeclaration> parameterTypes = determineParameterTypes(mnd);
List<TypedNameDeclaration> argumentTypes = determineArgumentTypes(occurrence, parameterTypes);
if (!mnd.isVarargs()
&& occurrence.getArgumentCount() == mnd.getParameterCount()
&& (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath() || parameterTypes
.equals(argumentTypes))) {
if (!mnd.isVarargs() && occurrence.getArgumentCount() == mnd.getParameterCount()
&& (!hasAuxclasspath || parameterTypes.equals(argumentTypes))) {
result.add(mnd);
} else if (mnd.isVarargs()) {
int varArgIndex = parameterTypes.size() - 1;
@ -201,11 +200,12 @@ public class ClassScope extends AbstractJavaScope {
// or the calling method has enough arguments to fill in
// the parameters before the vararg
if ((varArgIndex == 0 || argumentTypes.size() >= varArgIndex)
&& (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath() || parameterTypes
&& (!hasAuxclasspath || parameterTypes
.subList(0, varArgIndex).equals(argumentTypes.subList(0, varArgIndex)))) {
if (!getEnclosingScope(SourceFileScope.class).hasAuxclasspath()) {
if (!hasAuxclasspath) {
result.add(mnd);
continue;
}
boolean sameType = true;
@ -319,8 +319,7 @@ public class ClassScope extends AbstractJavaScope {
}
}
MethodNameDeclaration mnd = new MethodNameDeclaration(methodDeclarator);
return mnd;
return new MethodNameDeclaration(methodDeclarator);
}
/**
@ -332,9 +331,16 @@ public class ClassScope extends AbstractJavaScope {
* @return List of types
*/
private List<TypedNameDeclaration> determineParameterTypes(MethodNameDeclaration mnd) {
List<TypedNameDeclaration> parameterTypes = new ArrayList<>();
List<ASTFormalParameter> parameters = mnd.getMethodNameDeclaratorNode().findDescendantsOfType(
ASTFormalParameter.class);
List<ASTFormalParameter> parameters = mnd.getMethodNameDeclaratorNode()
.findDescendantsOfType(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) {
String typeImage = p.getTypeNode().getTypeImage();
// 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.
// It might also refer to a type, that is imported.
typeImage = qualifyTypeName(typeImage);
Node declaringNode = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().get(typeImage);
Class<?> resolvedType = this.getEnclosingScope(SourceFileScope.class).resolveType(typeImage);
Node declaringNode = qualifiedTypeNames.get(typeImage);
Class<?> resolvedType = fileScope.resolveType(typeImage);
if (resolvedType == null) {
resolvedType = resolveGenericType(p, typeImage);
}
@ -388,20 +394,25 @@ public class ClassScope extends AbstractJavaScope {
*/
private List<TypedNameDeclaration> determineArgumentTypes(JavaNameOccurrence occurrence,
List<TypedNameDeclaration> parameterTypes) {
List<TypedNameDeclaration> argumentTypes = new ArrayList<>();
Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
ASTArgumentList arguments = null;
Node nextSibling = null;
Node nextSibling;
if (occurrence.getLocation() instanceof ASTPrimarySuffix) {
nextSibling = getNextSibling(occurrence.getLocation());
} else {
nextSibling = getNextSibling(occurrence.getLocation().jjtGetParent());
}
if (nextSibling != null) {
arguments = nextSibling.getFirstDescendantOfType(ASTArgumentList.class);
}
if (arguments != null) {
if (arguments == null) {
return Collections.emptyList();
}
List<TypedNameDeclaration> argumentTypes = new ArrayList<>(arguments.jjtGetNumChildren());
Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
for (int i = 0; i < arguments.jjtGetNumChildren(); i++) {
Node argument = arguments.jjtGetChild(i);
Node child = null;
@ -426,13 +437,15 @@ public class ClassScope extends AbstractJavaScope {
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
// 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));
type = new SimpleTypedNameDeclaration(typeName,
this.getEnclosingScope(SourceFileScope.class).resolveType(typeName),
determineSuper(declaringNode));
break;
}
}
@ -451,7 +464,8 @@ public class ClassScope extends AbstractJavaScope {
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) {
} else if (literal.jjtGetNumChildren() == 1
&& literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
type = new SimpleTypedNameDeclaration("boolean", Boolean.TYPE);
}
} else if (child instanceof ASTAllocationExpression
@ -473,12 +487,8 @@ public class ClassScope extends AbstractJavaScope {
if (parameterTypes.size() > i) {
type = parameterTypes.get(i);
} else {
type = parameterTypes.get(parameterTypes.size() - 1); // last
// parameter
// is
// the
// vararg
// type
// last parameter is the vararg type
type = parameterTypes.get(parameterTypes.size() - 1);
}
}
if (type != null && type.getType() == null) {
@ -489,7 +499,6 @@ public class ClassScope extends AbstractJavaScope {
}
argumentTypes.add(type);
}
}
return argumentTypes;
}

View File

@ -191,7 +191,7 @@ public class JavaNameOccurrence implements NameOccurrence {
* @return return true if image equal to 'this' or 'super'.
*/
public boolean isThisOrSuper() {
return image != null && (image.equals(THIS) || image.equals(SUPER));
return THIS.equals(image) || SUPER.equals(image);
}
/**