Skip to content

[java] Update rule AvoidInstantiatingObjectsInLoops#3394

Merged
oowekyala merged 4 commits into
pmd:pmd/7.0.xfrom
adangel:pmd7-update-AvoidInstantiatingObjectsInLoops
Aug 7, 2021
Merged

[java] Update rule AvoidInstantiatingObjectsInLoops#3394
oowekyala merged 4 commits into
pmd:pmd/7.0.xfrom
adangel:pmd7-update-AvoidInstantiatingObjectsInLoops

Conversation

@adangel

@adangel adangel commented Jul 9, 2021

Copy link
Copy Markdown
Member

@adangel adangel added this to the 7.0.0 milestone Jul 9, 2021
@ghost

ghost commented Jul 9, 2021

Copy link
Copy Markdown
2 Messages
📖 Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 1651 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
📖 Compared to master:
This changeset changes 30191 violations,
introduces 32731 new violations, 3 new errors and 0 new configuration errors,
removes 142259 violations, 8 errors and 3 configuration errors.
Full report
Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 1668 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
Compared to master:
This changeset changes 30191 violations,
introduces 32746 new violations, 3 new errors and 0 new configuration errors,
removes 142257 violations, 8 errors and 3 configuration errors.
Full report
Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 1537 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 0 configuration errors.
Full report
Compared to master:
This changeset changes 0 violations,
introduces 28 new violations, 1 new errors and 0 new configuration errors,
removes 229 violations, 8 errors and 3 configuration errors.
Full report

Generated by 🚫 Danger

@adangel

adangel commented Jul 10, 2021

Copy link
Copy Markdown
Member Author
  1. false-negative:
WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);

(https://github.com/checkstyle/checkstyle/blob/checkstyle-8.10/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java#L209)

ArrayAllocations are not considered anymore obviously....

  1. fixed false-positive:
for (String configName : new String[] {"config", "test"}) {

(https://github.com/checkstyle/checkstyle/blob/checkstyle-8.10/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java#L1386)

We should add a test case for this.

  1. fixed false-positive:
this.propertyValueList.add(new PropertyValue(pv));

(https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java#L73)

Maybe worth to add a test case - I guess in pmd6 we stumble over "this." ...

  1. fixed false-positive:
this.configResources[i] = new ClassPathResource(paths[i], clazz);

(https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java#L200)

Same as 3 - we probably stumble in pmd6 over "this." for this array access. Maybe worth a test case.

  1. accidentally fixed false-positive:
dataBuffers.add(this.bufferFactory.wrap(new byte[]{b}));

(https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java#L87)

I think, this is not reported because of the array allocation. But we should make sure, that this is not reported at all, because it is part of the "Collection.add" call - which should be ignored. In this case, it is not the direct argument, but only indirectly.

  1. false-positive:
cookieStrings[i] = new ToStringCreator(cookie)...

(https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java#L271)

It's assigning it to an array so it should be ignored. Maybe we don't recognize the method call chain correctly...

Update: All of these have been fixed ✔️

@adangel

adangel commented Jul 15, 2021

Copy link
Copy Markdown
Member Author

New round:

  1. false positive
powerCache[i] = new BigInteger[] { BigInteger.valueOf(i) };

(https://github.com/openjdk/jdk/blob/jdk-11%2B28/src/java.base/share/classes/java/math/BigInteger.java#L1255)

ArrayAllocation should also be ignored if it is assigned to an array (like collection).

  1. false positive
return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())};

(https://github.com/openjdk/jdk/blob/jdk-11%2B28/src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java#L316)

ArrayAllocation should also be ignored, if the array is returned and therefore the loop is exited.

  1. fixed false positive - need to add a test
pathResolver.setAllowedLocations(getLocations().toArray(new Resource[0]));

(https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java#L390)

getLocations() returns a Collection, so the ArrayAllocation is used in a collection method call. That's correct, this usage should not be flagged.

Update: All of these have been fixed ✔️

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think this is fine

@oowekyala oowekyala self-assigned this Aug 7, 2021
@oowekyala oowekyala merged commit 623e5db into pmd:pmd/7.0.x Aug 7, 2021
@adangel adangel deleted the pmd7-update-AvoidInstantiatingObjectsInLoops branch August 19, 2021 08:29
@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants