A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/commit/493e0014b042bf1076244ad7b7c7e91a31f52d42 below:

Also fix a false negative · pmd/pmd@493e001 · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+20

-20

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+20

-20

lines changed Original file line number Diff line number Diff line change

@@ -107,12 +107,12 @@ private boolean couldBeMethodCall(JavaNode node) {

107 107

}

108 108 109 109

private void checkImports(TypeNode node, Object data) {

110 -

String name = node.getImage();

110 +

final String name = node.getImage();

111 111 112 112

// variable names shadow everything else

113 113

// If the first segment is a variable, then all

114 114

// the following are field accesses and it's not an FQCN

115 -

if (isVariable(node)) {

115 +

if (isVariable(node.getScope(), name)) {

116 116

return;

117 117

}

118 118

@@ -184,7 +184,7 @@ private void checkImports(TypeNode node, Object data) {

184 184

if (matches.isEmpty()) {

185 185

if (isJavaLangImplicit(node)) {

186 186

addViolation(data, node, new Object[] { node.getImage(), "java.lang.*", "implicit "});

187 -

} else if (isSamePackage(node)) {

187 +

} else if (isSamePackage(name)) {

188 188

addViolation(data, node, new Object[] { node.getImage(), currentPackage + ".*", "same package "});

189 189

}

190 190

} else {

@@ -223,19 +223,13 @@ private ASTImportDeclaration findFirstMatch(List<ASTImportDeclaration> imports)

223 223

return result;

224 224

}

225 225 226 -

private boolean isVariable(TypeNode node) {

227 -

String name = node.getImage();

226 +

private boolean isVariable(Scope scope, String name) {

228 227

String firstSegment = name.substring(0, name.indexOf('.'));

229 228 230 -

return isVariableInScope(node.getScope(), firstSegment);

231 -

}

232 - 233 -

private boolean isVariableInScope(Scope scope, String name) {

234 - 235 229

while (scope != null) {

236 230 237 231

for (Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {

238 -

if (entry.getKey().getName().equals(name)) {

232 +

if (entry.getKey().getName().equals(firstSegment)) {

239 233

return true;

240 234

}

241 235

}

@@ -246,9 +240,17 @@ private boolean isVariableInScope(Scope scope, String name) {

246 240

return false;

247 241

}

248 242 249 -

private boolean isSamePackage(TypeNode node) {

250 -

String name = node.getImage();

251 -

return name.substring(0, name.lastIndexOf('.')).equals(currentPackage);

243 +

private boolean isSamePackage(String name) {

244 +

int i = name.lastIndexOf('.');

245 +

while (i > 0) {

246 +

name = name.substring(0, i);

247 +

if (name.equals(currentPackage)) {

248 +

return true;

249 +

}

250 +

i = name.lastIndexOf('.');

251 +

}

252 + 253 +

return false;

252 254

}

253 255 254 256

private boolean isJavaLangImplicit(TypeNode node) {

Original file line number Diff line number Diff line change

@@ -488,7 +488,7 @@ public class TestArrayType {

488 488

}

489 489

]]></code>

490 490

</test-code>

491 - 491 + 492 492

<test-code>

493 493

<description>#1199 false negative for same package FQCN</description>

494 494

<expected-problems>1</expected-problems>

@@ -504,7 +504,7 @@ public class SamePackage {

504 504

</test-code>

505 505 506 506

<test-code>

507 -

<description>#1951 false positive when package name is shadowed by variable</description>

507 +

<description>#1951 false positive when package name is obscured by variable</description>

508 508

<expected-problems>0</expected-problems>

509 509

<code><![CDATA[

510 510

package threads;

@@ -519,15 +519,14 @@ public class FQNTest {

519 519

</test-code>

520 520 521 521

<test-code>

522 -

<description>False positive when package name is shadowed by variable (2)</description>

522 +

<description>False positive when package name is obscured by variable (2)</description>

523 523

<expected-problems>0</expected-problems>

524 524

<code><![CDATA[

525 525

package threads;

526 526 527 527

public class FQNTest {

528 528

public static void main(String[] args) {

529 529

Thread[] threads = new Thread[5];

530 -

// threads is obscured

531 530

int i = threads.length.foo;

532 531

}

533 532

}

@@ -558,15 +557,14 @@ class length {

558 557

</test-code>

559 558 560 559

<test-code>

561 -

<description>False positive when type name is shadowed by variable</description>

560 +

<description>False positive when type name is obscured by variable</description>

562 561

<expected-problems>0</expected-problems>

563 562

<code><![CDATA[

564 563 565 564

public class FQNTest {

566 565

public static void main(String[] args) {

567 566

length length = new length();

568 567

// the type name 'length' is obscured.

569 -

// 'length' here may only mean the variable

570 568

int i = length.foo;

571 569

}

572 570

}

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4