1
1
/*
2
- * Copyright 2009-2022 the original author or authors.
2
+ * Copyright 2009-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -1442,9 +1442,7 @@ private void createConstructorDeclarations(ClassNode classNode, boolean isEnum,
1442
1442
// add default constructor if no other constructors exist (and not anonymous/interface/trait)
1443
1443
} else if (constructorNodes .isEmpty () && !isAnon && !classNode .isInterface () && !isTrait (classNode )) {
1444
1444
ConstructorDeclaration constructorDecl = new ConstructorDeclaration (unitDeclaration .compilationResult );
1445
- constructorDecl .annotations = new Annotation [] {
1446
- new MarkerAnnotation (createTypeReferenceForClassNode (ClassHelper .make (groovy .transform .Generated .class )), -1 )
1447
- };
1445
+ constructorDecl .annotations = createAnnotations (ClassHelper .make (groovy .transform .Generated .class ));
1448
1446
if (!isEnum ) {
1449
1447
if (classNode .getObjectInitializerStatements ().isEmpty ()) {
1450
1448
constructorDecl .bits |= ASTNode .IsDefaultConstructor ;
@@ -1463,9 +1461,7 @@ private void createConstructorDeclarations(ClassNode classNode, boolean isEnum,
1463
1461
1464
1462
if (isEnum ) { // named arguments constructor
1465
1463
constructorDecl = new ConstructorDeclaration (unitDeclaration .compilationResult );
1466
- constructorDecl .annotations = new Annotation [] {
1467
- new MarkerAnnotation (createTypeReferenceForClassNode (ClassHelper .make (groovy .transform .Generated .class )), -1 )
1468
- };
1464
+ constructorDecl .annotations = createAnnotations (ClassHelper .make (groovy .transform .Generated .class ));
1469
1465
constructorDecl .arguments = createArguments (new Parameter [] {
1470
1466
new Parameter (ClassHelper .makeWithoutCaching (java .util .LinkedHashMap .class , false ), "__namedArgs" )
1471
1467
});
@@ -1758,35 +1754,38 @@ private void configurePermittedSubtypes(TypeDeclaration typeDeclaration, ClassNo
1758
1754
}
1759
1755
}
1760
1756
1761
- private Annotation [] createAnnotations (List <AnnotationNode > groovyAnnotations ) {
1762
- if (groovyAnnotations != null && !groovyAnnotations .isEmpty ()) {
1763
- List <Annotation > annotations = new ArrayList <>(groovyAnnotations .size ());
1757
+ private Annotation [] createAnnotations (ClassNode annotationType ) {
1758
+ Annotation annotation = new MarkerAnnotation (createTypeReferenceForClassNode (annotationType ), -1 );
1759
+ annotation .declarationSourceEnd = annotation .sourceEnd ;
1760
+ return new Annotation [] {annotation };
1761
+ }
1764
1762
1765
- for (AnnotationNode annotationNode : groovyAnnotations ) {
1766
- TypeReference annotationReference = createTypeReferenceForClassNode (annotationNode .getClassNode ());
1767
- annotationReference .sourceStart = annotationNode .getStart ();
1768
- annotationReference .sourceEnd = annotationNode .getEnd () - 1 ;
1763
+ private Annotation [] createAnnotations (List <AnnotationNode > annotationNodes ) {
1764
+ if (annotationNodes == null || annotationNodes .isEmpty ()) return null ;
1765
+ Annotation [] annotations = new Annotation [annotationNodes .size ()];
1766
+ int i = 0 ;
1767
+ for (AnnotationNode annotationNode : annotationNodes ) {
1768
+ TypeReference annotationType = createTypeReferenceForClassNode (annotationNode .getClassNode ());
1769
+ annotationType .sourceStart = annotationNode .getStart ();
1770
+ annotationType .sourceEnd = annotationNode .getEnd () - 1 ;
1769
1771
1770
- Map <String , Expression > memberValuePairs = annotationNode .getMembers ();
1771
- if (memberValuePairs == null || memberValuePairs .isEmpty ()) {
1772
- MarkerAnnotation annotation = new MarkerAnnotation (annotationReference , annotationReference .sourceStart );
1773
- annotations .add (annotation );
1774
- } else if (memberValuePairs .size () == 1 && memberValuePairs .containsKey ("value" )) {
1775
- SingleMemberAnnotation annotation = new SingleMemberAnnotation (annotationReference , annotationReference .sourceStart );
1776
- annotation .memberValue = createAnnotationMemberExpression (memberValuePairs .get ("value" ), null );
1777
- annotations .add (annotation );
1778
- } else {
1779
- NormalAnnotation annotation = new NormalAnnotation (annotationReference , annotationReference .sourceStart );
1780
- annotation .memberValuePairs = createAnnotationMemberValuePairs (memberValuePairs );
1781
- annotations .add (annotation );
1782
- }
1783
- // TODO: declarationSourceEnd should be rparen position; antlr2 includes any trailing comment
1784
- annotations .get (annotations .size () - 1 ).declarationSourceEnd = annotationReference .sourceEnd ;
1772
+ Map <String , Expression > memberValuePairs = annotationNode .getMembers ();
1773
+ if (memberValuePairs == null || memberValuePairs .isEmpty ()) {
1774
+ MarkerAnnotation annotation = new MarkerAnnotation (annotationType , annotationType .sourceStart );
1775
+ annotations [i ] = annotation ;
1776
+ } else if (memberValuePairs .size () == 1 && memberValuePairs .containsKey ("value" )) {
1777
+ SingleMemberAnnotation annotation = new SingleMemberAnnotation (annotationType , annotationType .sourceStart );
1778
+ annotation .memberValue = createAnnotationMemberExpression (memberValuePairs .get ("value" ), null );
1779
+ annotations [i ] = annotation ;
1780
+ } else {
1781
+ NormalAnnotation annotation = new NormalAnnotation (annotationType , annotationType .sourceStart );
1782
+ annotation .memberValuePairs = createAnnotationMemberValuePairs (memberValuePairs );
1783
+ annotations [i ] = annotation ;
1785
1784
}
1786
-
1787
- return annotations . toArray ( new Annotation [ annotations . size ()]) ;
1785
+ // TODO: declarationSourceEnd should be rparen position; antlr2 includes trailing comment
1786
+ annotations [ i ++]. declarationSourceEnd = annotationType . sourceEnd ;
1788
1787
}
1789
- return null ;
1788
+ return annotations ;
1790
1789
}
1791
1790
1792
1791
private org .eclipse .jdt .internal .compiler .ast .Expression createAnnotationMemberExpression (Expression expr , ClassNode type ) {
0 commit comments