From daaa8d43785b85a85c54a8d89d6ddfbc10c1d1bc Mon Sep 17 00:00:00 2001 From: rickard Date: Mon, 25 Nov 2024 20:11:06 +0100 Subject: [PATCH 1/2] fix AppStateExplorer not finding AppStates extending BaseAppState --- .../appstates/NewAppStateVisualPanel1.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java index 126a804b..545754c6 100644 --- a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java +++ b/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2010 jMonkeyEngine + * Copyright (c) 2009-2024 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,7 @@ */ package com.jme3.gde.core.appstates; +import java.io.IOException; import java.util.EnumSet; import java.util.Iterator; import java.util.LinkedList; @@ -49,8 +50,8 @@ 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.java.source.Task; import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectUtils; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.Sources; import org.openide.util.Exceptions; @@ -82,8 +83,8 @@ private void scanControls() { } private List getSources() { - Sources sources = proj.getLookup().lookup(Sources.class); - final List list = new LinkedList(); + Sources sources = ProjectUtils.getSources(proj); + final List list = new LinkedList<>(); if (sources != null) { SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); if (groups != null) { @@ -98,31 +99,27 @@ private List getSources() { final ElementHandle elementHandle = it.next(); JavaSource js = JavaSource.create(cpInfo); try { - js.runUserActionTask(new Task() { - public void run(CompilationController control) - throws Exception { - control.toPhase(Phase.RESOLVED); - //TODO: check with proper casting check.. gotta get TypeMirror of Control interface.. -// TypeUtilities util = control.getTypeUtilities();//.isCastable(Types., null) -// util.isCastable(null, null); - TypeElement elem = elementHandle.resolve(control); - if (elem != null) { - List 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)) { + js.runUserActionTask((CompilationController control) -> { + control.toPhase(Phase.RESOLVED); + + TypeElement elem = elementHandle.resolve(control); + if (elem != null) { + List 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()); + } } }, false); - } catch (Exception ioe) { + } catch (IOException ioe) { Exceptions.printStackTrace(ioe); } } From ecde876e997d2a9694205a3a79e8f9cb8599caab Mon Sep 17 00:00:00 2001 From: rickard Date: Fri, 29 Nov 2024 12:13:55 +0100 Subject: [PATCH 2/2] recursive search, thanks to chatgpt --- .../appstates/NewAppStateVisualPanel1.java | 77 +++++++++++++------ 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java index 545754c6..84b8352d 100644 --- a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java +++ b/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java @@ -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; @@ -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; @@ -89,47 +89,78 @@ private List 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 set = EnumSet.of(ClassIndex.SearchScope.SOURCE); Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); - for (Iterator> it = types.iterator(); it.hasNext();) { - final ElementHandle elementHandle = it.next(); + for (ElementHandle 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 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();