Fetch the type of TypeNode reflectively
This commit is contained in:
parent
f9aa6679c5
commit
16267426d4
@ -26,6 +26,7 @@ import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
||||
import net.sourceforge.pmd.util.fxdesigner.app.AbstractController;
|
||||
import net.sourceforge.pmd.util.fxdesigner.app.NodeSelectionSource;
|
||||
import net.sourceforge.pmd.util.fxdesigner.model.MetricResult;
|
||||
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
|
||||
import net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsPersistenceUtil.PersistentProperty;
|
||||
import net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeCell;
|
||||
import net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeItem;
|
||||
@ -242,10 +243,8 @@ public class NodeInfoPanelController extends AbstractController<MainDesignerCont
|
||||
}
|
||||
}
|
||||
|
||||
// TODO maybe put some equivalent to TypeNode inside pmd-core
|
||||
// if (node instanceof TypeNode) {
|
||||
// result.add("typeIs() = " + ((TypeNode) node).getType());
|
||||
// }
|
||||
DesignerUtil.getResolvedType(node).map(t -> "typeIs() = " + t).ifPresent(result::add);
|
||||
|
||||
Collections.sort(result);
|
||||
return result;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
package net.sourceforge.pmd.util.fxdesigner.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -43,6 +44,7 @@ import net.sourceforge.pmd.lang.Language;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.Parser;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery;
|
||||
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
||||
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
|
||||
@ -450,4 +452,26 @@ public final class DesignerUtil {
|
||||
})
|
||||
.orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to retrieve the type of a java TypeNode reflectively.
|
||||
*/
|
||||
public static Optional<Class<?>> getResolvedType(Node node) {
|
||||
// TODO maybe put some equivalent to TypeNode inside pmd-core
|
||||
|
||||
try {
|
||||
return Optional.of(node.getClass().getMethod("getType"))
|
||||
.filter(m -> m.getReturnType() == Class.class)
|
||||
.map(m -> {
|
||||
try {
|
||||
return m.invoke(node);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
return null;
|
||||
}
|
||||
}).map(type -> (Class<?>) type);
|
||||
} catch (NoSuchMethodException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user