Java Code Examples for org.objectweb.asm.MethodVisitor#visitLookupSwitchInsn()

The following examples show how to use org.objectweb.asm.MethodVisitor#visitLookupSwitchInsn() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void toCode(MethodVisitor visitor, BytecodeFrontend assembler) {
	if (needsSort()) {
		sort();
	}

	int[] cases = new int[targets.size()];
	Label[] labels = new Label[targets.size()];
	int j = 0;
	for (Entry<Integer, BasicBlock> e : targets.entrySet()) {
		cases[j] = e.getKey();
		labels[j++] = assembler.getLabel(e.getValue());
	}

	expression.toCode(visitor, assembler);
	int[] cast = TypeUtils.getPrimitiveCastOpcodes(expression.getType(), Type.INT_TYPE); // widen
	for (int i = 0; i < cast.length; i++) {
		visitor.visitInsn(cast[i]);
	}
	boolean fitsIntoTable = fitsIntoTableSwitch();
	if (fitsIntoTable) {
		visitor.visitTableSwitchInsn(cases[0], cases[cases.length - 1], assembler.getLabel(defaultTarget), labels);
	} else {
		visitor.visitLookupSwitchInsn(assembler.getLabel(defaultTarget), cases, labels);
	}
}
 
Example 2
@Override
public void accept(final MethodVisitor methodVisitor) {
	int[] keysArray = new int[this.keys.size()];
	for (int i = 0, n = keysArray.length; i < n; ++i) {
		keysArray[i] = this.keys.get(i).intValue();
	}
	Label[] labelsArray = new Label[this.labels.size()];
	for (int i = 0, n = labelsArray.length; i < n; ++i) {
		labelsArray[i] = this.labels.get(i).getLabel();
	}
	methodVisitor.visitLookupSwitchInsn(dflt.getLabel(), keysArray, labelsArray);
	acceptAnnotations(methodVisitor);
}
 
Example 3
Source Project: Concurnas   File: LookupSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  int[] keysArray = new int[this.keys.size()];
  for (int i = 0, n = keysArray.length; i < n; ++i) {
    keysArray[i] = this.keys.get(i).intValue();
  }
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitLookupSwitchInsn(dflt.getLabel(), keysArray, labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 4
@Override
public void accept(final MethodVisitor mv) {
  int[] keys = new int[this.keys.size()];
  for (int i = 0; i < keys.length; ++i) {
    keys[i] = this.keys.get(i).intValue();
  }
  Label[] labels = new Label[this.labels.size()];
  for (int i = 0; i < labels.length; ++i) {
    labels[i] = this.labels.get(i).getLabel();
  }
  mv.visitLookupSwitchInsn(dflt.getLabel(), keys, labels);
  acceptAnnotations(mv);
}
 
Example 5
Source Project: yql-plus   File: CodeEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public void emitIntegerSwitch(Map<Integer, Label> labels, Label defaultCase) {
    // TODO: we should see if the labels are dense, and if so use TABLESWITCH
    MethodVisitor mv = getMethodVisitor();
    List<Integer> codes = Lists.newArrayList(labels.keySet());
    Collections.sort(codes);
    int[] k = new int[codes.size()];
    Label[] kl = new Label[codes.size()];
    for (int i = 0; i < k.length; ++i) {
        k[i] = codes.get(i);
        kl[i] = labels.get(i);
    }
    mv.visitLookupSwitchInsn(defaultCase, k, kl);
    mv.visitJumpInsn(GOTO, defaultCase);
}
 
Example 6
Source Project: yql-plus   File: CodeEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public void emitStringSwitch(Map<String, Label> labels, Label defaultCase, boolean caseInsensitive) {
    MethodVisitor mv = getMethodVisitor();
    if (caseInsensitive) {
        mv.visitFieldInsn(GETSTATIC, "java/util/Locale", "ENGLISH", "Ljava/util/Locale;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toUpperCase", "(Ljava/util/Locale;)Ljava/lang/String;", false);
    }
    mv.visitInsn(DUP);
    AssignableValue tmp = allocate(String.class);
    tmp.write(BaseTypeAdapter.STRING).generate(this);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    Multimap<Integer, SwitchLabel> keys = ArrayListMultimap.create();
    for (Map.Entry<String, Label> e : labels.entrySet()) {
        String name = e.getKey();
        if (caseInsensitive) {
            name = name.toUpperCase(Locale.ENGLISH);
        }
        keys.put(name.hashCode(), new SwitchLabel(name, e.getValue()));
    }
    List<Integer> codes = Lists.newArrayList(keys.keySet());
    Collections.sort(codes);
    int[] k = new int[codes.size()];
    Label[] kl = new Label[codes.size()];
    for (int i = 0; i < k.length; ++i) {
        k[i] = codes.get(i);
        kl[i] = new Label();
    }
    mv.visitLookupSwitchInsn(defaultCase, k, kl);
    for (int i = 0; i < k.length; ++i) {
        mv.visitLabel(kl[i]);
        for (SwitchLabel switchLabel : keys.get(k[i])) {
            mv.visitLdcInsn(switchLabel.name);
            tmp.read().generate(this);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
            mv.visitJumpInsn(IFNE, switchLabel.label);
        }
        mv.visitJumpInsn(GOTO, defaultCase);
    }
}
 
Example 7
Source Project: JReFrameworker   File: LookupSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  int[] keysArray = new int[this.keys.size()];
  for (int i = 0, n = keysArray.length; i < n; ++i) {
    keysArray[i] = this.keys.get(i).intValue();
  }
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitLookupSwitchInsn(dflt.getLabel(), keysArray, labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 8
Source Project: JReFrameworker   File: LookupSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  int[] keysArray = new int[this.keys.size()];
  for (int i = 0, n = keysArray.length; i < n; ++i) {
    keysArray[i] = this.keys.get(i).intValue();
  }
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitLookupSwitchInsn(dflt.getLabel(), keysArray, labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 9
@Override
public void accept(MethodVisitor visitor)
{
	visitor.visitLookupSwitchInsn(defi.getLabel(), match,
		branchi.stream().map(l -> l.getLabel()).toArray(org.objectweb.asm.Label[]::new));
}