1 package jp.sourceforge.stigmata.birthmarks;
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Iterator;
11 import java.util.List;
13 import org.objectweb.asm.Label;
17 * @author Haruaki Tamada
20 public class Opcode implements Serializable, Iterable<Label>{
21 private static final long serialVersionUID = -2349834745416345564L;
23 public static enum Category{
24 NORMAL, BRANCH, OBJECT, INVOKE, TARGETER,
28 private int argumentCount;
30 private Category category;
31 private List<Label> labels = new ArrayList<Label>();
33 public Opcode(Opcode opcode){
34 this(opcode.getOpcode(), opcode.getName(), opcode.getArgumentCount(), opcode.getAct(), opcode.getCategory());
37 public Opcode(int opcode, String name, int argumentCount, int act, String category){
38 this(opcode, name, argumentCount, act, Category.valueOf(category));
41 public Opcode(int opcode, String name, int argumentCount, int act, Category category){
44 this.argumentCount = argumentCount;
46 this.category = category;
49 public final int getOpcode(){
53 public final String getName(){
57 public int getArgumentCount(){
61 public void addLabel(Label label){
63 throw new NullPointerException();
65 if(!(category == Category.TARGETER && labels.size() == 0)
66 && category != Category.BRANCH){
67 throw new IllegalStateException("this method allows only branch category");
72 public void setLabels(Label[] labelArray){
73 if(labelArray == null){
74 throw new NullPointerException();
76 if(category != Category.BRANCH){
77 throw new IllegalStateException("this method allows only branch category");
80 for(Label label: labelArray){
82 throw new NullPointerException();
88 public boolean hasLabel(Label label){
89 return labels.contains(label);
92 public Label getLabel(int index){
93 return labels.get(index);
96 public synchronized Label[] getLabels(){
97 return labels.toArray(new Label[labels.size()]);
100 public Iterator<Label> iterator(){
101 return Collections.unmodifiableList(labels).iterator();
104 public void setAct(int act){
105 if(category != Category.OBJECT && category != Category.INVOKE){
106 throw new IllegalStateException("setAct can be called only object and invoke category.");
115 public Category getCategory(){
119 public String toString(){
120 StringBuilder sb = new StringBuilder();
121 sb.append(String.format("%d:%s:%d(%s)", getOpcode(), getName(), getAct(), getCategory()));
123 if(getCategory() == Category.BRANCH || getCategory() == Category.TARGETER){
127 return new String(sb);