@@ -81,6 +81,7 @@ public class JavaClass implements HasName.AndFullName, HasAnnotations, HasModifi
81
81
private final Set <JavaClass > interfaces = new HashSet <>();
82
82
private final Set <JavaClass > subClasses = new HashSet <>();
83
83
private Optional <JavaClass > enclosingClass = Optional .absent ();
84
+ private Optional <JavaClass > componentType = Optional .absent ();
84
85
private Supplier <Map <String , JavaAnnotation >> annotations =
85
86
Suppliers .ofInstance (Collections .<String , JavaAnnotation >emptyMap ());
86
87
private Supplier <Set <JavaMethod >> allMethods ;
@@ -186,6 +187,30 @@ public boolean isArray() {
186
187
return javaType .isArray ();
187
188
}
188
189
190
+ /**
191
+ * This is a convenience method for {@link #tryGetComponentType()} in cases where
192
+ * clients know that this type is certainly an array type and thus the component type present.
193
+ * @throws IllegalArgumentException if this class is no array
194
+ * @return The result of {@link #tryGetComponentType()}
195
+ */
196
+ @ PublicAPI (usage = ACCESS )
197
+ public JavaClass getComponentType () {
198
+ return tryGetComponentType ().getOrThrow (new IllegalArgumentException (
199
+ String .format ("Type %s is no array" , getSimpleName ())));
200
+ }
201
+
202
+ /**
203
+ * Returns the component type of this class, if this class is an array, otherwise
204
+ * {@link Optional#absent()}. The component type is the type of the elements of an array type.
205
+ * Consider {@code String[]}, then the component type would be {@code String}.
206
+ * Likewise for {@code String[][]} the component type would be {@code String[]}.
207
+ * @return The component type, if this type is an array, otherwise {@link Optional#absent()}
208
+ */
209
+ @ PublicAPI (usage = ACCESS )
210
+ Optional <JavaClass > tryGetComponentType () {
211
+ return componentType ;
212
+ }
213
+
189
214
/**
190
215
* @return Returns true if this class is declared within another class.
191
216
* Returns false for top-level classes.
@@ -841,6 +866,7 @@ public Map<String, JavaAnnotation> get() {
841
866
}
842
867
843
868
CompletionProcess completeFrom (ImportContext context ) {
869
+ completeComponentType (context );
844
870
enclosingClass = context .createEnclosingClass (this );
845
871
memberDependenciesOnClass = new MemberDependenciesOnClass (
846
872
context .getFieldsOfType (this ),
@@ -852,6 +878,15 @@ CompletionProcess completeFrom(ImportContext context) {
852
878
return new CompletionProcess ();
853
879
}
854
880
881
+ private void completeComponentType (ImportContext context ) {
882
+ JavaClass current = this ;
883
+ while (current .isArray () && !current .componentType .isPresent ()) {
884
+ JavaClass componentType = context .resolveClass (current .javaType .tryGetComponentType ().get ().getName ());
885
+ current .componentType = Optional .of (componentType );
886
+ current = componentType ;
887
+ }
888
+ }
889
+
855
890
@ Override
856
891
public String toString () {
857
892
return "JavaClass{name='" + javaType .getName () + "\' }" ;
0 commit comments