From 4cfe90cfdcacfd7e1c2789ec0253a2d2f1c85dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 15 May 2024 09:36:39 +0200 Subject: [PATCH] Cleanup symtable factory --- .../pmd/lang/ast/internal/StreamImpl.java | 5 ++++ .../pmd/lang/java/ast/ASTVariableId.java | 2 +- .../table/coreimpl/ShadowChainBuilder.java | 9 ++++--- .../table/internal/SymTableFactory.java | 27 ++++++++----------- .../table/internal/SymbolTableResolver.java | 6 ++--- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java index dbeb481a37..66035b5b0d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java @@ -101,6 +101,11 @@ public final class StreamImpl { } } + @Override + public List toList() { + return Collections.unmodifiableList(Arrays.asList(array)); + } + @Override protected Iterator baseIterator() { return Arrays.asList(array).iterator(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableId.java index 29ceca7c17..4e100e9a74 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableId.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableId.java @@ -135,7 +135,7 @@ public final class ASTVariableId extends AbstractTypedSymbolDeclarator= 0; + return "_".equals(name) && getLanguageVersion().compareToVersion("21-preview") >= 0; } /** Returns the name of the variable. */ diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/ShadowChainBuilder.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/ShadowChainBuilder.java index ad4e25cb30..c24d63e500 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/ShadowChainBuilder.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/coreimpl/ShadowChainBuilder.java @@ -17,9 +17,11 @@ import java.util.Map.Entry; import java.util.function.BinaryOperator; import java.util.function.Function; +import org.checkerframework.checker.nullness.qual.Nullable; + import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.MostlySingularMultimap.Builder; import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.MostlySingularMultimap.MapMaker; -import net.sourceforge.pmd.util.CollectionUtil; +import net.sourceforge.pmd.util.IteratorUtil; /** * Build a shadow chain for some type. @@ -126,8 +128,9 @@ public abstract class ShadowChainBuilder { // convenience to build name resolvers - public ResolverBuilder groupByName(Iterable input, Function symbolFetcher) { - return new ResolverBuilder(newMapBuilder().groupBy(CollectionUtil.map(input, symbolFetcher), this::getSimpleName)); + public ResolverBuilder groupByName(Iterable input, Function symbolFetcher) { + Iterable mapped = () -> IteratorUtil.mapNotNull(input.iterator(), symbolFetcher); + return new ResolverBuilder(newMapBuilder().groupBy(mapped, this::getSimpleName)); } public ResolverBuilder groupByName(Iterable tparams) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymTableFactory.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymTableFactory.java index 680679ba46..eb15e316dc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymTableFactory.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymTableFactory.java @@ -23,7 +23,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.lang.ast.NodeStream; import net.sourceforge.pmd.lang.ast.SemanticErrorReporter; -import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter; import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters; import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTList; @@ -366,13 +365,13 @@ final class SymTableFactory { * of fields by local variables and formals. */ JSymbolTable bodyDeclaration(JSymbolTable parent, JClassType enclosing, @Nullable ASTFormalParameters formals, @Nullable ASTTypeParameters tparams) { - NodeStream namedFormals = ASTList.orEmptyStream(formals).map(ASTFormalParameter::getVarId); - if (unnamedVariableIsSupported()) { - namedFormals = namedFormals.filterNot(ASTVariableId::isUnnamed); - } return new SymbolTableImpl( - VARS.shadow(varNode(parent), ScopeInfo.FORMAL_PARAM, VARS.groupByName(namedFormals, fp -> { - JVariableSymbol sym = fp.getSymbol(); + VARS.shadow(varNode(parent), ScopeInfo.FORMAL_PARAM, VARS.groupByName(ASTList.orEmptyStream(formals), fp -> { + ASTVariableId varId = fp.getVarId(); + if (varId.isUnnamed()) { + return null; + } + JVariableSymbol sym = varId.getSymbol(); return sym.getTypeSystem().sigOf(enclosing, (JFormalParamSymbol) sym); })), TYPES.shadow(typeNode(parent), ScopeInfo.TYPE_PARAM, TYPES.groupByName(ASTList.orEmptyStream(tparams), ASTTypeParameter::getTypeMirror)), @@ -391,7 +390,7 @@ final class SymTableFactory { JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, Iterable ids) { List sigs = new ArrayList<>(); for (ASTVariableId id : ids) { - if (unnamedVariableIsSupported() && id.isUnnamed()) { + if (id.isUnnamed()) { continue; } sigs.add(id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id.getSymbol())); @@ -399,16 +398,12 @@ final class SymTableFactory { return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, VARS.groupByName(sigs))); } - JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, JVariableSymbol id) { - if (unnamedVariableIsSupported() && "_".equals(id.getSimpleName())) { - // unnamed variable + JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, ASTVariableId id) { + assert !id.isField(); + if (id.isUnnamed()) { // checks language version already return parent; } - return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id))); - } - - private boolean unnamedVariableIsSupported() { - return processor.getJdkVersion() >= 22; + return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id.getSymbol()))); } JSymbolTable localTypeSymTable(JSymbolTable parent, JClassType sym) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java index f7679a49e3..72f095e2fd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SymbolTableResolver.java @@ -428,7 +428,7 @@ public final class SymbolTableResolver { int pushed = 0; for (ASTVariableDeclarator declarator : st.children(ASTVariableDeclarator.class)) { ASTVariableId varId = declarator.getVarId(); - pushed += pushOnStack(f.localVarSymTable(top(), enclosing(), varId.getSymbol())); + pushed += pushOnStack(f.localVarSymTable(top(), enclosing(), varId)); // visit initializer setTopSymbolTableAndVisit(declarator.getInitializer(), ctx); } @@ -443,7 +443,7 @@ public final class SymbolTableResolver { ASTVariableId varId = node.getVarId(); setTopSymbolTableAndVisit(varId.getTypeNode(), ctx); - int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), varId.getSymbol())); + int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), varId)); ASTStatement body = node.getBody(); // unless it's a block the body statement may never set a // symbol table that would have this table as parent, @@ -479,7 +479,7 @@ public final class SymbolTableResolver { @Override public Void visit(ASTCatchClause node, @NonNull ReferenceCtx ctx) { - int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), node.getParameter().getVarId().getSymbol())); + int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), node.getParameter().getVarId())); setTopSymbolTableAndVisitAllChildren(node, ctx); popStack(pushed); return null;