70
70
import java .security .PrivilegedAction ;
71
71
import java .util .List ;
72
72
73
+ import static org .codehaus .groovy .runtime .MetaClassHelper .EMPTY_CLASS_ARRAY ;
74
+
73
75
/**
74
76
* Java 8 based functions.
75
77
*
76
78
* @since 2.5.0
77
79
*/
78
80
public class Java8 implements VMPlugin {
79
81
80
- private static final Class <?>[] EMPTY_CLASS_ARRAY = new Class [0 ];
81
82
private static final Method [] EMPTY_METHOD_ARRAY = new Method [0 ];
82
83
private static final Annotation [] EMPTY_ANNOTATION_ARRAY = new Annotation [0 ];
83
84
private static final Permission ACCESS_PERMISSION = new ReflectPermission ("suppressAccessChecks" );
84
85
85
- public static GenericsType configureTypeVariableDefinition (final ClassNode base , final ClassNode [] cBounds ) {
86
+ public static GenericsType configureTypeVariableDefinition (final ClassNode base , final ClassNode [] bounds ) {
86
87
ClassNode redirect = base .redirect ();
87
88
base .setRedirect (null );
88
89
GenericsType gt ;
89
- if (cBounds == null || cBounds .length == 0 ) {
90
+ if (bounds == null || bounds .length == 0 ) {
90
91
gt = new GenericsType (base );
91
92
} else {
92
- gt = new GenericsType (base , cBounds , null );
93
+ // GROOVY-10756: fix erasure -- ResolveVisitor#resolveGenericsHeader
94
+ if (!ClassHelper .OBJECT_TYPE .equals (bounds [0 ])) redirect = bounds [0 ];
95
+ gt = new GenericsType (base , bounds , null );
93
96
gt .setName (base .getName ());
94
97
gt .setPlaceholder (true );
95
98
}
96
99
base .setRedirect (redirect );
97
100
return gt ;
98
101
}
99
102
100
- private static ClassNode configureClass (final Class <?> c ) {
101
- if (c .isPrimitive ()) {
102
- return ClassHelper .make (c );
103
- } else {
104
- return ClassHelper .makeWithoutCaching (c , false );
105
- }
106
- }
107
-
108
103
public static ClassNode configureTypeVariableReference (final String name ) {
109
104
ClassNode cn = ClassHelper .makeWithoutCaching (name );
110
105
cn .setGenericsPlaceHolder (true );
111
106
ClassNode cn2 = ClassHelper .makeWithoutCaching (name );
112
107
cn2 .setGenericsPlaceHolder (true );
113
- GenericsType [] gts = new GenericsType []{ new GenericsType ( cn2 )};
114
- cn .setGenericsTypes (gts );
108
+
109
+ cn .setGenericsTypes (new GenericsType []{ new GenericsType ( cn2 )} );
115
110
cn .setRedirect (ClassHelper .OBJECT_TYPE );
116
111
return cn ;
117
112
}
118
113
114
+ private static ClassNode configureClass (final Class <?> c ) {
115
+ if (c .isPrimitive ()) {
116
+ return ClassHelper .make (c );
117
+ } else {
118
+ return ClassHelper .makeWithoutCaching (c , false );
119
+ }
120
+ }
121
+
119
122
private static void setRetentionPolicy (final RetentionPolicy value , final AnnotationNode node ) {
120
123
switch (value ) {
121
124
case RUNTIME :
@@ -132,14 +135,7 @@ private static void setRetentionPolicy(final RetentionPolicy value, final Annota
132
135
}
133
136
}
134
137
135
- private static void setMethodDefaultValue (final MethodNode mn , final Method m ) {
136
- // GRECLIPSE add -- GROOVY-10862
137
- if (m .getDefaultValue () == null ) return ;
138
- // GRECLIPSE end
139
- ConstantExpression cExp = new ConstantExpression (m .getDefaultValue ());
140
- mn .setCode (new ReturnStatement (cExp ));
141
- mn .setAnnotationDefault (true );
142
- }
138
+ //--------------------------------------------------------------------------
143
139
144
140
@ Override
145
141
public Class <?>[] getPluginDefaultGroovyMethods () {
@@ -190,27 +186,7 @@ protected int getElementCode(final ElementType value) {
190
186
191
187
@ Override
192
188
public void setAdditionalClassInformation (final ClassNode cn ) {
193
- setGenericsTypes (cn );
194
- }
195
-
196
- private void setGenericsTypes (final ClassNode cn ) {
197
- TypeVariable [] tvs = cn .getTypeClass ().getTypeParameters ();
198
- GenericsType [] gts = configureTypeVariable (tvs );
199
- cn .setGenericsTypes (gts );
200
- }
201
-
202
- private GenericsType [] configureTypeVariable (final TypeVariable [] tvs ) {
203
- final int n = tvs .length ;
204
- if (n == 0 ) return null ;
205
- GenericsType [] gts = new GenericsType [n ];
206
- for (int i = 0 ; i < n ; i += 1 ) {
207
- gts [i ] = configureTypeVariableDefinition (tvs [i ]);
208
- }
209
- return gts ;
210
- }
211
-
212
- private GenericsType configureTypeVariableDefinition (final TypeVariable tv ) {
213
- return configureTypeVariableDefinition (configureTypeVariableReference (tv .getName ()), configureTypes (tv .getBounds ()));
189
+ cn .setGenericsTypes (configureTypeParameters (cn .getTypeClass ().getTypeParameters ()));
214
190
}
215
191
216
192
private ClassNode [] configureTypes (final Type [] types ) {
@@ -231,7 +207,7 @@ private ClassNode configureType(final Type type) {
231
207
} else if (type instanceof GenericArrayType ) {
232
208
return configureGenericArray ((GenericArrayType ) type );
233
209
} else if (type instanceof TypeVariable ) {
234
- return configureTypeVariableReference (((TypeVariable ) type ).getName ());
210
+ return configureTypeVariableReference (((TypeVariable <?> ) type ).getName ());
235
211
} else if (type instanceof Class ) {
236
212
return configureClass ((Class <?>) type );
237
213
} else if (type == null ) {
@@ -289,6 +265,18 @@ private GenericsType[] configureTypeArguments(final Type[] ta) {
289
265
return gts ;
290
266
}
291
267
268
+ private GenericsType [] configureTypeParameters (final TypeVariable <?>[] tp ) {
269
+ final int n = tp .length ;
270
+ if (n == 0 ) return null ;
271
+ GenericsType [] gt = new GenericsType [n ];
272
+ for (int i = 0 ; i < n ; i += 1 ) {
273
+ ClassNode t = configureTypeVariableReference (tp [i ].getName ());
274
+ ClassNode [] bounds = configureTypes (tp [i ].getBounds ());
275
+ gt [i ] = configureTypeVariableDefinition (t , bounds );
276
+ }
277
+ return gt ;
278
+ }
279
+
292
280
//
293
281
294
282
@ Override
@@ -381,8 +369,7 @@ private Expression toAnnotationValueExpression(final Object value) {
381
369
382
370
@ Override
383
371
public void configureAnnotationNodeFromDefinition (final AnnotationNode definition , final AnnotationNode root ) {
384
- ClassNode type = definition .getClassNode ();
385
- final String typeName = type .getName ();
372
+ String typeName = definition .getClassNode ().getName ();
386
373
if ("java.lang.annotation.Retention" .equals (typeName )) {
387
374
Expression exp = definition .getMember ("value" );
388
375
if (!(exp instanceof PropertyExpression )) return ;
@@ -393,16 +380,16 @@ public void configureAnnotationNodeFromDefinition(final AnnotationNode definitio
393
380
} else if ("java.lang.annotation.Target" .equals (typeName )) {
394
381
Expression exp = definition .getMember ("value" );
395
382
if (!(exp instanceof ListExpression )) return ;
396
- ListExpression le = (ListExpression ) exp ;
397
- int bitmap = 0 ;
398
- for (Expression e : le .getExpressions ()) {
383
+ ListExpression list = (ListExpression ) exp ;
384
+ int targets = 0 ;
385
+ for (Expression e : list .getExpressions ()) {
399
386
if (!(e instanceof PropertyExpression )) return ;
400
387
PropertyExpression element = (PropertyExpression ) e ;
401
388
String name = element .getPropertyAsString ();
402
- ElementType value = ElementType .valueOf (name );
403
- bitmap |= getElementCode (value );
389
+ ElementType type = ElementType .valueOf (name );
390
+ targets |= getElementCode (type );
404
391
}
405
- root .setAllowedTargets (bitmap );
392
+ root .setAllowedTargets (targets );
406
393
}
407
394
}
408
395
@@ -412,29 +399,33 @@ public void configureClassNode(final CompileUnit compileUnit, final ClassNode cl
412
399
Class <?> clazz = classNode .getTypeClass ();
413
400
Field [] fields = clazz .getDeclaredFields ();
414
401
for (Field f : fields ) {
415
- ClassNode ret = makeClassNode (compileUnit , f .getGenericType (), f .getType ());
416
- FieldNode fn = new FieldNode (f .getName (), f .getModifiers (), ret , classNode , null );
402
+ ClassNode rt = makeClassNode (compileUnit , f .getGenericType (), f .getType ());
403
+ FieldNode fn = new FieldNode (f .getName (), f .getModifiers (), rt , classNode , null );
417
404
setAnnotationMetaData (f .getAnnotations (), fn );
418
405
classNode .addField (fn );
419
406
}
420
407
Method [] methods = clazz .getDeclaredMethods ();
421
408
for (Method m : methods ) {
422
- ClassNode ret = makeClassNode (compileUnit , m .getGenericReturnType (), m .getReturnType ());
409
+ ClassNode rt = makeClassNode (compileUnit , m .getGenericReturnType (), m .getReturnType ());
423
410
Parameter [] params = makeParameters (compileUnit , m .getGenericParameterTypes (), m .getParameterTypes (), m .getParameterAnnotations (), m );
424
411
ClassNode [] exceptions = makeClassNodes (compileUnit , m .getGenericExceptionTypes (), m .getExceptionTypes ());
425
- MethodNode mn = new MethodNode (m .getName (), m .getModifiers (), ret , params , exceptions , null );
426
- mn .setSynthetic (m .isSynthetic ());
427
- setMethodDefaultValue (mn , m );
412
+ MethodNode mn = new MethodNode (m .getName (), m .getModifiers (), rt , params , exceptions , null );
428
413
setAnnotationMetaData (m .getAnnotations (), mn );
429
- mn .setGenericsTypes (configureTypeVariable (m .getTypeParameters ()));
414
+ // GRECLIPSE edit --- GROOVY-10862
415
+ if (m .getDefaultValue () != null ) {
416
+ mn .setAnnotationDefault (true );
417
+ mn .setCode (new ReturnStatement (new ConstantExpression (m .getDefaultValue ())));
418
+ }
419
+ mn .setGenericsTypes (configureTypeParameters (m .getTypeParameters ()));
420
+ mn .setSynthetic (m .isSynthetic ());
430
421
classNode .addMethod (mn );
431
422
}
432
423
Constructor <?>[] constructors = clazz .getDeclaredConstructors ();
433
- for (Constructor <?> ctor : constructors ) {
434
- Parameter [] params = makeParameters (compileUnit , ctor .getGenericParameterTypes (), ctor .getParameterTypes (), getConstructorParameterAnnotations (ctor ), ctor );
435
- ClassNode [] exceptions = makeClassNodes (compileUnit , ctor .getGenericExceptionTypes (), ctor .getExceptionTypes ());
436
- ConstructorNode cn = classNode .addConstructor (ctor .getModifiers (), params , exceptions , null );
437
- setAnnotationMetaData (ctor .getAnnotations (), cn );
424
+ for (Constructor <?> c : constructors ) {
425
+ Parameter [] params = makeParameters (compileUnit , c .getGenericParameterTypes (), c .getParameterTypes (), getConstructorParameterAnnotations (c ), c );
426
+ ClassNode [] exceptions = makeClassNodes (compileUnit , c .getGenericExceptionTypes (), c .getExceptionTypes ());
427
+ ConstructorNode cn = classNode .addConstructor (c .getModifiers (), params , exceptions , null );
428
+ setAnnotationMetaData (c .getAnnotations (), cn );
438
429
}
439
430
440
431
Class <?> sc = clazz .getSuperclass ();
@@ -575,7 +566,7 @@ private Parameter[] makeParameters(final CompileUnit cu, final Type[] types, fin
575
566
fillParameterNames (names , member );
576
567
for (int i = 0 ; i < n ; i += 1 ) {
577
568
setAnnotationMetaData (parameterAnnotations [i ],
578
- params [i ] = new Parameter (makeClassNode (cu , types [i ], cls [i ]), names [i ]));
569
+ params [i ] = new Parameter (makeClassNode (cu , types [i ], cls [i ]), names [i ]));
579
570
}
580
571
}
581
572
return params ;
@@ -592,6 +583,8 @@ protected void fillParameterNames(final String[] names, final Member member) {
592
583
}
593
584
}
594
585
586
+ //--------------------------------------------------------------------------
587
+
595
588
/**
596
589
* The following scenarios can not set accessible, i.e. the return value is false
597
590
* 1) SecurityException occurred
0 commit comments