46
46
import static org .codehaus .groovy .transform .sc .StaticCompilationMetadataKeys .*;
47
47
import static org .codehaus .groovy .transform .stc .StaticTypesMarker .DIRECT_METHOD_CALL_TARGET ;
48
48
import static org .objectweb .asm .Opcodes .ACC_PUBLIC ;
49
+ import static org .objectweb .asm .Opcodes .ACC_STATIC ;
50
+ import static org .objectweb .asm .Opcodes .ACC_SYNTHETIC ;
49
51
50
52
/**
51
53
* This visitor is responsible for amending the AST with static compilation metadata or transform the AST so that
@@ -248,6 +250,7 @@ private static void addPrivateBridgeMethods(final ClassNode node) {
248
250
Set <ASTNode > accessedMethods = (Set <ASTNode >) node .getNodeMetaData (StaticTypesMarker .PV_METHODS_ACCESS );
249
251
if (accessedMethods ==null ) return ;
250
252
List <MethodNode > methods = new ArrayList <MethodNode >(node .getAllDeclaredMethods ());
253
+ methods .addAll (node .getDeclaredConstructors ());
251
254
Map <MethodNode , MethodNode > privateBridgeMethods = (Map <MethodNode , MethodNode >) node .getNodeMetaData (PRIVATE_BRIDGE_METHODS );
252
255
if (privateBridgeMethods !=null ) {
253
256
// private bridge methods already added
@@ -273,7 +276,6 @@ private static void addPrivateBridgeMethods(final ClassNode node) {
273
276
orig .getName ()
274
277
);
275
278
}
276
- newParams [0 ] = new Parameter (node .getPlainNodeReference (), "$that" );
277
279
Expression arguments ;
278
280
if (method .getParameters ()==null || method .getParameters ().length ==0 ) {
279
281
arguments = ArgumentListExpression .EMPTY_ARGUMENTS ;
@@ -284,17 +286,36 @@ private static void addPrivateBridgeMethods(final ClassNode node) {
284
286
}
285
287
arguments = new ArgumentListExpression (args );
286
288
}
287
- Expression receiver = method .isStatic ()?new ClassExpression (node ):new VariableExpression (newParams [0 ]);
288
- MethodCallExpression mce = new MethodCallExpression (receiver , method .getName (), arguments );
289
- mce .setMethodTarget (method );
290
-
291
- ExpressionStatement returnStatement = new ExpressionStatement (mce );
292
- MethodNode bridge = node .addMethod (
293
- "access$" +i , access ,
294
- correctToGenericsSpecRecurse (genericsSpec , method .getReturnType (), methodSpecificGenerics ),
295
- newParams ,
296
- method .getExceptions (),
297
- returnStatement );
289
+
290
+ MethodNode bridge ;
291
+ if (method instanceof ConstructorNode ) {
292
+ // create constructor with a nested class as the first parameter, creating one if necessary
293
+ ClassNode thatType = null ;
294
+ Iterator <InnerClassNode > innerClasses = node .getInnerClasses ();
295
+ if (innerClasses .hasNext ()) {
296
+ thatType = innerClasses .next ();
297
+ } else {
298
+ thatType = new InnerClassNode (node .redirect (), node .getName () + "$1" , ACC_STATIC | ACC_SYNTHETIC , ClassHelper .OBJECT_TYPE );
299
+ node .getModule ().addClass (thatType );
300
+ }
301
+ newParams [0 ] = new Parameter (thatType .getPlainNodeReference (), "$that" );
302
+ Expression cce = new ConstructorCallExpression (ClassNode .THIS , arguments );
303
+ Statement body = new ExpressionStatement (cce );
304
+ bridge = node .addConstructor (ACC_SYNTHETIC , newParams , ClassNode .EMPTY_ARRAY , body );
305
+ } else {
306
+ newParams [0 ] = new Parameter (node .getPlainNodeReference (), "$that" );
307
+ Expression receiver = method .isStatic ()?new ClassExpression (node ):new VariableExpression (newParams [0 ]);
308
+ MethodCallExpression mce = new MethodCallExpression (receiver , method .getName (), arguments );
309
+ mce .setMethodTarget (method );
310
+
311
+ ExpressionStatement returnStatement = new ExpressionStatement (mce );
312
+ bridge = node .addMethod (
313
+ "access$" +i , access ,
314
+ correctToGenericsSpecRecurse (genericsSpec , method .getReturnType (), methodSpecificGenerics ),
315
+ newParams ,
316
+ method .getExceptions (),
317
+ returnStatement );
318
+ }
298
319
GenericsType [] origGenericsTypes = method .getGenericsTypes ();
299
320
if (origGenericsTypes !=null ) {
300
321
bridge .setGenericsTypes (applyGenericsContextToPlaceHolders (genericsSpec ,origGenericsTypes ));
0 commit comments