Skip to content

Method: convertOperation(GenOperation)

1: package de.fhdw.wtf.generator.transformer.clipper;
2:
3: import java.util.Iterator;
4:
5: import de.fhdw.wtf.common.exception.walker.TaskException;
6: import de.fhdw.wtf.common.task.TaskExecutor;
7: import de.fhdw.wtf.generator.java.generatorModel.GenClassClass;
8: import de.fhdw.wtf.generator.java.generatorModel.GenFullParsedOperationState;
9: import de.fhdw.wtf.generator.java.generatorModel.GenInterfaceClass;
10: import de.fhdw.wtf.generator.java.generatorModel.GenJavaOperation;
11: import de.fhdw.wtf.generator.java.generatorModel.GenOperation;
12: import de.fhdw.wtf.generator.java.generatorModel.GenOperationState;
13: import de.fhdw.wtf.generator.java.generatorModel.GenSimpleOperationState;
14: import de.fhdw.wtf.generator.java.generatorModel.GeneratorModel;
15: import de.fhdw.wtf.generator.java.visitor.GenOperationStateVisitorReturnException;
16: import de.fhdw.wtf.generator.java.walker.SimpleGeneratorModelWalkerTask;
17: import de.fhdw.wtf.generator.transformer.exception.NoMatchingMethodException;
18:
19: /**
20: * Java syntax tree operations have to states (SimpleOperationState, FullParsedOperationState). This class offers
21: * operations for converting unparsed (simple) operations to full parsed ones.
22: *
23: * Attention: This class is currently unused, because our java syntax tree does not support all java features (generics
24: * in operation signatures, attributes with default implementation, several attribute and parameter modifier).
25: *
26: */
27: public class ConvertSimpleOperationsToFullParsedTask extends SimpleGeneratorModelWalkerTask {
28:         
29:         /**
30:          * @param taskmanager
31:          * @param javaGeneratorModel
32:          */
33:         public ConvertSimpleOperationsToFullParsedTask(final TaskExecutor taskmanager,
34:                         final GeneratorModel javaGeneratorModel) {
35:                 super(taskmanager, javaGeneratorModel);
36:         }
37:         
38:         @Override
39:         public void handleOperation(final GenOperation o) throws TaskException {
40:                 // nothing to do
41:         }
42:         
43:         @Override
44:         public void finalizeTask() throws TaskException {
45:                 // nothing to do
46:         }
47:         
48:         @Override
49:         public void handleClassClass(final GenClassClass cc) throws TaskException {
50:                 this.convert(cc);
51:         }
52:         
53:         @Override
54:         public void handleInterfaceClass(final GenInterfaceClass ic) throws TaskException {
55:                 this.convert(ic);
56:         }
57:         
58:         // @Override
59:         // public void handleInterfaceWithImplClassClass(final GenInterfaceWithImplClassClass iwic) throws TaskException {
60:         // this.convert(iwic);
61:         // }
62:         
63:         private void convert(final de.fhdw.wtf.generator.java.generatorModel.GenClass t) throws TaskException {
64:                 final Iterator<GenJavaOperation> i = t.getOperations().iterator();
65:                 while (i.hasNext()) {
66:                         final GenJavaOperation current = i.next();
67:                         try {
68:                                 final GenFullParsedOperationState newState = this.convertOperation(current);
69:                                 current.setState(newState);
70:                         } catch (final NoMatchingMethodException e) {
71:                                 // no need to replace
72:                         }
73:                 }
74:         }
75:         
76:         /**
77:          * Converts the state of the given operation to a FullParsedState - it analyzes the source code in a
78:          * SimpleOperationState and returns the merged state.
79:          *
80:          * @param op
81:          * @return merged state of the operation
82:          * @throws TaskException
83:          */
84:         private GenFullParsedOperationState convertOperation(final GenOperation op) throws TaskException {
85:                 return op.getState().accept(
86:                                 new GenOperationStateVisitorReturnException<GenFullParsedOperationState, TaskException>() {
87:                                         @Override
88:                                         public GenFullParsedOperationState handle(final GenFullParsedOperationState s)
89:                                                         throws NoMatchingMethodException {
90:                                                 // nothing to do
91:                                                 throw new NoMatchingMethodException(op.getName());
92:                                         }
93:                                         
94:                                         @Override
95:                                         public GenFullParsedOperationState handle(final GenSimpleOperationState o) throws TaskException {
96:                                                 final GenOperation modelOperation = o.getOverwrittenOperation();
97:                                                 final GenFullParsedOperationState clippedOperation =
98:                                                                 ClipperUtils.parseOperation(
99:                                                                                 o.getFullOperationWithPossibleImplementation(),
100:                                                                                 op.getName());
101:                                                 return this.mergeOp(this.expectFullParsed(modelOperation.getState()), clippedOperation);
102:                                         }
103:                                         
104:                                         /**
105:                                          * Merges the model and clipped operation. Has side effects on modelOperation!
106:                                          *
107:                                          * @param modelOperation
108:                                          * @param clippedOperation
109:                                          * @return
110:                                          */
111:                                         @SuppressWarnings("javadoc")
112:                                         private GenFullParsedOperationState mergeOp(final GenFullParsedOperationState modelOperation,
113:                                                         final GenFullParsedOperationState clippedOperation) {
114:                                                 modelOperation.setVisibility(modelOperation.getVisibility().min(
115:                                                                 clippedOperation.getVisibility()));
116:                                                 modelOperation.setMethodBody(clippedOperation.getMethodBody());
117:                                                 modelOperation.setComment(clippedOperation.getComment());
118:                                                 modelOperation.getModifiers().addAll(clippedOperation.getModifiers());
119:                                                 return clippedOperation;
120:                                         }
121:                                         
122:                                         private GenFullParsedOperationState expectFullParsed(final GenOperationState state)
123:                                                         throws TaskException {
124:                                                 return state
125:                                                                 .accept(new GenOperationStateVisitorReturnException<GenFullParsedOperationState, TaskException>() {
126:                                                                         
127:                                                                         @Override
128:                                                                         public GenFullParsedOperationState handle(final GenSimpleOperationState s)
129:                                                                                         throws TaskException {
130:                                                                                 throw new TaskException("Overridden Model operation must be full parsed.");
131:                                                                         }
132:                                                                         
133:                                                                         @Override
134:                                                                         public GenFullParsedOperationState handle(final GenFullParsedOperationState s)
135:                                                                                         throws TaskException {
136:                                                                                 return s;
137:                                                                         }
138:                                                                 });
139:                                         }
140:                                 });
141:         }
142: }