|
| 1 | +package fr.inria.inspectorguidget.api.analyser; |
| 2 | + |
| 3 | +import fr.inria.inspectorguidget.api.UIDataExtractor; |
| 4 | +import fr.inria.inspectorguidget.api.processor.WidgetProcessor; |
| 5 | +import fr.inria.inspectorguidget.data.ClassRef; |
| 6 | +import fr.inria.inspectorguidget.data.Handler; |
| 7 | +import fr.inria.inspectorguidget.data.HandlerInteraction; |
| 8 | +import fr.inria.inspectorguidget.data.Interaction; |
| 9 | +import fr.inria.inspectorguidget.data.Location; |
| 10 | +import fr.inria.inspectorguidget.data.UICommand; |
| 11 | +import fr.inria.inspectorguidget.data.UIData; |
| 12 | +import fr.inria.inspectorguidget.data.Widget; |
| 13 | +import fr.inria.inspectorguidget.data.WidgetBinding; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import spoon.reflect.declaration.CtClass; |
| 19 | +import spoon.reflect.declaration.CtExecutable; |
| 20 | + |
| 21 | +public class UIDataAnalyser implements UIDataExtractor { |
| 22 | + private final CommandAnalyser cmdAnalyser; |
| 23 | + private final WidgetProcessor widgetProc; |
| 24 | + CommandWidgetFinder finder; |
| 25 | + |
| 26 | + public UIDataAnalyser() { |
| 27 | + super(); |
| 28 | + |
| 29 | + cmdAnalyser = new CommandAnalyser(); |
| 30 | + widgetProc = new WidgetProcessor(true); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public UIData extractUIData() { |
| 35 | + runAnalysis(); |
| 36 | + return produceUIData(); |
| 37 | + } |
| 38 | + |
| 39 | + private void runAnalysis() { |
| 40 | + cmdAnalyser.run(); |
| 41 | + |
| 42 | + final InspectorGuidetAnalyser launcher = new InspectorGuidetAnalyser( |
| 43 | + Collections.singletonList(widgetProc), cmdAnalyser.getModelBuilder()); |
| 44 | + launcher.process(); |
| 45 | + |
| 46 | + finder = new CommandWidgetFinder( |
| 47 | + cmdAnalyser.getCommands().values().parallelStream().flatMap(s -> s.getCommands().stream()).collect(Collectors.toList()), |
| 48 | + widgetProc.getWidgetUsages()); |
| 49 | + finder.process(); |
| 50 | + } |
| 51 | + |
| 52 | + private UIData produceUIData() { |
| 53 | + final Map<Command, CommandWidgetFinder.WidgetFinderEntry> results = finder.getResults(); |
| 54 | + |
| 55 | + return new UIData(results.entrySet().stream() |
| 56 | + .map(entry -> { |
| 57 | + // The method/lambda that contains the command |
| 58 | + final CtExecutable<?> exec = entry.getKey().getExecutable(); |
| 59 | + |
| 60 | + // Reference to the class that contains the command |
| 61 | + final ClassRef cref = new ClassRef(exec.getBody().getPosition().getCompilationUnit().getFile().toString(), |
| 62 | + exec.getParent(CtClass.class).getSimpleName(), exec.getParent(CtClass.class).getQualifiedName()); |
| 63 | + |
| 64 | + // Data on the handler |
| 65 | + final Handler handler = new Handler(exec.getReference().getDeclaringType().getQualifiedName(), |
| 66 | + new Location(exec.getBody().getPosition().getLine(), exec.getBody().getPosition().getEndLine(), cref)); |
| 67 | + |
| 68 | + // Data on the interaction |
| 69 | + final Interaction interaction = new HandlerInteraction(Collections.singletonList(handler)); |
| 70 | + |
| 71 | + // Data command |
| 72 | + final UICommand cmd = new UICommand(new Location(entry.getKey().getLineStart(), entry.getKey().getLineEnd(), cref), List.of(cref)); |
| 73 | + |
| 74 | + // Data widgets |
| 75 | + final List<Widget> widgets = entry.getValue().getRegisteredWidgets() |
| 76 | + .stream() |
| 77 | + .map(w -> new Widget(w.widgetVar.getSimpleName(), w.widgetVar.getType().getSimpleName(), |
| 78 | + w.getUsagesWithCons() |
| 79 | + .stream() |
| 80 | + .map(u -> new Location(u.getPosition().getLine(), u.getPosition().getEndLine(), |
| 81 | + new ClassRef(u.getPosition().getCompilationUnit().getFile().toString(), |
| 82 | + u.getParent(CtClass.class).getSimpleName(), |
| 83 | + u.getParent(CtClass.class).getQualifiedName()))) |
| 84 | + .collect(Collectors.toList()) |
| 85 | + )) |
| 86 | + .collect(Collectors.toList()); |
| 87 | + |
| 88 | + // Data on the widget binding |
| 89 | + return new WidgetBinding(interaction, widgets, cmd); |
| 90 | + }) |
| 91 | + .collect(Collectors.toList())); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void addInputResource(final String file) { |
| 96 | + cmdAnalyser.addInputResource(file); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void setSourceClasspath(final String... args) { |
| 101 | + cmdAnalyser.setSourceClasspath(args); |
| 102 | + } |
| 103 | +} |
0 commit comments