Move methods from ReconcileUtils into ASTUtils

Signed-off-by: aboyko <alex.boyko@broadcom.com>
This commit is contained in:
aboyko
2025-03-31 12:33:25 -04:00
committed by Martin Lippert
parent 094304bdda
commit d8015fc4ec
5 changed files with 80 additions and 88 deletions

View File

@@ -45,7 +45,6 @@ import org.springframework.ide.vscode.boot.java.events.EventListenerIndexer;
import org.springframework.ide.vscode.boot.java.events.EventPublisherIndexElement;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.boot.java.reconcilers.NotRegisteredBeansReconciler;
import org.springframework.ide.vscode.boot.java.reconcilers.ReconcileUtils;
import org.springframework.ide.vscode.boot.java.reconcilers.RequiredCompleteAstException;
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexer;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
@@ -364,7 +363,7 @@ public class ComponentSymbolProvider implements SymbolProvider {
ITypeBinding typeBinding = typeDeclaration.resolveBinding();
if (typeBinding == null) return;
if (ReconcileUtils.implementsAnyType(NotRegisteredBeansReconciler.AOT_BEANS, typeBinding)) {
if (ASTUtils.isAnyTypeInHierarchy(typeBinding, NotRegisteredBeansReconciler.AOT_BEANS)) {
String type = typeBinding.getQualifiedName();
String docUri = context.getDocURI();

View File

@@ -14,6 +14,7 @@ import static org.springframework.ide.vscode.commons.java.SpringProjectUtil.spri
import java.net.URI;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jdt.core.dom.ASTVisitor;
@@ -23,6 +24,7 @@ import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.springframework.ide.vscode.boot.java.SpringAotJavaProblemType;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType;
@@ -108,7 +110,7 @@ public class BeanPostProcessingIgnoreInAotReconciler implements JdtAstReconciler
}
private static boolean isApplicable(ITypeBinding type) {
return ReconcileUtils.implementsType(RUNTIME_BEAN_POST_PROCESSOR, type) && ReconcileUtils.implementsType(COMPILE_BEAN_POST_PROCESSOR, type);
return ASTUtils.areAllTypesInHierarchy(type, Set.of(RUNTIME_BEAN_POST_PROCESSOR, COMPILE_BEAN_POST_PROCESSOR));
}
}

View File

@@ -30,6 +30,7 @@ import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.SpringAotJavaProblemType;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
@@ -83,7 +84,7 @@ public class NotRegisteredBeansReconciler implements JdtAstReconciler {
if (!node.isInterface() && !Modifier.isAbstract(node.getModifiers())) {
ITypeBinding type = node.resolveBinding();
if (type != null && ReconcileUtils.implementsAnyType(AOT_BEANS, type)) {
if (type != null && ASTUtils.isAnyTypeInHierarchy(type, AOT_BEANS)) {
// // reconcile AOT Proceesor itself

View File

@@ -148,34 +148,6 @@ public class ReconcileUtils {
return typeUsed.get();
}
public static boolean implementsType(String fqName, ITypeBinding type) {
if (fqName.equals(type.getQualifiedName())) {
return true;
} else {
for (ITypeBinding t : type.getInterfaces()) {
if (implementsType(fqName, t)) {
return true;
}
}
}
return false;
}
public static boolean implementsAnyType(Collection<String> fqNames, ITypeBinding type) {
if (fqNames.contains(type.getQualifiedName())) {
return true;
} else {
for (ITypeBinding t : type.getInterfaces()) {
if (implementsAnyType(fqNames, t)) {
return true;
}
}
}
return false;
}
public static String getSimpleName(String fqName) {
int idx = fqName.lastIndexOf('.');
if (idx >= 0 && idx < fqName.length() - 1) {

View File

@@ -10,13 +10,18 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.utils;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
@@ -394,36 +399,14 @@ public class ASTUtils {
}
public static ITypeBinding findInTypeHierarchy(ITypeBinding resolvedType, Set<String> typesToCheck) {
ITypeBinding[] interfaces = resolvedType.getInterfaces();
for (ITypeBinding resolvedInterface : interfaces) {
String simplifiedType = null;
if (resolvedInterface.isParameterizedType()) {
simplifiedType = resolvedInterface.getBinaryName();
}
else {
simplifiedType = resolvedInterface.getQualifiedName();
}
if (typesToCheck.contains(simplifiedType)) {
return resolvedInterface;
}
else {
ITypeBinding result = findInTypeHierarchy(resolvedInterface, typesToCheck);
if (result != null) {
return result;
}
for (Iterator<ITypeBinding> itr = getSuperTypesIterator(resolvedType); itr.hasNext();) {
ITypeBinding b = itr.next();
String fqn = b.isParameterizedType() ? b.getBinaryName() : b.getQualifiedName();
if (typesToCheck.contains(fqn)) {
return b;
}
}
ITypeBinding superclass = resolvedType.getSuperclass();
if (superclass != null) {
return findInTypeHierarchy(superclass, typesToCheck);
}
else {
return null;
}
return null;
}
public static Optional<DocumentEdits> getImportsEdit(CompilationUnit cu, Collection<String> imprts, IDocument doc) {
@@ -463,40 +446,75 @@ public class ASTUtils {
// }
//
public static void findSupertypes(ITypeBinding binding, Set<String> supertypesCollector) {
// interfaces
ITypeBinding[] interfaces = binding.getInterfaces();
for (ITypeBinding resolvedInterface : interfaces) {
String simplifiedType = null;
if (resolvedInterface.isParameterizedType()) {
simplifiedType = resolvedInterface.getBinaryName();
}
else {
simplifiedType = resolvedInterface.getQualifiedName();
}
if (simplifiedType != null) {
supertypesCollector.add(simplifiedType);
findSupertypes(resolvedInterface, supertypesCollector);
for (Iterator<String> itr = getSuperTypesFqNamesIterator(binding); itr.hasNext();) {
supertypesCollector.add(itr.next());
}
}
public static boolean isAnyTypeInHierarchy(ITypeBinding binding, Collection<String> typeFqns) {
for (Iterator<String> itr = getSuperTypesFqNamesIterator(binding); itr.hasNext();) {
String fqn = itr.next();
if (typeFqns.contains(fqn)) {
return true;
}
}
// superclasses
ITypeBinding superclass = binding.getSuperclass();
if (superclass != null) {
String simplifiedType = null;
if (superclass.isParameterizedType()) {
simplifiedType = superclass.getBinaryName();
return false;
}
public static boolean areAllTypesInHierarchy(ITypeBinding binding, Collection<String> typeFqns) {
HashSet<String> notFound = new HashSet<>(typeFqns);
for (Iterator<String> itr = getSuperTypesFqNamesIterator(binding); itr.hasNext() && !notFound.isEmpty();) {
notFound.remove(itr.next());
}
return notFound.isEmpty();
}
public static Iterator<ITypeBinding> getSuperTypesIterator(ITypeBinding binding) {
final Queue<ITypeBinding> q = new ArrayDeque<>(10);
q.add(binding);
return new Iterator<ITypeBinding>() {
@Override
public boolean hasNext() {
return !q.isEmpty();
}
else {
simplifiedType = superclass.getQualifiedName();
@Override
public ITypeBinding next() {
ITypeBinding t = q.poll();
if (t == null) {
throw new NoSuchElementException();
}
for (ITypeBinding b : t.getInterfaces()) {
if (b != null) {
q.add(b);
}
}
if (t.getSuperclass() != null) {
q.add(t.getSuperclass());
}
return t;
}
if (simplifiedType != null) {
supertypesCollector.add(simplifiedType);
findSupertypes(superclass, supertypesCollector);
};
}
public static Iterator<String> getSuperTypesFqNamesIterator(ITypeBinding binding) {
Iterator<ITypeBinding> itr = getSuperTypesIterator(binding);
return new Iterator<String>() {
@Override
public boolean hasNext() {
return itr.hasNext();
}
}
@Override
public String next() {
ITypeBinding b = itr.next();
return b.isParameterizedType() ? b.getBinaryName() : b.getQualifiedName();
}
};
}
public static InjectionPoint[] findInjectionPoints(MethodDeclaration method, TextDocument doc) throws BadLocationException {