-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6412 Implement Visitor for grouping beans per types #5987
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
Draft
NoemieBenard
wants to merge
5
commits into
epic-SONARJAVA-6237
Choose a base branch
from
nb/sonarjava-6412-type-to-bean-names-index
base: epic-SONARJAVA-6237
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e56ddad
Implement visitor for grouping beans of the same type
NoemieBenard 28df48c
Write tests for new visitor
NoemieBenard faaea4e
Address review comments
NoemieBenard c3b31d8
Improve test coverage
NoemieBenard 21b680b
Group similar tests into a single parameterized one
NoemieBenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ontend/src/main/java/org/sonar/java/model/springcontext/TypeToBeanNamesIndexGatherer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.model.springcontext; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.sonar.java.utils.SpringUtils; | ||
| import org.sonar.plugins.java.api.ModuleScannerContext; | ||
| import org.sonar.plugins.java.api.semantic.Symbol; | ||
| import org.sonar.plugins.java.api.semantic.Type; | ||
| import org.sonar.plugins.java.api.tree.ClassTree; | ||
| import org.sonar.plugins.java.api.tree.MethodTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| /** | ||
| * Populates {@link TypeToBeanNamesIndex} by mapping every type in a bean's hierarchy | ||
| * (concrete class, superclasses, interfaces) to the bean's name. | ||
| */ | ||
| public class TypeToBeanNamesIndexGatherer extends SpringContextModelGatherer { | ||
|
|
||
| private record BeanTypeEntry(String beanName, Set<String> typeHierarchy) {} | ||
|
|
||
| private final List<BeanTypeEntry> collectedEntries = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.CLASS); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| ClassTree classTree = (ClassTree) tree; | ||
| if (classTree.simpleName() == null) { | ||
| return; | ||
| } | ||
|
|
||
| var meta = classTree.symbol().metadata(); | ||
| if (SpringUtils.STEREOTYPE_ANNOTATIONS.stream().anyMatch(meta::isAnnotatedWith)) { | ||
| String beanName = SpringUtils.resolveStereotypeBeanName(meta, classTree.simpleName().name()); | ||
| collectedEntries.add(new BeanTypeEntry(beanName, collectTypeHierarchy(classTree.symbol()))); | ||
|
|
||
| for (MethodTree method : SpringUtils.getBeanMethods(classTree)) { | ||
| Set<String> typeHierarchy = collectTypeHierarchy(method.returnType().symbolType().symbol()); | ||
| for (String methodBeanName : SpringUtils.resolveBeanMethodNames(method)) { | ||
| collectedEntries.add(new BeanTypeEntry(methodBeanName, typeHierarchy)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void gatherSpringContextData(ModuleScannerContext context, SpringContextModel springContextModel) { | ||
| TypeToBeanNamesIndex index = springContextModel.getTypeToBeanNamesIndex(); | ||
| for (BeanTypeEntry entry : collectedEntries) { | ||
| for (String typeFqn : entry.typeHierarchy()) { | ||
| index.addBeanForType(typeFqn, entry.beanName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Set<String> collectTypeHierarchy(Symbol.TypeSymbol symbol) { | ||
| Set<String> visited = new LinkedHashSet<>(); | ||
| walkTypeHierarchy(symbol, visited); | ||
| return visited; | ||
| } | ||
|
|
||
| private static void walkTypeHierarchy(Symbol.TypeSymbol symbol, Set<String> visited) { | ||
| String fqn = symbol.type().fullyQualifiedName(); | ||
| if ("java.lang.Object".equals(fqn) || symbol.type().isUnknown() || !visited.add(fqn)) { | ||
| return; | ||
| } | ||
| Type superClass = symbol.superClass(); | ||
| if (superClass != null && !superClass.isUnknown()) { | ||
| walkTypeHierarchy(superClass.symbol(), visited); | ||
| } | ||
| for (Type iface : symbol.interfaces()) { | ||
| if (!iface.isUnknown()) { | ||
| walkTypeHierarchy(iface.symbol(), visited); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
java-frontend/src/test/files/springcontext/ComponentImplementingInterface.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package checks.spring.context; | ||
|
|
||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.context.ApplicationContextAware; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| class ComponentImplementingInterface implements ApplicationContextAware { | ||
|
|
||
| @Override | ||
| public void setApplicationContext(ApplicationContext ctx) { | ||
| // not needed for test | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.