All Downloads are FREE. Search and download functionalities are using the official Maven repository.

soot.jimple.internal.AbstractSwitchStmt Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
package soot.jimple.internal;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Function;

import soot.Unit;
import soot.UnitBox;
import soot.Value;
import soot.ValueBox;
import soot.jimple.SwitchStmt;

@SuppressWarnings("serial")
public abstract class AbstractSwitchStmt extends AbstractStmt implements SwitchStmt {

  protected final ValueBox keyBox;
  protected final UnitBox defaultTargetBox;
  protected final UnitBox[] targetBoxes;
  protected final List stmtBoxes;

  protected AbstractSwitchStmt(ValueBox keyBox, UnitBox defaultTargetBox, UnitBox... targetBoxes) {
    this.keyBox = keyBox;
    this.defaultTargetBox = defaultTargetBox;
    this.targetBoxes = targetBoxes;

    // Build up stmtBoxes
    List list = new ArrayList();
    Collections.addAll(list, targetBoxes);
    list.add(defaultTargetBox);
    this.stmtBoxes = Collections.unmodifiableList(list);
  }

  // This method is necessary to deal with constructor-must-be-first-ism.
  protected static UnitBox[] getTargetBoxesArray(List targets, Function stmtBoxWrap) {
    UnitBox[] targetBoxes = new UnitBox[targets.size()];
    for (ListIterator it = targets.listIterator(); it.hasNext();) {
      Unit u = it.next();
      targetBoxes[it.previousIndex()] = stmtBoxWrap.apply(u);
    }
    return targetBoxes;
  }

  @Override
  final public Unit getDefaultTarget() {
    return defaultTargetBox.getUnit();
  }

  @Override
  final public void setDefaultTarget(Unit defaultTarget) {
    defaultTargetBox.setUnit(defaultTarget);
  }

  @Override
  final public UnitBox getDefaultTargetBox() {
    return defaultTargetBox;
  }

  @Override
  final public Value getKey() {
    return keyBox.getValue();
  }

  @Override
  final public void setKey(Value key) {
    keyBox.setValue(key);
  }

  @Override
  final public ValueBox getKeyBox() {
    return keyBox;
  }

  @Override
  final public List getUseBoxes() {
    List list = new ArrayList(keyBox.getValue().getUseBoxes());
    list.add(keyBox);
    return list;
  }

  final public int getTargetCount() {
    return targetBoxes.length;
  }

  @Override
  final public Unit getTarget(int index) {
    return targetBoxes[index].getUnit();
  }

  @Override
  final public UnitBox getTargetBox(int index) {
    return targetBoxes[index];
  }

  @Override
  final public void setTarget(int index, Unit target) {
    targetBoxes[index].setUnit(target);
  }

  @Override
  final public List getTargets() {
    final UnitBox[] boxes = this.targetBoxes;
    List targets = new ArrayList(boxes.length);
    for (UnitBox element : boxes) {
      targets.add(element.getUnit());
    }
    return targets;
  }

  final public void setTargets(List targets) {
    for (ListIterator it = targets.listIterator(); it.hasNext();) {
      Unit u = it.next();
      targetBoxes[it.previousIndex()].setUnit(u);
    }
  }

  final public void setTargets(Unit[] targets) {
    for (int i = 0, e = targets.length; i < e; i++) {
      targetBoxes[i].setUnit(targets[i]);
    }
  }

  @Override
  final public List getUnitBoxes() {
    return stmtBoxes;
  }

  @Override
  public final boolean fallsThrough() {
    return false;
  }

  @Override
  public final boolean branches() {
    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy