39
39
import org .slf4j .LoggerFactory ;
40
40
41
41
import java .util .LinkedHashMap ;
42
+ import java .util .LinkedList ;
42
43
import java .util .List ;
43
44
import java .util .Map ;
44
- import java .util .concurrent .atomic .AtomicBoolean ;
45
45
import java .util .function .Consumer ;
46
46
import java .util .function .Function ;
47
47
import java .util .function .Supplier ;
@@ -57,126 +57,138 @@ public class RecordTransformer { // checkstyle-disable-line ClassFanOutComplexit
57
57
58
58
private static final Logger LOG = LoggerFactory .getLogger (RecordTransformer .class );
59
59
60
- private final List <Expression > expressions ;
60
+ private final List <Consumer < Record >> consumers = new LinkedList <>() ;
61
61
private final Map <String , String > vars ;
62
62
private final Metafix metafix ;
63
63
64
- private Record record ;
64
+ private Supplier < String > currentMessageSupplier ;
65
65
66
66
/*package-private*/ RecordTransformer (final Metafix metafix , final Fix fix ) {
67
67
this (metafix , fix .getElements ());
68
68
}
69
69
70
70
private RecordTransformer (final Metafix metafix , final List <Expression > expressions ) {
71
- this .expressions = expressions ;
72
71
this .metafix = metafix ;
73
72
vars = metafix .getVars ();
74
- }
75
-
76
- public void transform (final Record currentRecord ) {
77
- this .record = currentRecord ;
78
- process (expressions );
79
- }
80
-
81
- private void process (final List <Expression > currentExpressions ) {
82
- currentExpressions .forEach (e -> {
83
- final List <String > params = resolveParams (e .getParams ());
84
73
74
+ expressions .forEach (e -> {
85
75
if (e instanceof Do ) {
86
- processDo ((Do ) e , params );
76
+ processDo ((Do ) e );
87
77
}
88
78
else if (e instanceof If ) {
89
- processIf ((If ) e , params );
79
+ processIf ((If ) e );
90
80
}
91
81
else if (e instanceof Unless ) {
92
- processUnless ((Unless ) e , params );
82
+ processUnless ((Unless ) e );
93
83
}
94
84
else if (e instanceof MethodCall ) {
95
- processFunction ((MethodCall ) e , params );
85
+ processFunction ((MethodCall ) e );
96
86
}
97
87
else {
98
88
throw new FixProcessException (executionExceptionMessage (e ));
99
89
}
100
90
});
101
91
}
102
92
103
- private void processDo (final Do expression , final List <String > params ) {
104
- processExpression (expression , name -> {
105
- final FixContext context = getInstance (name , FixContext .class , FixBind ::valueOf );
106
- context .execute (metafix , record , params , options (expression .getOptions ()), new RecordTransformer (metafix , expression .getElements ()));
107
- });
93
+ public void transform (final Record record ) {
94
+ consumers .forEach (consumer -> {
95
+ final FixExecutionException exception = tryRun (() -> consumer .accept (record ));
108
96
109
- // TODO, possibly: use morph collectors here
110
- // final CollectFactory collectFactory = new CollectFactory( );
111
- // final Map<String, String> attributes = resolvedAttributeMap(params, expression.getOptions());
112
- // final Collect collect = collectFactory.newInstance(expression.getName(), attributes );
97
+ if ( exception != null ) {
98
+ metafix . getStrictness (). handle ( exception , record );
99
+ }
100
+ } );
113
101
}
114
102
115
- private void processIf (final If expression , final List <String > params ) {
116
- final ElsIf elseIfExpression = expression .getElseIf ();
117
- final Else elseExpression = expression .getElse ();
103
+ private void processDo (final Do expression ) {
104
+ processFix (() -> executionExceptionMessage (expression ), () -> {
105
+ final FixContext context = getInstance (expression .getName (), FixContext .class , FixBind ::valueOf );
106
+ final RecordTransformer recordTransformer = new RecordTransformer (metafix , expression .getElements ());
118
107
119
- if (testConditional (expression , expression .eResource (), expression .getName (), params )) {
120
- process (expression .getElements ());
121
- }
122
- else if (elseIfExpression != null && testConditional (elseIfExpression ,
123
- elseIfExpression .eResource (), elseIfExpression .getName (), resolveParams (elseIfExpression .getParams ()))) {
124
- process (elseIfExpression .getElements ());
125
- }
126
- else if (elseExpression != null ) {
127
- process (elseExpression .getElements ());
128
- }
108
+ return record -> context .execute (metafix , record , params (expression .getParams ()), options (expression .getOptions ()), recordTransformer );
109
+ });
129
110
}
130
111
131
- private void processUnless (final Unless expression , final List <String > params ) {
132
- if (!testConditional (expression , expression .eResource (), expression .getName (), params )) {
133
- process (expression .getElements ());
134
- }
112
+ private void processIf (final If ifExpression ) {
113
+ final ElsIf elseIfExpression = ifExpression .getElseIf ();
114
+ final Else elseExpression = ifExpression .getElse ();
115
+
116
+ final Supplier <String > elseIfMessageSupplier = () -> executionExceptionMessage (elseIfExpression , elseIfExpression .eResource ());
117
+ final Supplier <String > elseMessageSupplier = () -> executionExceptionMessage (elseExpression , elseExpression .eResource ());
118
+
119
+ processFix (() -> executionExceptionMessage (ifExpression , ifExpression .eResource ()), () -> {
120
+ final FixPredicate ifPredicate = getInstance (ifExpression .getName (), FixPredicate .class , FixConditional ::valueOf );
121
+ final FixPredicate elseIfPredicate = elseIfExpression != null ? getInstance (elseIfExpression .getName (), FixPredicate .class , FixConditional ::valueOf ) : null ;
122
+
123
+ final RecordTransformer ifTransformer = new RecordTransformer (metafix , ifExpression .getElements ());
124
+ final RecordTransformer elseIfTransformer = elseIfExpression != null ? new RecordTransformer (metafix , elseIfExpression .getElements ()) : null ;
125
+ final RecordTransformer elseTransformer = elseExpression != null ? new RecordTransformer (metafix , elseExpression .getElements ()) : null ;
126
+
127
+ return record -> {
128
+ if (ifPredicate .test (metafix , record , params (ifExpression .getParams ()), options (null ))) { // TODO: options
129
+ ifTransformer .transform (record );
130
+ }
131
+ else {
132
+ if (elseIfExpression != null ) {
133
+ currentMessageSupplier = elseIfMessageSupplier ;
134
+
135
+ if (elseIfPredicate .test (metafix , record , params (elseIfExpression .getParams ()), options (null ))) { // TODO: options
136
+ elseIfTransformer .transform (record );
137
+ return ;
138
+ }
139
+ }
140
+
141
+ if (elseExpression != null ) {
142
+ currentMessageSupplier = elseMessageSupplier ;
143
+ elseTransformer .transform (record );
144
+ }
145
+ }
146
+ };
147
+ });
135
148
}
136
149
137
- private boolean testConditional (final EObject object , final Resource resource , final String conditional , final List < String > params ) {
138
- LOG . debug ( "<IF>: {} parameters: {}" , conditional , params );
139
-
140
- final AtomicBoolean bool = new AtomicBoolean ( );
150
+ private void processUnless (final Unless expression ) {
151
+ processFix (() -> executionExceptionMessage ( expression , expression . eResource ()), () -> {
152
+ final FixPredicate predicate = getInstance ( expression . getName (), FixPredicate . class , FixConditional :: valueOf );
153
+ final RecordTransformer recordTransformer = new RecordTransformer ( metafix , expression . getElements () );
141
154
142
- processFix (() -> executionExceptionMessage (object , resource ), () -> {
143
- final FixPredicate predicate = getInstance (conditional , FixPredicate .class , FixConditional ::valueOf );
144
- bool .set (predicate .test (metafix , record , params , options (null ))); // TODO: options
155
+ return record -> {
156
+ if (!predicate .test (metafix , record , params (expression .getParams ()), options (null ))) { // TODO: options
157
+ recordTransformer .transform (record );
158
+ }
159
+ };
145
160
});
146
-
147
- return bool .get ();
148
-
149
- // TODO, possibly: use morph functions here (& in processFunction):
150
- // final FunctionFactory functionFactory = new FunctionFactory();
151
- // functionFactory.registerClass("not_equals", NotEquals.class);
152
- // functionFactory.registerClass("replace_all", Replace.class);
153
- // final Function function = functionFactory.newInstance(conditional,
154
- // resolvedAttributeMap(params, theIf.getOptions()));
155
161
}
156
162
157
- private void processFunction (final MethodCall expression , final List < String > params ) {
158
- processExpression ( expression , name -> {
159
- final FixFunction function = getInstance (name , FixFunction .class , FixMethod ::valueOf );
160
- function .apply (metafix , record , params , options (expression .getOptions ()));
163
+ private void processFunction (final MethodCall expression ) {
164
+ processFix (() -> executionExceptionMessage ( expression ), () -> {
165
+ final FixFunction function = getInstance (expression . getName () , FixFunction .class , FixMethod ::valueOf );
166
+ return record -> function .apply (metafix , record , params ( expression . getParams ()) , options (expression .getOptions ()));
161
167
});
162
168
}
163
169
164
170
private <T > T getInstance (final String name , final Class <T > baseType , final Function <String , ? extends T > enumFunction ) {
165
171
return name .contains ("." ) ? ReflectionUtil .loadClass (name , baseType ).newInstance () : enumFunction .apply (name );
166
172
}
167
173
168
- private void processExpression (final Expression expression , final Consumer <String > consumer ) {
169
- processFix (() -> executionExceptionMessage (expression ), () -> consumer .accept (expression .getName ()));
170
- }
174
+ private void processFix (final Supplier <String > messageSupplier , final Supplier <Consumer <Record >> consumerSupplier ) {
175
+ currentMessageSupplier = messageSupplier ;
176
+
177
+ final FixExecutionException exception = tryRun (() -> {
178
+ final Consumer <Record > consumer = consumerSupplier .get ();
179
+
180
+ consumers .add (record -> {
181
+ currentMessageSupplier = messageSupplier ;
182
+ consumer .accept (record );
183
+ });
184
+ });
171
185
172
- private void processFix (final Supplier <String > messageSupplier , final Runnable runnable ) {
173
- final FixExecutionException exception = tryRun (messageSupplier , runnable );
174
186
if (exception != null ) {
175
- metafix . getStrictness (). handle ( exception , record ) ;
187
+ throw exception ;
176
188
}
177
189
}
178
190
179
- private FixExecutionException tryRun (final Supplier < String > messageSupplier , final Runnable runnable ) { // checkstyle-disable-line ReturnCount
191
+ private FixExecutionException tryRun (final Runnable runnable ) { // checkstyle-disable-line ReturnCount
180
192
try {
181
193
runnable .run ();
182
194
}
@@ -187,11 +199,12 @@ private FixExecutionException tryRun(final Supplier<String> messageSupplier, fin
187
199
return e ; // TODO: Add nesting information?
188
200
}
189
201
catch (final IllegalStateException | NumberFormatException e ) {
190
- return new FixExecutionException (messageSupplier .get (), e );
202
+ return new FixExecutionException (currentMessageSupplier .get (), e );
191
203
}
192
204
catch (final RuntimeException e ) { // checkstyle-disable-line IllegalCatch
193
- throw new FixProcessException (messageSupplier .get (), e );
205
+ throw new FixProcessException (currentMessageSupplier .get (), e );
194
206
}
207
+
195
208
return null ;
196
209
}
197
210
@@ -206,14 +219,14 @@ private String executionExceptionMessage(final EObject object, final Resource re
206
219
resource .getURI (), node .getStartLine (), NodeModelUtils .getTokenText (node ));
207
220
}
208
221
209
- private List <String > resolveParams (final List <String > params ) {
210
- return params .stream ().map (this ::resolveVars ).collect (Collectors .toList ());
211
- }
212
-
213
222
private String resolveVars (final String value ) {
214
223
return value == null ? null : StringUtil .format (value , Metafix .VAR_START , Metafix .VAR_END , false , vars );
215
224
}
216
225
226
+ private List <String > params (final List <String > params ) {
227
+ return params .stream ().map (this ::resolveVars ).collect (Collectors .toList ());
228
+ }
229
+
217
230
private Map <String , String > options (final Options options ) {
218
231
final Map <String , String > map = new LinkedHashMap <>();
219
232
0 commit comments