Skip to content

Method: walk()

1: package de.fhdw.wtf.walker.walker;
2:
3: import de.fhdw.wtf.common.ast.Attribute;
4: import de.fhdw.wtf.common.ast.Constructor;
5: import de.fhdw.wtf.common.ast.Group;
6: import de.fhdw.wtf.common.ast.GroupElement;
7: import de.fhdw.wtf.common.ast.Model;
8: import de.fhdw.wtf.common.ast.Operation;
9: import de.fhdw.wtf.common.ast.type.ClassType;
10: import de.fhdw.wtf.common.ast.visitor.GroupElementExceptionVisitor;
11: import de.fhdw.wtf.common.exception.walker.TaskException;
12: import de.fhdw.wtf.common.task.DependencyTask;
13: import de.fhdw.wtf.common.task.TaskExecutor;
14: import de.fhdw.wtf.common.task.result.OKTaskResult;
15:
16: /**
17: * A task does its specific work, while traversing through the abstract syntax tree.
18: *
19: */
20: public abstract class SimpleWalkerTask extends WalkerTask {
21:         
22:         /**
23:          * Abstract syntax tree model, that is traversed in this task.
24:          */
25:         private final Model m;
26:         
27:         /**
28:          * Constructor for {@link SimpleWalkerTask}.
29:          *
30:          * @param m
31:          * Model
32:          * @param taskmanager
33:          * Taskmanager
34:          */
35:         public SimpleWalkerTask(final Model m, final TaskExecutor taskmanager) {
36:                 super(taskmanager);
37:                 this.m = m;
38:         }
39:         
40:         @Override
41:         public de.fhdw.wtf.common.task.result.TaskResult doWork() {
42:                 try {
43:                         this.beginTask();
44:                         this.walk();
45:                         this.finalizeTask();
46:                 } catch (final TaskException e) {
47:                         return new de.fhdw.wtf.common.task.result.ExceptionalTaskResult(e);
48:                 }
49:                 return new OKTaskResult();
50:         }
51:         
52:         /**
53:          * "Walks" down the tree, calling the current service at all elements.
54:          *
55:          * @throws TaskException
56:          * when the implementing task throws unfixable errors
57:          */
58:         private void walk() throws TaskException {
59:•                for (final Group g : this.m.getGroups()) {
60:                         this.handleGroup(g);
61:                         this.processGroupElements(g);
62:                 }
63:         }
64:         
65:         /**
66:          * Walks over and processes GroupElements. Calls the abstract handle-operations of {@link WalkerTask} when needed.
67:          *
68:          * @param g
69:          * : common.ast.Group.
70:          * @throws TaskException
71:          * : When the implementing task throws unfixable errors
72:          */
73:         void processGroupElements(final Group g) throws TaskException {
74:                 for (final GroupElement element : g.getGroupElements()) {
75:                         element.accept(new GroupElementExceptionVisitor<TaskException>() {
76:                                 @Override
77:                                 public void handle(final Group g2) throws TaskException {
78:                                         SimpleWalkerTask.this.handleGroup(g2);
79:                                         SimpleWalkerTask.this.processGroupElements(g2);
80:                                 }
81:                                 
82:                                 @Override
83:                                 public void handle(final ClassType clss) throws TaskException {
84:                                         SimpleWalkerTask.this.handleClass(clss);
85:                                         for (final Attribute a : clss.getAttributes()) {
86:                                                 SimpleWalkerTask.this.handleAttribute(a, clss);
87:                                         }
88:                                         for (final Operation o : clss.getOperations()) {
89:                                                 SimpleWalkerTask.this.handleConstructorOrOperation(o, clss);
90:                                         }
91:                                         for (final Constructor constructor : clss.getConstructors()) {
92:                                                 SimpleWalkerTask.this.handleConstructorOrOperation(constructor, clss);
93:                                         }
94:                                 }
95:                         });
96:                 }
97:         }
98:         
99:         @Override
100:         public boolean containsTransitive(final DependencyTask a) {
101:                 return false;
102:         }
103:         
104:         /**
105:          * Returns the AST-Model of this SimpleWalkerTask.
106:          *
107:          * @return Model
108:          */
109:         public Model getModel() {
110:                 return this.m;
111:         }
112:         
113: }