18
18
import java .util .stream .Collectors ;
19
19
import java .util .stream .Stream ;
20
20
21
+ import org .eclipse .jdt .core .dom .AbstractTypeDeclaration ;
21
22
import org .eclipse .jdt .core .dom .Annotation ;
22
23
import org .eclipse .jdt .core .dom .FieldDeclaration ;
23
24
import org .eclipse .jdt .core .dom .ITypeBinding ;
25
+ import org .eclipse .jdt .core .dom .RecordDeclaration ;
24
26
import org .eclipse .jdt .core .dom .SimpleName ;
27
+ import org .eclipse .jdt .core .dom .SingleVariableDeclaration ;
25
28
import org .eclipse .jdt .core .dom .Type ;
26
29
import org .eclipse .jdt .core .dom .TypeDeclaration ;
27
30
import org .eclipse .jdt .core .dom .VariableDeclarationFragment ;
@@ -55,44 +58,44 @@ public class ConfigurationPropertiesSymbolProvider implements SymbolProvider {
55
58
@ Override
56
59
public void addSymbols (Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , SpringIndexerJavaContext context , TextDocument doc ) {
57
60
try {
58
- if (node != null && node .getParent () != null && node .getParent () instanceof TypeDeclaration ) {
59
- createSymbol (node , annotationType , metaAnnotations , context , doc );
61
+ if (node != null && node .getParent () != null ) {
62
+ if (node .getParent () instanceof AbstractTypeDeclaration abstractType ) {
63
+ createSymbolForType (abstractType , node , annotationType , metaAnnotations , context , doc );
64
+ }
60
65
}
61
66
}
62
67
catch (BadLocationException e ) {
63
68
log .error ("" , e );
64
69
}
65
70
}
66
71
67
- protected void createSymbol ( Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , SpringIndexerJavaContext context , TextDocument doc ) throws BadLocationException {
72
+ protected void createSymbolForType ( AbstractTypeDeclaration type , Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , SpringIndexerJavaContext context , TextDocument doc ) throws BadLocationException {
68
73
String annotationTypeName = annotationType .getName ();
69
74
70
75
Collection <String > metaAnnotationNames = metaAnnotations .stream ()
71
76
.map (ITypeBinding ::getName )
72
77
.collect (Collectors .toList ());
73
78
74
- TypeDeclaration type = (TypeDeclaration ) node .getParent ();
75
79
ITypeBinding typeBinding = type .resolveBinding ();
76
80
77
81
AnnotationHierarchies annotationHierarchies = AnnotationHierarchies .get (type );
78
82
boolean isComponentAnnotated = annotationHierarchies .isAnnotatedWith (typeBinding , Annotations .COMPONENT );
79
83
80
84
if (!isComponentAnnotated ) {
81
85
String beanName = BeanUtils .getBeanNameFromType (type .getName ().getFullyQualifiedName ());
82
- ITypeBinding beanType = type .resolveBinding ();
83
86
84
87
Location location = new Location (doc .getUri (), doc .toRange (type .getStartPosition (), type .getLength ()));
85
88
86
89
WorkspaceSymbol symbol = new WorkspaceSymbol (
87
- ComponentSymbolProvider .beanLabel ("+" , annotationTypeName , metaAnnotationNames , beanName , beanType .getName ()), SymbolKind .Interface ,
90
+ ComponentSymbolProvider .beanLabel ("+" , annotationTypeName , metaAnnotationNames , beanName , typeBinding .getName ()), SymbolKind .Interface ,
88
91
Either .forLeft (location ));
89
92
90
93
boolean isConfiguration = false ; // otherwise, the ComponentSymbolProvider takes care of the bean definiton for this type
91
94
92
95
InjectionPoint [] injectionPoints = ASTUtils .findInjectionPoints (type , doc );
93
96
94
97
Set <String > supertypes = new HashSet <>();
95
- ASTUtils .findSupertypes (beanType , supertypes );
98
+ ASTUtils .findSupertypes (typeBinding , supertypes );
96
99
97
100
Collection <Annotation > annotationsOnType = ASTUtils .getAnnotations (type );
98
101
@@ -103,7 +106,7 @@ protected void createSymbol(Annotation node, ITypeBinding annotationType, Collec
103
106
.map (an -> new AnnotationMetadata (an .getQualifiedName (), true , null , null )))
104
107
.toArray (AnnotationMetadata []::new );
105
108
106
- Bean beanDefinition = new Bean (beanName , beanType .getQualifiedName (), location , injectionPoints , supertypes , annotations , isConfiguration , symbol .getName ());
109
+ Bean beanDefinition = new Bean (beanName , typeBinding .getQualifiedName (), location , injectionPoints , supertypes , annotations , isConfiguration , symbol .getName ());
107
110
108
111
indexConfigurationProperties (beanDefinition , type , context , doc );
109
112
@@ -112,7 +115,16 @@ protected void createSymbol(Annotation node, ITypeBinding annotationType, Collec
112
115
}
113
116
}
114
117
115
- public static void indexConfigurationProperties (Bean beanDefinition , TypeDeclaration type , SpringIndexerJavaContext context , TextDocument doc ) {
118
+ public static void indexConfigurationProperties (Bean beanDefinition , AbstractTypeDeclaration abstractType , SpringIndexerJavaContext context , TextDocument doc ) {
119
+ if (abstractType instanceof TypeDeclaration type ) {
120
+ indexConfigurationPropertiesForType (beanDefinition , type , context , doc );
121
+ }
122
+ else if (abstractType instanceof RecordDeclaration record ) {
123
+ indexConfigurationPropertiesForRecord (beanDefinition , record , context , doc );
124
+ }
125
+ }
126
+
127
+ public static void indexConfigurationPropertiesForType (Bean beanDefinition , TypeDeclaration type , SpringIndexerJavaContext context , TextDocument doc ) {
116
128
117
129
FieldDeclaration [] fields = type .getFields ();
118
130
if (fields != null ) {
@@ -145,4 +157,32 @@ public static void indexConfigurationProperties(Bean beanDefinition, TypeDeclara
145
157
146
158
}
147
159
160
+ public static void indexConfigurationPropertiesForRecord (Bean beanDefinition , RecordDeclaration record , SpringIndexerJavaContext context , TextDocument doc ) {
161
+
162
+ @ SuppressWarnings ("unchecked" )
163
+ List <SingleVariableDeclaration > fields = record .recordComponents ();
164
+
165
+ if (fields != null ) {
166
+ for (SingleVariableDeclaration field : fields ) {
167
+ try {
168
+ Type fieldType = field .getType ();
169
+ if (fieldType != null ) {
170
+
171
+ SimpleName name = field .getName ();
172
+ if (name != null ) {
173
+
174
+ DocumentRegion nodeRegion = ASTUtils .nodeRegion (doc , field );
175
+ Range range = doc .toRange (nodeRegion );
176
+ ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement (name .getFullyQualifiedName (), fieldType .resolveBinding ().getQualifiedName (), range );
177
+
178
+ beanDefinition .addChild (configPropElement );
179
+ }
180
+ }
181
+ } catch (BadLocationException e ) {
182
+ log .error ("error identifying config property field" , e );
183
+ }
184
+ }
185
+ }
186
+
187
+ }
148
188
}
0 commit comments