1 package jp.sourceforge.stigmata.cflib;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Iterator;
9 import org.objectweb.asm.Label;
13 * @author Haruaki Tamada
15 public class Opcode implements Serializable, Iterable<Label>{
16 private static final long serialVersionUID = -2349834745416345564L;
18 public static enum Category{
19 CONSTANT, LOAD, STORE, ARRAY, STACK, ADD, SUBTRACT, MULTIPLY, DIVIDE, REMAIN,
20 NEGATE, SHIFT_LEFT, SHIFT_RIGHT, USHIFT_RIGHT, AND, OR, XOR, CAST, COMPARE,
21 BRANCH, RETURN, FIELD, INVOKE, NEW, THROW, TARGETER, OTHERS,
25 private int argumentCount;
27 private Category category;
28 private List<Label> labels = new ArrayList<Label>();
30 public Opcode(Opcode opcode){
31 this(opcode.getOpcode(), opcode.getName(), opcode.getArgumentCount(), opcode.getAct(), opcode.getCategory());
34 public Opcode(int opcode, String name, int argumentCount, int act, String category){
35 this(opcode, name, argumentCount, act, Category.valueOf(category));
38 public Opcode(int opcode, String name, int argumentCount, int act, Category category){
41 this.argumentCount = argumentCount;
43 this.category = category;
46 public final int getOpcode(){
50 public final String getName(){
54 public int getArgumentCount(){
58 public void addLabel(Label label){
60 throw new NullPointerException();
62 if(!(category == Category.TARGETER && labels.size() == 0)
63 && category != Category.BRANCH){
64 throw new IllegalStateException("this method allows only branch category");
69 public void setLabels(Label[] labelArray){
70 if(labelArray == null){
71 throw new NullPointerException();
73 if(category != Category.BRANCH){
74 throw new IllegalStateException("this method allows only branch category");
77 for(Label label: labelArray){
79 throw new NullPointerException();
85 public boolean hasLabel(Label label){
86 return labels.contains(label);
89 public Label getLabel(int index){
90 return labels.get(index);
93 public synchronized Label[] getLabels(){
94 return labels.toArray(new Label[labels.size()]);
98 public Iterator<Label> iterator(){
99 return Collections.unmodifiableList(labels).iterator();
102 public void setAct(int act){
103 if(category != Category.FIELD && category != Category.INVOKE){
104 throw new IllegalStateException("setAct can be called only object and invoke category.");
113 public Category getCategory(){
118 public String toString(){
119 StringBuilder sb = new StringBuilder();
120 sb.append(String.format("%d:%s:%d(%s)", getOpcode(), getName(), getAct(), getCategory()));
122 if(getCategory() == Category.BRANCH || getCategory() == Category.TARGETER){
126 return new String(sb);