New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Analyzing enum with lambda passed in constructor fails with "The enclosing scope must exist." #1077

Closed
mhankus opened this Issue May 4, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@mhankus

mhankus commented May 4, 2018

Affects PMD Version:
6.3.0

Rule:

Description:

Inspection fails with exception

net.sourceforge.pmd.PMDException: Error while processing ....../FallbackFieldValue.java
	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:101)
	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:50)
	at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:75)
	at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:23)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: The enclosing scope must exist.
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.findLambdaScopeNameSegment(QualifiedNameResolver.java:379)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.visit(QualifiedNameResolver.java:322)
	at net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression.jjtAccept(ASTLambdaExpression.java:28)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:314)
	at net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix.jjtAccept(ASTPrimaryPrefix.java:42)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:310)
	at net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression.jjtAccept(ASTPrimaryExpression.java:21)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:230)
	at net.sourceforge.pmd.lang.java.ast.ASTExpression.jjtAccept(ASTExpression.java:21)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:338)
	at net.sourceforge.pmd.lang.java.ast.ASTArgumentList.jjtAccept(ASTArgumentList.java:21)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:334)
	at net.sourceforge.pmd.lang.java.ast.ASTArguments.jjtAccept(ASTArguments.java:28)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:54)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.visit(QualifiedNameResolver.java:240)
	at net.sourceforge.pmd.lang.java.ast.ASTEnumConstant.jjtAccept(ASTEnumConstant.java:27)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:50)
	at net.sourceforge.pmd.lang.java.ast.ASTEnumBody.jjtAccept(ASTEnumBody.java:21)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter.visit(JavaParserVisitorReducedAdapter.java:33)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.visit(QualifiedNameResolver.java:212)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter.visit(JavaParserVisitorReducedAdapter.java:28)
	at net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration.jjtAccept(ASTEnumDeclaration.java:22)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:46)
	at net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration.jjtAccept(ASTClassOrInterfaceBodyDeclaration.java:41)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:42)
	at net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody.jjtAccept(ASTClassOrInterfaceBody.java:21)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter.visit(JavaParserVisitorReducedAdapter.java:33)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.visit(QualifiedNameResolver.java:212)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter.visit(JavaParserVisitorReducedAdapter.java:16)
	at net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration.jjtAccept(ASTClassOrInterfaceDeclaration.java:55)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:158)
	at net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration.jjtAccept(ASTTypeDeclaration.java:35)
	at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:56)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:10)
	at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:138)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.visit(QualifiedNameResolver.java:132)
	at net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit.jjtAccept(ASTCompilationUnit.java:41)
	at net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver.initializeWith(QualifiedNameResolver.java:117)
	at net.sourceforge.pmd.lang.java.AbstractJavaHandler$8.start(AbstractJavaHandler.java:130)
	at net.sourceforge.pmd.SourceCodeProcessor.resolveQualifiedNames(SourceCodeProcessor.java:126)
	at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:181)
	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:95)

Code Sample demonstrating the issue:

This enum is defined inside bigger class.

 public enum FallbackType {
        CONST(config -> config.get("value")),
        CURRENT_TIMESTAMP(config -> TimeUtils.zonedDateTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

        private final Function<Map<String, Object>, Object> resolver;

        FallbackType(Function<Map<String, Object>, Object> resolver) {
            this.resolver = resolver;
        }
        @Nullable
        public Object resolve(Map<String, Object> configuration) {
            return this.resolver.apply(configuration);
        }
    }


Running PMD through: Maven

@jsotuyod jsotuyod self-assigned this May 4, 2018

@jsotuyod jsotuyod added the a:bug label May 4, 2018

@jsotuyod jsotuyod added this to the 6.4.0 milestone May 4, 2018

@jsotuyod

This comment has been minimized.

Show comment
Hide comment
@jsotuyod

jsotuyod May 4, 2018

Member

@mhankus thanks for the report! This may be down to some of the scoping oddities being tackled in #1068, but I'll investigate and let you know. Definitely targeted for 6.4.0

Member

jsotuyod commented May 4, 2018

@mhankus thanks for the report! This may be down to some of the scoping oddities being tackled in #1068, but I'll investigate and let you know. Definitely targeted for 6.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment