From 19a8d0d060949b5870442448dc6c03666d60d870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Sat, 12 Nov 2016 02:29:10 -0300 Subject: [PATCH 1/3] Minor optimizations to type resolution --- .../pmd/lang/java/symboltable/ClassScope.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java index b6c379d91d..8a6620f577 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java @@ -137,10 +137,9 @@ public class ClassScope extends AbstractJavaScope { } protected Set findVariableHere(JavaNameOccurrence occurrence) { - Set result = new HashSet<>(); Map> methodDeclarations = getMethodDeclarations(); Map> variableDeclarations = getVariableDeclarations(); - if (occurrence.isThisOrSuper() || occurrence.getImage() != null && occurrence.getImage().equals(className)) { + if (occurrence.isThisOrSuper() || className.equals(occurrence.getImage())) { if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) { // this could happen if you do this: // public class Foo { @@ -158,13 +157,12 @@ public class ClassScope extends AbstractJavaScope { // we'll look up Foo just to get a handle to the class scope // and then we'll look up X. if (!variableDeclarations.isEmpty()) { - result.add(variableDeclarations.keySet().iterator().next()); - return result; + return Collections.singleton(variableDeclarations.keySet().iterator().next()); } - result.add(methodDeclarations.keySet().iterator().next()); - return result; + return Collections.singleton(methodDeclarations.keySet().iterator().next()); } + Set result = new HashSet<>(); if (occurrence.isMethodOrConstructorInvocation()) { for (MethodNameDeclaration mnd : methodDeclarations.keySet()) { if (mnd.getImage().equals(occurrence.getImage())) { @@ -390,8 +388,9 @@ public class ClassScope extends AbstractJavaScope { 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(new JavaNameOccurrence(name, name.getImage()))) { + if (s.contains(nameOccurrence)) { break; } s = s.getParent(); From 810760e3a6b1145aaf66693866079d12748d24d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Sat, 12 Nov 2016 03:07:40 -0300 Subject: [PATCH 2/3] Update changelog --- src/site/markdown/overview/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 633c5f73eb..fb3f1002cf 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -15,6 +15,7 @@ * [#126](https://github.com/pmd/pmd/pull/126): \[java] Avoid creating a new String to qualify types * [#127](https://github.com/pmd/pmd/pull/127): \[java] Don't look twice for the same variables +* [#128](https://github.com/pmd/pmd/pull/128): \[java] Minor optimizations to type resolution * [#129](https://github.com/pmd/pmd/pull/129): \[plsql] Added correct parse of IS [NOT] NULL and multiline DML **Bugfixes:** From b5ca7baa9adb3d445658f4429c88cc8a630aa26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Tue, 15 Nov 2016 03:36:08 -0300 Subject: [PATCH 3/3] Ensure className is never null --- .../sourceforge/pmd/lang/java/symboltable/ClassScope.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java index 8a6620f577..95c4face17 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java @@ -9,6 +9,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; @@ -51,12 +52,12 @@ public class ClassScope extends AbstractJavaScope { } }; - private String className; + private final String className; private boolean isEnum; - public ClassScope(String className) { - this.className = className; + public ClassScope(final String className) { + this.className = Objects.requireNonNull(className); anonymousInnerClassCounter.set(Integer.valueOf(1)); }