Distinguish captured field from general one
This commit is contained in:
@@ -41,8 +41,10 @@ public class FieldRemapper {
|
|||||||
params = methodParams;
|
params = methodParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canProcess(@NotNull String fieldOwner, boolean isFolding) {
|
protected boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) {
|
||||||
return fieldOwner.equals(getLambdaInternalName());
|
return fieldOwner.equals(getLambdaInternalName()) &&
|
||||||
|
//don't process general field of anonymous objects
|
||||||
|
InlineCodegenUtil.isCapturedFieldName(fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -73,7 +75,7 @@ public class FieldRemapper {
|
|||||||
if (transformed == null) {
|
if (transformed == null) {
|
||||||
//if parent couldn't transform
|
//if parent couldn't transform
|
||||||
FieldInsnNode insnNode = (FieldInsnNode) capturedFieldAccess.get(currentInstruction);
|
FieldInsnNode insnNode = (FieldInsnNode) capturedFieldAccess.get(currentInstruction);
|
||||||
if (canProcess(insnNode.owner, true)) {
|
if (canProcess(insnNode.owner, insnNode.name, true)) {
|
||||||
insnNode.name = "$$$" + insnNode.name;
|
insnNode.name = "$$$" + insnNode.name;
|
||||||
insnNode.setOpcode(Opcodes.GETSTATIC);
|
insnNode.setOpcode(Opcodes.GETSTATIC);
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,12 @@ public class InlineCodegenUtil {
|
|||||||
public static final String INVOKE = "invoke";
|
public static final String INVOKE = "invoke";
|
||||||
public static final boolean DEFAULT_INLINE_FLAG = true;
|
public static final boolean DEFAULT_INLINE_FLAG = true;
|
||||||
|
|
||||||
|
public static final String CAPTURED_FIELD_PREFIX = "$";
|
||||||
|
|
||||||
|
public static final String THIS$0 = "this$0";
|
||||||
|
|
||||||
|
public static final String RECEIVER$0 = "receiver$0";
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static MethodNode getMethodNode(
|
public static MethodNode getMethodNode(
|
||||||
InputStream classData,
|
InputStream classData,
|
||||||
@@ -259,4 +265,8 @@ public class InlineCodegenUtil {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isCapturedFieldName(@NotNull String fieldName) {
|
||||||
|
return fieldName.startsWith(CAPTURED_FIELD_PREFIX) || THIS$0.equals(fieldName) || RECEIVER$0.equals(fieldName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ public class InlinedLambdaRemapper extends FieldRemapper {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canProcess(@NotNull String fieldOwner, boolean isFolding) {
|
public boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) {
|
||||||
return isFolding ? super.canProcess(fieldOwner, isFolding) : false;
|
return isFolding ? super.canProcess(fieldOwner, fieldName, isFolding) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ public class LambdaTransformer {
|
|||||||
AbstractInsnNode cur = constructor.instructions.getFirst();
|
AbstractInsnNode cur = constructor.instructions.getFirst();
|
||||||
//load captured parameters (NB: there is also could be object fields)
|
//load captured parameters (NB: there is also could be object fields)
|
||||||
while (cur != null) {
|
while (cur != null) {
|
||||||
if (cur.getType() == AbstractInsnNode.FIELD_INSN) {
|
if (cur instanceof FieldInsnNode && cur.getOpcode() == Opcodes.PUTFIELD && InlineCodegenUtil.isCapturedFieldName(((FieldInsnNode) cur).name)) {
|
||||||
FieldInsnNode fieldNode = (FieldInsnNode) cur;
|
FieldInsnNode fieldNode = (FieldInsnNode) cur;
|
||||||
CapturedParamInfo info = builder.addCapturedParam(owner, fieldNode.name, Type.getType(fieldNode.desc), false, null);
|
CapturedParamInfo info = builder.addCapturedParam(owner, fieldNode.name, Type.getType(fieldNode.desc), false, null);
|
||||||
|
|
||||||
@@ -290,7 +290,7 @@ public class LambdaTransformer {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getNewFieldName(@NotNull String oldName) {
|
public String getNewFieldName(@NotNull String oldName) {
|
||||||
if (oldName.equals("this$0")) {
|
if (InlineCodegenUtil.THIS$0.equals(oldName)) {
|
||||||
//"this$0" couldn't clash and we should keep this name invariant for further transformations
|
//"this$0" couldn't clash and we should keep this name invariant for further transformations
|
||||||
return oldName;
|
return oldName;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -51,8 +51,8 @@ public class RegeneratedLambdaFieldRemapper extends FieldRemapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canProcess(@NotNull String fieldOwner, boolean isFolding) {
|
public boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) {
|
||||||
return super.canProcess(fieldOwner, isFolding) || isRecapturedLambdaType(fieldOwner);
|
return super.canProcess(fieldOwner, fieldName, isFolding) || isRecapturedLambdaType(fieldOwner);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRecapturedLambdaType(String owner) {
|
private boolean isRecapturedLambdaType(String owner) {
|
||||||
@@ -62,7 +62,7 @@ public class RegeneratedLambdaFieldRemapper extends FieldRemapper {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) {
|
public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) {
|
||||||
boolean searchInParent = !canProcess(fieldInsnNode.owner, false);
|
boolean searchInParent = !canProcess(fieldInsnNode.owner, fieldInsnNode.name, false);
|
||||||
if (searchInParent) {
|
if (searchInParent) {
|
||||||
return parent.findField(fieldInsnNode);
|
return parent.findField(fieldInsnNode);
|
||||||
} else {
|
} else {
|
||||||
@@ -87,7 +87,7 @@ public class RegeneratedLambdaFieldRemapper extends FieldRemapper {
|
|||||||
field = findFieldInMyCaptured(new FieldInsnNode(Opcodes.GETSTATIC, oldOwnerType, "this$0", Type.getObjectType(parent.getLambdaInternalName()).getDescriptor()));
|
field = findFieldInMyCaptured(new FieldInsnNode(Opcodes.GETSTATIC, oldOwnerType, "this$0", Type.getObjectType(parent.getLambdaInternalName()).getDescriptor()));
|
||||||
searchInParent = true;
|
searchInParent = true;
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
throw new IllegalStateException("Could find captured this " + getLambdaInternalName());
|
throw new IllegalStateException("Couldn't find captured this " + getLambdaInternalName() + " for " + node.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user