|
1 | 1 | package com.dreamgyf.android.plugin.fastinflate.plugin |
2 | 2 |
|
3 | 3 | import com.squareup.kotlinpoet.* |
| 4 | +import com.squareup.kotlinpoet.jvm.throws |
4 | 5 | import groovy.util.Node |
5 | 6 | import groovy.xml.XmlParser |
6 | 7 | import java.io.File |
@@ -96,38 +97,48 @@ object FastInflateLayoutGenerator { |
96 | 97 | ) |
97 | 98 | .addStatement("val res = context.resources") |
98 | 99 | .addStatement("val parser = res.getLayout(resource)") |
99 | | - .addStatement("try {") |
| 100 | + .beginControlFlow("try") |
100 | 101 | .addStatement("val attrs = %T.asAttributeSet(parser)", xmlClz) |
101 | 102 | .addStatement("var result: %T = root", viewClz.copy(nullable = true)) |
102 | 103 | .addStatement("%T.advanceToRootNode(parser)", helperClz) |
103 | 104 |
|
104 | 105 | val rootNodeName = root.name().toString() |
105 | 106 | if (rootNodeName == TAG_MERGE) { |
106 | 107 | inflateFunBuilder |
| 108 | + .beginControlFlow("if (root == null || !attachToRoot)") |
107 | 109 | .addStatement( |
108 | | - "if (root == null || !attachToRoot) { throw %T(\"%L\") }", |
| 110 | + "throw %T(%S)", |
109 | 111 | fastInflateExceptionClz, |
110 | | - "<merge·/>·can·be·used·only·with·a·valid·ViewGroup·root·and·attachToRoot=true" |
| 112 | + "<merge /> can be used only with a valid ViewGroup root and attachToRoot=true" |
111 | 113 | ) |
| 114 | + .endControlFlow() |
112 | 115 | .addStatement("inflateChildren(parser, root, context, attrs)") |
113 | 116 | } else { |
114 | 117 | inflateFunBuilder.addStatement( |
115 | 118 | "val temp = %L(root, context, attrs, false)", |
116 | 119 | genCreateViewFunc(root) |
117 | 120 | ) |
118 | 121 | .addStatement("var params: %T.LayoutParams? = null", viewGroupClz) |
119 | | - .addStatement("if (root != null) {") |
| 122 | + .beginControlFlow("if (root != null)") |
120 | 123 | .addStatement("params = root.generateLayoutParams(attrs)") |
121 | | - .addStatement("if (!attachToRoot) { temp.layoutParams = params }") |
122 | | - .addStatement("}") |
| 124 | + .beginControlFlow("if (!attachToRoot)") |
| 125 | + .addStatement("temp.layoutParams = params") |
| 126 | + .endControlFlow() |
| 127 | + .endControlFlow() |
123 | 128 | .addStatement("inflateChildren(parser, temp, context, attrs)") |
124 | | - .addStatement("if (root != null && attachToRoot) { root.addView(temp, params) }") |
125 | | - .addStatement("if (root == null || !attachToRoot) { result = temp }") |
| 129 | + .beginControlFlow("if (root != null && attachToRoot)") |
| 130 | + .addStatement("root.addView(temp, params)") |
| 131 | + .endControlFlow() |
| 132 | + .beginControlFlow("if (root == null || !attachToRoot)") |
| 133 | + .addStatement("result = temp") |
| 134 | + .endControlFlow() |
126 | 135 | } |
127 | 136 |
|
128 | 137 | inflateFunBuilder |
129 | 138 | .addStatement("return result!!") |
130 | | - .addStatement("} finally { parser.close() }") |
| 139 | + .nextControlFlow("finally") |
| 140 | + .addStatement("parser.close()") |
| 141 | + .endControlFlow() |
131 | 142 |
|
132 | 143 | return inflateFunBuilder.build() |
133 | 144 | } |
@@ -200,8 +211,9 @@ object FastInflateLayoutGenerator { |
200 | 211 | .addStatement("%T.consumeChildElements(parser)", helperClz) |
201 | 212 | } else if (nodeName == TAG_MERGE) { |
202 | 213 | funSpecBuilder.addStatement( |
203 | | - "throw %T(\"<merge /> must be the root element\")", |
204 | | - fastInflateExceptionClz |
| 214 | + "throw %T(%S)", |
| 215 | + fastInflateExceptionClz, |
| 216 | + "<merge /> must be the root element" |
205 | 217 | ) |
206 | 218 | } else { |
207 | 219 | funSpecBuilder.addStatement( |
@@ -265,39 +277,42 @@ object FastInflateLayoutGenerator { |
265 | 277 | .addParameter("ignoreThemeAttr", Boolean::class.java) |
266 | 278 | .returns(viewClz) |
267 | 279 | .addStatement("var ctx = context") |
268 | | - .addStatement("if (!ignoreThemeAttr) {") |
| 280 | + .beginControlFlow("if (!ignoreThemeAttr)") |
269 | 281 | .addStatement("val ta = context.obtainStyledAttributes(attrs, ATTRS_THEME)") |
270 | 282 | .addStatement("val themeResId = ta.getResourceId(0, 0)") |
271 | | - .addStatement( |
272 | | - "if (themeResId != 0) { ctx = %T(context, themeResId) }", |
273 | | - contextThemeWrapperClz |
274 | | - ) |
275 | | - .addStatement("ta.recycle() }") |
276 | | - |
| 283 | + .beginControlFlow("if (themeResId != 0)") |
| 284 | + .addStatement("ctx = %T(context, themeResId)", contextThemeWrapperClz) |
| 285 | + .endControlFlow() |
| 286 | + .addStatement("ta.recycle()") |
| 287 | + .endControlFlow() |
277 | 288 | if (nodeName == TAG_1995) { |
278 | 289 | funSpecBuilder.addStatement("return %T.newBlinkLayout(ctx, attrs)", helperClz) |
279 | 290 | } else { |
280 | 291 | funSpecBuilder |
281 | 292 | .addStatement("var view: %T? = null", viewClz) |
282 | | - .addStatement("if (layoutInflater.factory2 != null) {") |
| 293 | + .beginControlFlow("if (layoutInflater.factory2 != null)") |
283 | 294 | .addStatement( |
284 | | - "view = layoutInflater.factory2.onCreateView(parent, \"%L\", ctx, attrs)", |
| 295 | + "view = layoutInflater.factory2.onCreateView(parent, %S, ctx, attrs)", |
285 | 296 | className |
286 | 297 | ) |
287 | | - .addStatement("} else if (layoutInflater.factory != null) {") |
| 298 | + .nextControlFlow("else if (layoutInflater.factory != null)") |
288 | 299 | .addStatement( |
289 | | - "view = layoutInflater.factory.onCreateView(\"%L\", ctx, attrs)", |
| 300 | + "view = layoutInflater.factory.onCreateView(%S, ctx, attrs)", |
290 | 301 | className |
291 | 302 | ) |
292 | | - .addStatement("}") |
| 303 | + .endControlFlow() |
| 304 | + .beginControlFlow("if (view == null)") |
293 | 305 | .addStatement( |
294 | | - "if (view == null) { view = privateFactory?.onCreateView(\"%L\", ctx, attrs) }", |
| 306 | + "view = privateFactory?.onCreateView(%S, ctx, attrs)", |
295 | 307 | className |
296 | 308 | ) |
297 | | - .addStatement("if (view == null) { view = $fixedClassName(ctx, attrs) }") |
| 309 | + .endControlFlow() |
| 310 | + .beginControlFlow("if (view == null)") |
| 311 | + .addStatement("view = $fixedClassName(ctx, attrs)") |
| 312 | + .endControlFlow() |
298 | 313 | .apply { |
299 | | - if (className == "android.view.ViewStub") { |
300 | | - addStatement("view.layoutInflater = LayoutInflater.from(context)") |
| 314 | + if (fixedClassName == "android.view.ViewStub") { |
| 315 | + addStatement("(view as? android.view.ViewStub?)?.layoutInflater = layoutInflater.cloneInContext(context)") |
301 | 316 | } |
302 | 317 | } |
303 | 318 | .addStatement("return view") |
|
0 commit comments