7070import java .security .PrivilegedAction ;
7171import java .util .List ;
7272
73+ import static org .codehaus .groovy .runtime .MetaClassHelper .EMPTY_CLASS_ARRAY ;
74+
7375/**
7476 * Java 8 based functions.
7577 *
7678 * @since 2.5.0
7779 */
7880public class Java8 implements VMPlugin {
7981
80- private static final Class <?>[] EMPTY_CLASS_ARRAY = new Class [0 ];
8182 private static final Method [] EMPTY_METHOD_ARRAY = new Method [0 ];
8283 private static final Annotation [] EMPTY_ANNOTATION_ARRAY = new Annotation [0 ];
8384 private static final Permission ACCESS_PERMISSION = new ReflectPermission ("suppressAccessChecks" );
8485
85- public static GenericsType configureTypeVariableDefinition (final ClassNode base , final ClassNode [] cBounds ) {
86+ public static GenericsType configureTypeVariableDefinition (final ClassNode base , final ClassNode [] bounds ) {
8687 ClassNode redirect = base .redirect ();
8788 base .setRedirect (null );
8889 GenericsType gt ;
89- if (cBounds == null || cBounds .length == 0 ) {
90+ if (bounds == null || bounds .length == 0 ) {
9091 gt = new GenericsType (base );
9192 } 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 );
9396 gt .setName (base .getName ());
9497 gt .setPlaceholder (true );
9598 }
9699 base .setRedirect (redirect );
97100 return gt ;
98101 }
99102
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-
108103 public static ClassNode configureTypeVariableReference (final String name ) {
109104 ClassNode cn = ClassHelper .makeWithoutCaching (name );
110105 cn .setGenericsPlaceHolder (true );
111106 ClassNode cn2 = ClassHelper .makeWithoutCaching (name );
112107 cn2 .setGenericsPlaceHolder (true );
113- GenericsType [] gts = new GenericsType []{ new GenericsType ( cn2 )};
114- cn .setGenericsTypes (gts );
108+
109+ cn .setGenericsTypes (new GenericsType []{ new GenericsType ( cn2 )} );
115110 cn .setRedirect (ClassHelper .OBJECT_TYPE );
116111 return cn ;
117112 }
118113
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+
119122 private static void setRetentionPolicy (final RetentionPolicy value , final AnnotationNode node ) {
120123 switch (value ) {
121124 case RUNTIME :
@@ -132,14 +135,7 @@ private static void setRetentionPolicy(final RetentionPolicy value, final Annota
132135 }
133136 }
134137
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+ //--------------------------------------------------------------------------
143139
144140 @ Override
145141 public Class <?>[] getPluginDefaultGroovyMethods () {
@@ -190,27 +186,7 @@ protected int getElementCode(final ElementType value) {
190186
191187 @ Override
192188 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 ()));
214190 }
215191
216192 private ClassNode [] configureTypes (final Type [] types ) {
@@ -231,7 +207,7 @@ private ClassNode configureType(final Type type) {
231207 } else if (type instanceof GenericArrayType ) {
232208 return configureGenericArray ((GenericArrayType ) type );
233209 } else if (type instanceof TypeVariable ) {
234- return configureTypeVariableReference (((TypeVariable ) type ).getName ());
210+ return configureTypeVariableReference (((TypeVariable <?> ) type ).getName ());
235211 } else if (type instanceof Class ) {
236212 return configureClass ((Class <?>) type );
237213 } else if (type == null ) {
@@ -289,6 +265,18 @@ private GenericsType[] configureTypeArguments(final Type[] ta) {
289265 return gts ;
290266 }
291267
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+
292280 //
293281
294282 @ Override
@@ -381,8 +369,7 @@ private Expression toAnnotationValueExpression(final Object value) {
381369
382370 @ Override
383371 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 ();
386373 if ("java.lang.annotation.Retention" .equals (typeName )) {
387374 Expression exp = definition .getMember ("value" );
388375 if (!(exp instanceof PropertyExpression )) return ;
@@ -393,16 +380,16 @@ public void configureAnnotationNodeFromDefinition(final AnnotationNode definitio
393380 } else if ("java.lang.annotation.Target" .equals (typeName )) {
394381 Expression exp = definition .getMember ("value" );
395382 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 ()) {
399386 if (!(e instanceof PropertyExpression )) return ;
400387 PropertyExpression element = (PropertyExpression ) e ;
401388 String name = element .getPropertyAsString ();
402- ElementType value = ElementType .valueOf (name );
403- bitmap |= getElementCode (value );
389+ ElementType type = ElementType .valueOf (name );
390+ targets |= getElementCode (type );
404391 }
405- root .setAllowedTargets (bitmap );
392+ root .setAllowedTargets (targets );
406393 }
407394 }
408395
@@ -412,29 +399,33 @@ public void configureClassNode(final CompileUnit compileUnit, final ClassNode cl
412399 Class <?> clazz = classNode .getTypeClass ();
413400 Field [] fields = clazz .getDeclaredFields ();
414401 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 );
417404 setAnnotationMetaData (f .getAnnotations (), fn );
418405 classNode .addField (fn );
419406 }
420407 Method [] methods = clazz .getDeclaredMethods ();
421408 for (Method m : methods ) {
422- ClassNode ret = makeClassNode (compileUnit , m .getGenericReturnType (), m .getReturnType ());
409+ ClassNode rt = makeClassNode (compileUnit , m .getGenericReturnType (), m .getReturnType ());
423410 Parameter [] params = makeParameters (compileUnit , m .getGenericParameterTypes (), m .getParameterTypes (), m .getParameterAnnotations (), m );
424411 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 );
428413 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 ());
430421 classNode .addMethod (mn );
431422 }
432423 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 );
438429 }
439430
440431 Class <?> sc = clazz .getSuperclass ();
@@ -575,7 +566,7 @@ private Parameter[] makeParameters(final CompileUnit cu, final Type[] types, fin
575566 fillParameterNames (names , member );
576567 for (int i = 0 ; i < n ; i += 1 ) {
577568 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 ]));
579570 }
580571 }
581572 return params ;
@@ -592,6 +583,8 @@ protected void fillParameterNames(final String[] names, final Member member) {
592583 }
593584 }
594585
586+ //--------------------------------------------------------------------------
587+
595588 /**
596589 * The following scenarios can not set accessible, i.e. the return value is false
597590 * 1) SecurityException occurred
0 commit comments