forked from phoedos/pmd
Merge pull request #3197 from
oowekyala:issue2757-closeResource-Cleanup-annot [java] Fix #2757: make CloseResource support @lombok.Cleanup #3197
This commit is contained in:
commit
a55f760d7d
@ -25,6 +25,8 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
* java-bestpractices
|
||||
* [#3190](https://github.com/pmd/pmd/issues/3190): \[java] Use StandardCharsets instead of Charset.forName
|
||||
* java-errorprone
|
||||
* [#2757](https://github.com/pmd/pmd/issues/2757): \[java] CloseResource: support Lombok's @Cleanup annotation
|
||||
|
||||
### API Changes
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.annotation.InternalApi;
|
||||
@ -45,6 +46,11 @@ public class ASTLocalVariableDeclaration extends AbstractJavaAccessNode implemen
|
||||
return visitor.visit(this, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ASTAnnotation> getDeclaredAnnotations() {
|
||||
return findChildrenOfType(ASTAnnotation.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSuppressWarningsAnnotationFor(Rule rule) {
|
||||
for (int i = 0; i < getNumChildren(); i++) {
|
||||
|
@ -45,6 +45,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.ast.Annotatable;
|
||||
import net.sourceforge.pmd.lang.java.ast.JavaNode;
|
||||
import net.sourceforge.pmd.lang.java.ast.TypeNode;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
@ -187,6 +188,10 @@ public class CloseResourceRule extends AbstractJavaRule {
|
||||
List<ASTVariableDeclarator> vars = method.findDescendantsOfType(ASTVariableDeclarator.class);
|
||||
Map<ASTVariableDeclarator, TypeNode> resVars = new HashMap<>();
|
||||
for (ASTVariableDeclarator var : vars) {
|
||||
if (var.getParent() instanceof Annotatable
|
||||
&& ((Annotatable) var.getParent()).isAnnotationPresent("lombok.Cleanup")) {
|
||||
continue; // auto cleaned up
|
||||
}
|
||||
TypeNode varType = getTypeOfVariable(var);
|
||||
if (varType != null && isResourceTypeOrSubtype(varType)) {
|
||||
resVars.put(var, wrappedResourceTypeOrReturn(var, varType));
|
||||
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.errorprone.closeresource;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Helper class for #2757
|
||||
*/
|
||||
public class FakeContext implements Closeable, AutoCloseable {
|
||||
|
||||
public <T> T getBean(Class<T> klass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
}
|
@ -1659,4 +1659,25 @@ public class FalsePositive {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#2757 support @lombok.Cleanup annotation</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
package net.sourceforge.pmd.lang.java.rule.errorprone.closeresource;
|
||||
import lombok.Cleanup;
|
||||
import lombok.val;
|
||||
|
||||
public class Mwe2757 {
|
||||
private static SomeClass getCreator() {
|
||||
@Cleanup val context = new FakeContext(); // FakeContext is is the package
|
||||
return context.getBean(SomeClass.class);
|
||||
}
|
||||
|
||||
public Mwe2757() {
|
||||
|
||||
}
|
||||
static class SomeClass {}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Loading…
x
Reference in New Issue
Block a user