Skip to content

Commit

Permalink
feat: get layoutParams with generateLayoutParams
Browse files Browse the repository at this point in the history
Signed-off-by: Haruue Icymoon <[email protected]>
  • Loading branch information
haruue committed Apr 6, 2018
1 parent 8e4a91f commit dcfd9c3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.view.ViewGroup.LayoutParams.*;

/**
* @author Haruue Icymoon [email protected]
Expand Down Expand Up @@ -38,7 +39,9 @@ private ActivityJavaLayout(Context context) {
// get these two guys from attrs first
// construct parent layout's LayoutParams Class, get its type from stack.peek().type
// android:layout_width="wrap_content" android:layout_height="wrap_content"
LinearLayout.LayoutParams text1Param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams text1Param = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
text1Param.width = WRAP_CONTENT;
text1Param.height = WRAP_CONTENT;
// than ignore the id, layout_width and layout_height, find other thing...
// begin with layout --> param.xxxxxx
// android:layout_gravity="start"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package moe.haruue.layoutparser.compiler

import com.squareup.javapoet.*
import moe.haruue.layoutparser.compiler.tools.cnActivity
import moe.haruue.layoutparser.compiler.tools.cnContext
import moe.haruue.layoutparser.compiler.tools.toClassName
import moe.haruue.layoutparser.compiler.tools.underlineToUpperCamel
import moe.haruue.layoutparser.compiler.tools.*
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParser.*
import java.util.*
Expand Down Expand Up @@ -45,7 +42,12 @@ class LayoutParser(
val methods = mutableListOf<MethodSpec>()
val fields = mutableListOf<FieldSpec>()

private data class ViewType(val id: String, val type: String)
private data class ViewType(val id: String, val type: String) {
val layoutParamsClass
get() = type.toClass()
.getMethodRecursive("generateLayoutParams", cAttributeSet)
.returnType
}
private data class Dimension(val value: Int, val unit: String)
private val stack = Stack<ViewType>()
private var currentNoId = 0
Expand Down Expand Up @@ -134,13 +136,14 @@ class LayoutParser(

val parentClassName = parent.type.toClassName()
val layoutParamsName = layoutParamsNameOf(name)
val layoutParamsType = parent.layoutParamsClass

constructorBuilder.addStatement("""
this.$name = new $T(context)
""".trimIndent(), typeClassName)
constructorBuilder.addStatement("""
$T.LayoutParams $layoutParamsName = new $T.LayoutParams(${width.value}, ${height.value})
""".trimIndent(), parentClassName, parentClassName)
$T $layoutParamsName = new $T(${width.value}, ${height.value})
""".trimIndent(), layoutParamsType, layoutParamsType)

// parse other attrs here...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ import com.squareup.javapoet.ClassName
*
* @author Haruue Icymoon [email protected]
*/
fun String.toClass(): Class<*> = Class.forName(this)
fun ClassName.toClass(): Class<*> = reflectionName().toClass()
fun Class<*>.toClassName(): ClassName = ClassName.get(this)
fun String.toClassName(): ClassName = toClass().toClassName()

val cnContext = ClassName.get("android.content", "Context")
val cnActivity = ClassName.get("android.app", "Activity")
val cnActivity = ClassName.get("android.app", "Activity")
val cAttributeSet = Class.forName("android.util.AttributeSet")


Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package moe.haruue.layoutparser.compiler.tools

import java.lang.reflect.Method

/**
*
* @author Haruue Icymoon [email protected]
*/
fun Class<*>.getMethodRecursive(name: String, vararg parameterTypes: Class<*>): Method {
var clazz: Class<*>? = this
while (clazz != null) {
try {
return getMethod(name, *parameterTypes)
} catch (e: NoSuchMethodException) {
clazz = superclass
}
}
throw NoSuchMethodException(
"no such " +
"method $name(${parameterTypes.joinToString(separator = ",")}) " +
"in ${this.name}")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package moe.haruue.layoutparser.compiler.tools

import com.squareup.javapoet.ClassName

/**
*
* @author Haruue Icymoon [email protected]
Expand All @@ -26,8 +24,6 @@ fun String.parseSimpleClassName(): String {
return simpleName
}

fun String.toClassName(): ClassName = ClassName.get(parsePackageName(), parseSimpleClassName())

fun String.underlineToUpperCamel(): String {
val words = split("_")
return words.joinToString(separator = "") { it.upperCaseFirst() }
Expand Down

0 comments on commit dcfd9c3

Please sign in to comment.