[java] AssertionError "this should be unreachable" with scala library #5283
Closed
Description
Affects PMD Version: 7.3.0, 7.4.0, 7.5.0, 7.60
Make sure, to test with the latest PMD version.
Description:
We use a lot of Spark. Our PMD setup is with the maven pmd plugin. We are attempting to upgrade from PMD plugin 3.21.2 which uses PMD v 6.55.0. At that version we are able to run mvn pmd:pmd
(or mvn clean install
successfully.
Upon upgrading to PMD plugin 3.25.0 using PMD 7.3.0, we began encountering errors when scanning files that use Scala. I have also tested with PMD 7.4.0, 7.5.0, and 7.6.0 by configuring the PMD version at runtime
Exception Stacktrace:
Exception in thread "main" java.lang.AssertionError: This should be unreachable: private field of an interface? scala/PartialFunction$AndThen, modifiers: public private static
at net.sourceforge.pmd.util.AssertionUtil.shouldNotReachHere(AssertionUtil.java:223)
at net.sourceforge.pmd.util.AssertionUtil.shouldNotReachHere(AssertionUtil.java:216)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.isAccessibleIn(JavaResolvers.java:482)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.lambda$hidingWalkResolvers$3(JavaResolvers.java:383)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.processDeclarations(JavaResolvers.java:435)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.walkSelf(JavaResolvers.java:412)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.walkSelf(JavaResolvers.java:417)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.hidingWalkResolvers(JavaResolvers.java:386)
at net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers.inheritedMembersResolvers(JavaResolvers.java:358)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymTableFactory.typeBody(SymTableFactory.java:395)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.visitTypeDecl(SymbolTableResolver.java:283)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.visit(SymbolTableResolver.java:319)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.visit(SymbolTableResolver.java:138)
at net.sourceforge.pmd.lang.java.ast.ASTAnonymousClassDeclaration.acceptVisitor(ASTAnonymousClassDeclaration.java:63)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.traverse(SymbolTableResolver.java:164)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver.traverse(SymbolTableResolver.java:101)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.lambda$process$1(JavaAstProcessor.java:132)
at net.sourceforge.pmd.benchmark.TimeTracker.bench(TimeTracker.java:163)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:132)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:166)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:150)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:69)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:25)
at net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter.parse(JjtreeParserAdapter.java:36)
at net.sourceforge.pmd.lang.impl.PmdRunnable.parse(PmdRunnable.java:112)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:132)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Code Sample demonstrating the issue:
package org.example;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.List;
import scala.Option;
import scala.PartialFunction;
import scala.collection.JavaConverters;
import org.apache.spark.sql.catalyst.expressions.Expression;
import org.apache.spark.sql.catalyst.expressions.NamedExpression;
import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression;
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan;
import org.apache.spark.sql.catalyst.plans.logical.Aggregate;
import scala.runtime.AbstractPartialFunction;
public class Util {
public static Set<String> getClientPlanFields(LogicalPlan rootPlan) {
return getSelectedExpressionsFromLogicalPlan(rootPlan).stream()
.filter(NamedExpression.class::isInstance)
.map(exp -> ((NamedExpression) exp).name())
.collect(Collectors.toSet());
}
public static List<Expression> getSelectedExpressionsFromLogicalPlan(LogicalPlan rootPlan) {
Option<Aggregate> maybeAggregate = rootPlan.collectFirst(partial(
Aggregate.class::isInstance, Aggregate.class::cast));
List<NamedExpression> aggregateExpressions = JavaConverters.seqAsJavaList(
maybeAggregate.get().aggregateExpressions());
return aggregateExpressions.stream().map(Expression.class::cast).collect(Collectors.toList());
}
public static <T, R> PartialFunction<T, R> partial(Function<T, Boolean> filter, Function<T, R> applier) {
return new AbstractPartialFunction<T, R>() {
@Override
public boolean isDefinedAt(T item) {
return filter.apply(item);
}
@Override
public R apply(T item) {
return applier.apply(item);
}
};
}
}
Steps to reproduce:
Please provide detailed steps for how we can reproduce the bug.
mvn clean pmd:pmd
Running PMD through: [Maven]
Target JDK 1.8