|
37 | 37 | import javax.annotation.processing.SupportedOptions; |
38 | 38 | import javax.lang.model.SourceVersion; |
39 | 39 | import javax.lang.model.element.Element; |
| 40 | +import javax.lang.model.element.ElementKind; |
| 41 | +import javax.lang.model.element.ExecutableElement; |
40 | 42 | import javax.lang.model.element.Modifier; |
41 | 43 | import javax.lang.model.element.Name; |
42 | 44 | import javax.lang.model.element.TypeElement; |
@@ -138,10 +140,41 @@ public Name visitType(TypeElement typeElement, Void aVoid) { |
138 | 140 | } else if ((forTarget & SearchIndexable.ARC) != 0) { |
139 | 141 | builder = arcConstructorBuilder; |
140 | 142 | } |
141 | | - builder.addCode( |
142 | | - "$N(new com.android.settingslib.search.SearchIndexableData($L.class, $L" |
143 | | - + ".SEARCH_INDEX_DATA_PROVIDER));\n", |
144 | | - addIndex, className, className); |
| 143 | + boolean hasNoArgConstructor = false; |
| 144 | + for (Element enclosed : ((TypeElement) element).getEnclosedElements()) { |
| 145 | + if (enclosed.getKind() == ElementKind.CONSTRUCTOR && |
| 146 | + ((ExecutableElement) enclosed).getParameters().isEmpty()) { |
| 147 | + hasNoArgConstructor = true; |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + builder.beginControlFlow("try") |
| 153 | + .addCode( |
| 154 | + "$N(new com.android.settingslib.search.SearchIndexableData($L.class, " + |
| 155 | + "$L.SEARCH_INDEX_DATA_PROVIDER));\n", |
| 156 | + addIndex, className, className) |
| 157 | + .nextControlFlow("catch (Throwable e1)"); |
| 158 | + |
| 159 | + if (hasNoArgConstructor) { |
| 160 | + builder.beginControlFlow("try") |
| 161 | + .addStatement("Object obj = new $L()", className) |
| 162 | + .beginControlFlow("if (obj instanceof $T)", |
| 163 | + ClassName.get("com.android.settings.search", "SearchIndexProviderHolder")) |
| 164 | + .addStatement( |
| 165 | + "$N(new com.android.settingslib.search.SearchIndexableData($L.class, " + |
| 166 | + "(($T) obj).getSearchIndexProvider()))", |
| 167 | + addIndex, className, |
| 168 | + ClassName.get("com.android.settings.search", "SearchIndexProviderHolder")) |
| 169 | + .endControlFlow() |
| 170 | + .nextControlFlow("catch (Throwable e2)") |
| 171 | + .addComment("// No-arg instantiation failed or not a SearchIndexProviderHolder.") |
| 172 | + .endControlFlow(); |
| 173 | + } else { |
| 174 | + builder.addComment("// Skipped instantiation: $L has no no-arg constructor", className); |
| 175 | + } |
| 176 | + |
| 177 | + builder.endControlFlow(); |
145 | 178 | } else { |
146 | 179 | throw new IllegalStateException("Null classname from " + element); |
147 | 180 | } |
|
0 commit comments