Skip to content

Commit

Permalink
recursive search, thanks to chatgpt
Browse files Browse the repository at this point in the history
  • Loading branch information
neph1 committed Nov 29, 2024
1 parent daaa8d4 commit ecde876
Showing 1 changed file with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@

import java.io.IOException;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import javax.swing.JPanel;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.project.JavaProjectConstants;
Expand All @@ -49,7 +50,6 @@
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.JavaSource.Phase;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.SourceGroup;
Expand Down Expand Up @@ -89,47 +89,78 @@ private List<String> getSources() {
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
if (groups != null) {
for (SourceGroup sourceGroup : groups) {
ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.BOOT),
ClasspathInfo cpInfo = ClasspathInfo.create(
ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.BOOT),
ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.COMPILE),
ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.SOURCE));
ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.SOURCE)
);

Set<SearchScope> set = EnumSet.of(ClassIndex.SearchScope.SOURCE);
Set<ElementHandle<TypeElement>> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set);
for (Iterator<ElementHandle<TypeElement>> it = types.iterator(); it.hasNext();) {
final ElementHandle<TypeElement> elementHandle = it.next();
for (ElementHandle<TypeElement> elementHandle : types) {
JavaSource js = JavaSource.create(cpInfo);
try {
js.runUserActionTask((CompilationController control) -> {
control.toPhase(Phase.RESOLVED);

control.toPhase(JavaSource.Phase.RESOLVED);
TypeElement elem = elementHandle.resolve(control);
if (elem != null) {
List<? extends TypeMirror> interfaces = elem.getInterfaces();
for (TypeMirror typeMirror : interfaces) {
String interfaceName = typeMirror.toString();
if ("com.jme3.app.state.AppState".equals(interfaceName)) {
list.add(elem.getQualifiedName().toString());
}
}
TypeMirror superClass = elem.getSuperclass();
String superClassName = superClass.toString();
if ("com.jme3.app.state.AbstractAppState".equals(superClassName)
|| "com.jme3.app.state.BaseAppState".equals(superClassName)) {
list.add(elem.getQualifiedName().toString());
}
if (elem != null && doesInheritFromAppState(elem, control.getTypes())) {
list.add(elem.getQualifiedName().toString());
}
}, false);
} catch (IOException ioe) {
Exceptions.printStackTrace(ioe);
}
}

}
}
}
return list;
}

/**
* Checks recursively if a type inherits from or implements any
* AppState-related class/interface.
*/
private boolean doesInheritFromAppState(TypeElement type, Types typeUtils) {
if (type == null) {
return false;
}

// Check interfaces
for (TypeMirror iface : type.getInterfaces()) {
if (isAppStateType(iface)) {
return true;
}
if (doesInheritFromAppState((TypeElement) typeUtils.asElement(iface), typeUtils)) {
return true;
}
}

// Check superclass
TypeMirror superClass = type.getSuperclass();
if (superClass != null && superClass.getKind() != TypeKind.NONE) {
if (isAppStateType(superClass)) {
return true;
}
return doesInheritFromAppState((TypeElement) typeUtils.asElement(superClass), typeUtils);
}

return false;
}

/**
* Determines if a TypeMirror corresponds to an AppState-related type.
*/
private boolean isAppStateType(TypeMirror typeMirror) {
if (typeMirror == null) {
return false;
}
String className = typeMirror.toString();
return "com.jme3.app.state.AppState".equals(className)
|| "com.jme3.app.state.AbstractAppState".equals(className)
|| "com.jme3.app.state.BaseAppState".equals(className);
}

public void load(Project proj) {
this.proj = proj;
scanControls();
Expand Down

0 comments on commit ecde876

Please sign in to comment.