package org.simantics.scl.compiler.constants.generic; import java.util.ArrayList; import org.cojen.classfile.TypeDesc; import org.objectweb.asm.Label; import org.simantics.scl.compiler.internal.codegen.utils.Constants; import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder; public enum ConvertToListFilter implements OutputFilter { INSTANCE; private static final TypeDesc ARRAY_LIST = TypeDesc.forClass(ArrayList.class); private static final TypeDesc[] CONVERSION_PARAMS = new TypeDesc[] {Constants.COLLECTION}; @Override public void filter(MethodBuilder mb) { Label conversionNeeded = mb.createLabel(); Label endFilter = mb.createLabel(); mb.dup(); mb.instanceOf(Constants.LIST); mb.ifZeroComparisonBranch(conversionNeeded, "=="); // no conversion needed mb.checkCast(Constants.LIST); mb.branch(endFilter); // conversion needed mb.setLocation(conversionNeeded); mb.newObject(ARRAY_LIST); mb.dupX1(); mb.swap(); mb.invokeConstructor(ARRAY_LIST, CONVERSION_PARAMS); // end mb.setLocation(endFilter); } }