Fetch the type of TypeNode reflectively

This commit is contained in:
Clément Fournier 2019-01-29 19:01:24 +01:00
parent f9aa6679c5
commit 16267426d4
2 changed files with 27 additions and 4 deletions

View File

@ -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;
}

View File

@ -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();
}
}
}