diff --git a/.all-contributorsrc b/.all-contributorsrc index 1c882017e1..10051359fc 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -6835,6 +6835,15 @@ "contributions": [ "financial" ] + }, + { + "login": "OlegAndreych", + "name": "Oleg Andreych", + "avatar_url": "https://avatars.githubusercontent.com/u/2041351?v=4", + "profile": "https://github.com/OlegAndreych", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index 33f61a966d..91201439cf 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -530,444 +530,445 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
Noam Tamim

πŸ›
Noel Grandin

πŸ›
Olaf Haalstra

πŸ› +
Oleg Andreych

πŸ’»
Oleg Pavlenko

πŸ›
Oleksii Dykov

πŸ’»
Oliver Eikemeier

πŸ› -
Oliver Siegmar

πŸ’΅ +
Oliver Siegmar

πŸ’΅
Olivier Parent

πŸ’» πŸ›
Ollie Abbey

πŸ’» πŸ›
OverDrone

πŸ›
Ozan Gulle

πŸ’» πŸ›
PUNEET JAIN

πŸ›
Parbati Bose

πŸ› -
Paul Berg

πŸ› +
Paul Berg

πŸ›
Pavel Bludov

πŸ›
Pavel Mička

πŸ›
Pedro Nuno Santos

πŸ›
Pedro Rijo

πŸ›
Pelisse Romain

πŸ’» πŸ“– πŸ›
Per Abich

πŸ’» -
Pete Davids

πŸ› +
Pete Davids

πŸ›
Peter Bruin

πŸ›
Peter Chittum

πŸ’» πŸ›
Peter Cudmore

πŸ›
Peter Kasson

πŸ›
Peter Kofler

πŸ›
Peter Paul Bakker

πŸ’» -
Pham Hai Trung

πŸ› +
Pham Hai Trung

πŸ›
Philip Graf

πŸ’» πŸ›
Philip Hachey

πŸ›
Philippe Ozil

πŸ›
Phinehas Artemix

πŸ›
Phokham Nonava

πŸ›
Piotr SzymaΕ„ski

πŸ› -
Piotrek Ε»ygieΕ‚o

πŸ’» πŸ› πŸ“– +
Piotrek Ε»ygieΕ‚o

πŸ’» πŸ› πŸ“–
Pranay Jaiswal

πŸ›
Prasad Kamath

πŸ›
Prasanna

πŸ›
Presh-AR

πŸ›
Puneet1726

πŸ›
Rafael CortΓͺs

πŸ› -
RaheemShaik999

πŸ› +
RaheemShaik999

πŸ›
RajeshR

πŸ’» πŸ›
Ramachandra Mohan

πŸ›
Ramel0921

πŸ›
Raquel Pau

πŸ›
Ravikiran Janardhana

πŸ›
Reda Benhemmouche

πŸ› -
Renato Oliveira

πŸ’» πŸ› +
Renato Oliveira

πŸ’» πŸ›
Rich DiCroce

πŸ›
Riot R1cket

πŸ›
Rishabh Jain

πŸ›
RishabhDeep Singh

πŸ›
Robbie Martinus

πŸ’» πŸ›
Robert Henry

πŸ› -
Robert Painsi

πŸ› +
Robert Painsi

πŸ›
Robert Russell

πŸ›
Robert SΓΆsemann

πŸ’» πŸ“– πŸ“’ πŸ›
Robert Whitebit

πŸ›
Robin Richtsfeld

πŸ›
Robin Stocker

πŸ’» πŸ›
Robin Wils

πŸ› -
RochusOest

πŸ› +
RochusOest

πŸ›
Rodolfo Noviski

πŸ›
Rodrigo Casara

πŸ›
Rodrigo Fernandes

πŸ›
Roman Salvador

πŸ’» πŸ›
Ronald Blaschke

πŸ›
RΓ³bert Papp

πŸ› -
Saikat Sengupta

πŸ› +
Saikat Sengupta

πŸ›
Saksham Handu

πŸ›
Saladoc

πŸ›
Salesforce Bob Lightning

πŸ›
Sam Carlberg

πŸ›
Satoshi Kubo

πŸ›
Scott Kennedy

πŸ› -
Scott Wells

πŸ› πŸ’» +
Scott Wells

πŸ› πŸ’»
Scrsloota

πŸ’»
Sebastian BΓΆgl

πŸ›
Sebastian Schuberth

πŸ›
Sebastian Schwarz

πŸ›
Sergey Gorbaty

πŸ›
Sergey Kozlov

πŸ› -
Sergey Yanzin

πŸ’» πŸ› +
Sergey Yanzin

πŸ’» πŸ›
Seth Wilcox

πŸ’»
Shubham

πŸ’» πŸ›
Simon Abykov

πŸ’»
Simon Xiao

πŸ›
Srinivasan Venkatachalam

πŸ›
Stanislav Gromov

πŸ› -
Stanislav Myachenkov

πŸ’» +
Stanislav Myachenkov

πŸ’»
Stefan Birkner

πŸ›
Stefan Bohn

πŸ›
Stefan Endrullis

πŸ›
Stefan KlΓΆss-Schuster

πŸ›
Stefan Wolf

πŸ›
Stephan H. Wissel

πŸ› -
Stephen

πŸ› +
Stephen

πŸ›
Stephen Friedrich

πŸ›
Steve Babula

πŸ’»
Stexxe

πŸ›
Stian LΓ₯gstad

πŸ›
StuartClayton5

πŸ›
Supun Arunoda

πŸ› -
Suren Abrahamyan

πŸ› +
Suren Abrahamyan

πŸ›
Suvashri

πŸ“–
SwatiBGupta1110

πŸ›
SyedThoufich

πŸ›
Szymon Sasin

πŸ›
T-chuangxin

πŸ›
TERAI Atsuhiro

πŸ› -
TIOBE Software

πŸ’» πŸ› +
TIOBE Software

πŸ’» πŸ›
Taylor Smock

πŸ›
Techeira DamiΓ‘n

πŸ’» πŸ›
Ted Husted

πŸ›
TehBakker

πŸ›
The Gitter Badger

πŸ›
Theodoor

πŸ› -
Thiago Henrique HΓΌpner

πŸ› +
Thiago Henrique HΓΌpner

πŸ›
Thibault Meyer

πŸ›
Thomas GΓΌttler

πŸ›
Thomas Jones-Low

πŸ›
Thomas Smith

πŸ’» πŸ›
ThrawnCA

πŸ›
Thunderforge

πŸ’» πŸ› -
Tim van der Lippe

πŸ› +
Tim van der Lippe

πŸ›
Tobias Weimer

πŸ’» πŸ›
Tom Daly

πŸ›
Tomer Figenblat

πŸ›
Tomi De Lucca

πŸ’» πŸ›
Torsten Kleiber

πŸ›
TrackerSB

πŸ› -
Ullrich Hafner

πŸ› +
Ullrich Hafner

πŸ›
Utku Cuhadaroglu

πŸ’» πŸ›
Valentin Brandl

πŸ›
Valeria

πŸ›
Vasily Anisimov

πŸ›
Vibhor Goyal

πŸ›
Vickenty Fesunov

πŸ› -
Victor NoΓ«l

πŸ› +
Victor NoΓ«l

πŸ›
Vincent Galloy

πŸ’»
Vincent HUYNH

πŸ›
Vincent Maurin

πŸ›
Vincent Privat

πŸ›
Vishhwas

πŸ›
Vitaly

πŸ› -
Vitaly Polonetsky

πŸ› +
Vitaly Polonetsky

πŸ›
Vojtech Polivka

πŸ›
Vsevolod Zholobov

πŸ›
Vyom Yadav

πŸ’»
Wang Shidong

πŸ›
Waqas Ahmed

πŸ›
Wayne J. Earl

πŸ› -
Wchenghui

πŸ› +
Wchenghui

πŸ›
Will Winder

πŸ›
William Brockhus

πŸ’» πŸ›
Wilson Kurniawan

πŸ›
Wim Deblauwe

πŸ›
Woongsik Choi

πŸ›
XenoAmess

πŸ’» πŸ› -
Yang

πŸ’» +
Yang

πŸ’»
YaroslavTER

πŸ›
Young Chan

πŸ’» πŸ›
YuJin Kim

πŸ›
Yuri Dolzhenko

πŸ›
Yurii Dubinka

πŸ›
Zoltan Farkas

πŸ› -
Zustin

πŸ› +
Zustin

πŸ›
aaronhurst-google

πŸ› πŸ’»
alexmodis

πŸ›
andreoss

πŸ›
andrey81inmd

πŸ’» πŸ›
anicoara

πŸ›
arunprasathav

πŸ› -
asiercamara

πŸ› +
asiercamara

πŸ›
astillich-igniti

πŸ’»
avesolovksyy

πŸ›
avishvat

πŸ›
avivmu

πŸ›
axelbarfod1

πŸ›
b-3-n

πŸ› -
balbhadra9

πŸ› +
balbhadra9

πŸ›
base23de

πŸ›
bergander

πŸ›
berkam

πŸ’» πŸ›
breizh31

πŸ›
caesarkim

πŸ›
carolyujing

πŸ› -
cesares-basilico

πŸ› +
cesares-basilico

πŸ›
chrite

πŸ›
cobratbq

πŸ›
coladict

πŸ›
cosmoJFH

πŸ›
cristalp

πŸ›
crunsk

πŸ› -
cwholmes

πŸ› +
cwholmes

πŸ›
cyberjj999

πŸ›
cyw3

πŸ›
d1ss0nanz

πŸ›
dalizi007

πŸ’»
danbrycefairsailcom

πŸ›
dariansanity

πŸ› -
darrenmiliband

πŸ› +
darrenmiliband

πŸ›
davidburstrom

πŸ›
dbirkman-paloalto

πŸ›
deepak-patra

πŸ›
dependabot[bot]

πŸ’» πŸ›
dinesh150

πŸ›
diziaq

πŸ› -
dreaminpast123

πŸ› +
dreaminpast123

πŸ›
duanyanan

πŸ›
dutt-sanjay

πŸ›
dylanleung

πŸ›
dzeigler

πŸ›
ekkirala

πŸ›
emersonmoura

πŸ› -
fairy

πŸ› +
fairy

πŸ›
filiprafalowicz

πŸ’»
foxmason

πŸ›
frankegabor

πŸ›
frankl

πŸ›
freafrea

πŸ›
fsapatin

πŸ› -
gracia19

πŸ› +
gracia19

πŸ›
guo fei

πŸ›
gurmsc5

πŸ›
gwilymatgearset

πŸ’» πŸ›
haigsn

πŸ›
hemanshu070

πŸ›
henrik242

πŸ› -
hongpuwu

πŸ› +
hongpuwu

πŸ›
hvbtup

πŸ’» πŸ›
igniti GmbH

πŸ›
ilovezfs

πŸ›
itaigilo

πŸ›
jakivey32

πŸ›
jbennett2091

πŸ› -
jcamerin

πŸ› +
jcamerin

πŸ›
jkeener1

πŸ›
jmetertea

πŸ›
johnra2

πŸ’»
josemanuelrolon

πŸ’» πŸ›
kabroxiko

πŸ’» πŸ›
karwer

πŸ› -
kaulonline

πŸ› +
kaulonline

πŸ›
kdaemonv

πŸ›
kenji21

πŸ’» πŸ›
kfranic

πŸ›
khalidkh

πŸ›
krzyk

πŸ›
lasselindqvist

πŸ› -
lgemeinhardt

πŸ› +
lgemeinhardt

πŸ›
lihuaib

πŸ›
lonelyma1021

πŸ›
lpeddy

πŸ›
lujiefsi

πŸ’»
lukelukes

πŸ’»
lyriccoder

πŸ› -
marcelmore

πŸ› +
marcelmore

πŸ›
matchbox

πŸ›
matthiaskraaz

πŸ›
meandonlyme

πŸ›
mikesive

πŸ›
milossesic

πŸ›
mohan-chinnappan-n

πŸ’» -
mriddell95

πŸ› +
mriddell95

πŸ›
mrlzh

πŸ›
msloan

πŸ›
mucharlaravalika

πŸ›
mvenneman

πŸ›
nareshl119

πŸ›
nicolas-harraudeau-sonarsource

πŸ› -
noerremark

πŸ› +
noerremark

πŸ›
novsirion

πŸ›
oggboy

πŸ›
oinume

πŸ›
orimarko

πŸ’» πŸ›
pacvz

πŸ’»
pallavi agarwal

πŸ› -
parksungrin

πŸ› +
parksungrin

πŸ›
patpatpat123

πŸ›
patriksevallius

πŸ›
pbrajesh1

πŸ›
phoenix384

πŸ›
piotrszymanski-sc

πŸ’»
plan3d

πŸ› -
poojasix

πŸ› +
poojasix

πŸ›
prabhushrikant

πŸ›
pujitha8783

πŸ›
r-r-a-j

πŸ›
raghujayjunk

πŸ›
rajeshveera

πŸ›
rajeswarreddy88

πŸ› -
recdevs

πŸ› +
recdevs

πŸ›
reudismam

πŸ’» πŸ›
rijkt

πŸ›
rillig-tk

πŸ›
rmohan20

πŸ’» πŸ›
rxmicro

πŸ›
ryan-gustafson

πŸ’» πŸ› -
sabi0

πŸ› +
sabi0

πŸ›
scais

πŸ›
sebbASF

πŸ›
sergeygorbaty

πŸ’»
shilko2013

πŸ›
shiomiyan

πŸ“–
simeonKondr

πŸ› -
snajberk

πŸ› +
snajberk

πŸ›
sniperrifle2004

πŸ›
snuyanzin

πŸ› πŸ’»
sratz

πŸ›
stonio

πŸ›
sturton

πŸ’» πŸ›
sudharmohan

πŸ› -
suruchidawar

πŸ› +
suruchidawar

πŸ›
svenfinitiv

πŸ›
tashiscool

πŸ›
test-git-hook

πŸ›
testation21

πŸ’» πŸ›
thanosa

πŸ›
tiandiyixian

πŸ› -
tobwoerk

πŸ› +
tobwoerk

πŸ›
tprouvot

πŸ› πŸ’»
trentchilders

πŸ›
triandicAnt

πŸ›
trishul14

πŸ›
tsui

πŸ›
winhkey

πŸ› -
witherspore

πŸ› +
witherspore

πŸ›
wjljack

πŸ›
wuchiuwong

πŸ›
xingsong

πŸ›
xioayuge

πŸ›
xnYi9wRezm

πŸ’» πŸ›
xuanuy

πŸ› -
xyf0921

πŸ› +
xyf0921

πŸ›
yalechen-cyw3

πŸ›
yasuharu-sato

πŸ›
zenglian

πŸ›
zgrzyt93

πŸ’» πŸ›
zh3ng

πŸ›
zt_soft

πŸ› -
ztt79

πŸ› +
ztt79

πŸ›
zzzzfeng

πŸ›
ÁrpÑd MagosÑnyi

πŸ›
任贡杰

πŸ› diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 929bc6e4cc..57478e11d7 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -27,6 +27,8 @@ from Lua. This means, that the Lua language in PMD can now parse both Lua and Lu * doc * [#4109](https://github.com/pmd/pmd/pull/4109): \[doc] Add page for 3rd party rulesets * [#4124](https://github.com/pmd/pmd/pull/4124): \[doc] Fix typos in Java rule docs +* java-codestyle + * [#4085](https://github.com/pmd/pmd/issues/4085): \[java] UnnecessaryFullyQualifiedName false positive when nested and non-nested classes with the same name and in the same package are used together ### API Changes @@ -44,6 +46,7 @@ Many thanks to our sponsors: * [#4066](https://github.com/pmd/pmd/pull/4066): \[lua] Add support for Luau syntax and skipping literal sequences in CPD - [Matt Hargett](https://github.com/matthargett) (@matthargett) * [#4116](https://github.com/pmd/pmd/pull/4116): \[core] Fix missing --file arg in TreeExport CLI example - [mohan-chinnappan-n](https://github.com/mohan-chinnappan-n) (@mohan-chinnappan-n) * [#4124](https://github.com/pmd/pmd/pull/4124) : \[doc] Fix typos in Java rule docs - [Piotrek Ε»ygieΕ‚o](https://github.com/pzygielo) (@pzygielo) +* [#4128](https://github.com/pmd/pmd/pull/4128): \[java] Fix False-positive UnnecessaryFullyQualifiedName when nested and non-nest… #4103 - [Oleg Andreych](https://github.com/OlegAndreych) (@OlegAndreych) * [#4131](https://github.com/pmd/pmd/pull/4131): \[doc] TooFewBranchesForASwitchStatement - Use "if-else" instead of "if-then" - [Suvashri](https://github.com/Suvashri) (@Suvashri) {% endtocmaker %} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index 32bf8fa21c..8091d944e3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -17,7 +17,9 @@ import java.util.logging.Logger; import org.apache.commons.lang3.StringUtils; import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; +import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTNameList; @@ -186,9 +188,12 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { if (matches.isEmpty()) { if (isJavaLangImplicit(node)) { - addViolation(data, node, new Object[] { node.getImage(), "java.lang.*", "implicit "}); + asCtx(data).addViolation(node, + node.getImage(), "java.lang.*", "implicit "); } else if (isSamePackage(node, name)) { - addViolation(data, node, new Object[] { node.getImage(), currentPackage + ".*", "same package "}); + if (!hasSameSimpleNameInScope(node)) { + asCtx(data).addViolation(node, node.getImage(), currentPackage + ".*", "same package "); + } } } else { ASTImportDeclaration firstMatch = findFirstMatch(matches); @@ -199,11 +204,29 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { String importStr = firstMatch.getImportedName() + (firstMatch.isImportOnDemand() ? ".*" : ""); String type = firstMatch.isStatic() ? "static " : ""; - addViolation(data, node, new Object[] { node.getImage(), importStr, type }); + asCtx(data).addViolation(node, node.getImage(), importStr, type); } } } + private boolean hasSameSimpleNameInScope(TypeNode node) { + final ASTCompilationUnit root = node.getRoot(); + final List declarationDescendants = root.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); + final Class nodeType = node.getType(); + + if (nodeType == null) { + return false; + } + + for (ASTClassOrInterfaceDeclaration declarationDescendant : declarationDescendants) { + if (nodeType.getSimpleName().equals(declarationDescendant.getSimpleName()) + && !nodeType.getName().equals(declarationDescendant.getQualifiedName().toString())) { + return true; + } + } + return false; + } + private ASTImportDeclaration findFirstMatch(List imports) { // first search only static imports ASTImportDeclaration result = null; @@ -404,7 +427,7 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { // Is it a conflict with a class in the same file? final Set qualifiedTypes = node.getScope().getEnclosingScope(SourceFileScope.class) - .getQualifiedTypeNames().keySet(); + .getQualifiedTypeNames().keySet(); for (final String qualified : qualifiedTypes) { int fullLength = qualified.length(); if (qualified.endsWith(unqualifiedName) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml index f95d7bc81e..097e096767 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml @@ -622,6 +622,40 @@ public class UnnecessaryFullyQualifiedName { ]]> + + False positive when same package inner class is referenced (not enum) #4085 + 0 + + + + + Should report fully-qualified name usage of a class in itself #4085 + 1 + 4 + + + #2098 false positive with annotated package 0