Clean code

This commit is contained in:
Mikhael Bogdanov
2017-08-04 14:50:52 +02:00
parent e2dcf47b3b
commit fca1500d48
@@ -253,18 +253,15 @@ class AnonymousObjectTransformer(
val newFieldsWithSkipped = getNewFieldsToGenerate(allCapturedBuilder.listCaptured()) val newFieldsWithSkipped = getNewFieldsToGenerate(allCapturedBuilder.listCaptured())
val fieldInfoWithSkipped = transformToFieldInfo(Type.getObjectType(transformationInfo.newClassName), newFieldsWithSkipped) val fieldInfoWithSkipped = transformToFieldInfo(Type.getObjectType(transformationInfo.newClassName), newFieldsWithSkipped)
var paramIndex = 0
val capturedFieldInitializer = InstructionAdapter(constructorVisitor) val capturedFieldInitializer = InstructionAdapter(constructorVisitor)
for (i in fieldInfoWithSkipped.indices) { fieldInfoWithSkipped.forEachIndexed { paramIndex, fieldInfo ->
val fieldInfo = fieldInfoWithSkipped[i] if (!newFieldsWithSkipped[paramIndex].skip) {
if (!newFieldsWithSkipped[i].skip) {
AsmUtil.genAssignInstanceFieldFromParam(fieldInfo, capturedIndexes[paramIndex], capturedFieldInitializer) AsmUtil.genAssignInstanceFieldFromParam(fieldInfo, capturedIndexes[paramIndex], capturedFieldInitializer)
} }
paramIndex++
} }
//then transform constructor //then transform constructor
//HACK: in inlinining into constructor we access original captured fields with field access not local var //HACK: in inlining into constructor we access original captured fields with field access not local var
//but this fields added to general params (this assumes local var access) not captured one, //but this fields added to general params (this assumes local var access) not captured one,
//so we need to add them to captured params //so we need to add them to captured params
for (info in constructorAdditionalFakeParams) { for (info in constructorAdditionalFakeParams) {
@@ -288,7 +285,7 @@ class AnonymousObjectTransformer(
removeFinallyMarkers(intermediateMethodNode) removeFinallyMarkers(intermediateMethodNode)
val first = intermediateMethodNode.instructions.first val first = intermediateMethodNode.instructions.first
val oldStartLabel = if (first is LabelNode) first.label else null val oldStartLabel = (first as? LabelNode)?.label
intermediateMethodNode.accept(object : MethodBodyVisitor(capturedFieldInitializer) { intermediateMethodNode.accept(object : MethodBodyVisitor(capturedFieldInitializer) {
override fun visitLocalVariable( override fun visitLocalVariable(
name: String, desc: String, signature: String?, start: Label, end: Label, index: Int name: String, desc: String, signature: String?, start: Label, end: Label, index: Int
@@ -444,16 +441,10 @@ class AnonymousObjectTransformer(
} }
private fun shouldRenameThis0(parentFieldRemapper: FieldRemapper, values: Collection<LambdaInfo>): Boolean { private fun shouldRenameThis0(parentFieldRemapper: FieldRemapper, values: Collection<LambdaInfo>): Boolean {
if (isFirstDeclSiteLambdaFieldRemapper(parentFieldRemapper)) { return if (isFirstDeclSiteLambdaFieldRemapper(parentFieldRemapper)) {
for (value in values) { values.any { it.capturedVars.any { isThis0(it.fieldName) }}
for (desc in value.capturedVars) {
if (isThis0(desc.fieldName)) {
return true
}
}
}
} }
return false else false
} }
private fun getNewFieldName(oldName: String, originalField: Boolean): String { private fun getNewFieldName(oldName: String, originalField: Boolean): String {
@@ -470,14 +461,13 @@ class AnonymousObjectTransformer(
} }
private fun addUniqueField(name: String): String { private fun addUniqueField(name: String): String {
val existNames = fieldNames.getOrPut(name) { LinkedList<String>() } val existNames = fieldNames.getOrPut(name) { LinkedList() }
val suffix = if (existNames.isEmpty()) "" else "$" + existNames.size val suffix = if (existNames.isEmpty()) "" else "$" + existNames.size
val newName = name + suffix val newName = name + suffix
existNames.add(newName) existNames.add(newName)
return newName return newName
} }
private fun isFirstDeclSiteLambdaFieldRemapper(parentRemapper: FieldRemapper): Boolean { private fun isFirstDeclSiteLambdaFieldRemapper(parentRemapper: FieldRemapper): Boolean =
return parentRemapper !is RegeneratedLambdaFieldRemapper && parentRemapper !is InlinedLambdaRemapper parentRemapper !is RegeneratedLambdaFieldRemapper && parentRemapper !is InlinedLambdaRemapper
}
} }