1 package jp.sourceforge.stigmata.birthmarks;
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 NORMAL, BRANCH, OBJECT, INVOKE, TARGETER,
23 private int argumentCount;
25 private Category category;
26 private List<Label> labels = new ArrayList<Label>();
28 public Opcode(Opcode opcode){
29 this(opcode.getOpcode(), opcode.getName(), opcode.getArgumentCount(), opcode.getAct(), opcode.getCategory());
32 public Opcode(int opcode, String name, int argumentCount, int act, String category){
33 this(opcode, name, argumentCount, act, Category.valueOf(category));
36 public Opcode(int opcode, String name, int argumentCount, int act, Category category){
39 this.argumentCount = argumentCount;
41 this.category = category;
44 public final int getOpcode(){
48 public final String getName(){
52 public int getArgumentCount(){
56 public void addLabel(Label label){
58 throw new NullPointerException();
60 if(!(category == Category.TARGETER && labels.size() == 0)
61 && category != Category.BRANCH){
62 throw new IllegalStateException("this method allows only branch category");
67 public void setLabels(Label[] labelArray){
68 if(labelArray == null){
69 throw new NullPointerException();
71 if(category != Category.BRANCH){
72 throw new IllegalStateException("this method allows only branch category");
75 for(Label label: labelArray){
77 throw new NullPointerException();
83 public boolean hasLabel(Label label){
84 return labels.contains(label);
87 public Label getLabel(int index){
88 return labels.get(index);
91 public synchronized Label[] getLabels(){
92 return labels.toArray(new Label[labels.size()]);
95 public Iterator<Label> iterator(){
96 return Collections.unmodifiableList(labels).iterator();
99 public void setAct(int act){
100 if(category != Category.OBJECT && category != Category.INVOKE){
101 throw new IllegalStateException("setAct can be called only object and invoke category.");
110 public Category getCategory(){
114 public String toString(){
115 StringBuilder sb = new StringBuilder();
116 sb.append(String.format("%d:%s:%d(%s)", getOpcode(), getName(), getAct(), getCategory()));
118 if(getCategory() == Category.BRANCH || getCategory() == Category.TARGETER){
122 return new String(sb);