OSDN Git Service

6aa6bff168d155a764fa6daa3df2f170955517dc
[stigmata/stigmata-plugins.git] / opcodes / src / main / java / jp / sourceforge / stigmata / birthmarks / Opcode.java
1 package jp.sourceforge.stigmata.birthmarks;
2
3 /*
4  * $Id$
5  */
6
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;
12
13 import org.objectweb.asm.Label;
14
15 /**
16  *
17  * @author Haruaki Tamada
18  * @version $Revision$
19  */
20 public class Opcode implements Serializable{
21     private static final long serialVersionUID = -2349834745416345564L;
22
23     public static enum Category{
24         NORMAL, BRANCH, OBJECT, INVOKE, TARGETER,
25     };
26     private int opcode;
27     private String name;
28     private int argumentCount;
29     private int act;
30     private Category category;
31     private List<Label> labels = new ArrayList<Label>();
32
33     public Opcode(Opcode opcode){
34         this(opcode.getOpcode(), opcode.getName(), opcode.getArgumentCount(), opcode.getAct(), opcode.getCategory());
35     }
36
37     public Opcode(int opcode, String name, int argumentCount, int act, String category){
38         this(opcode, name, argumentCount, act, Category.valueOf(category));
39     }
40
41     public Opcode(int opcode, String name, int argumentCount, int act, Category category){
42         this.opcode = opcode;
43         this.name = name;
44         this.argumentCount = argumentCount;
45         this.act = act;
46         this.category = category;
47     }
48
49     public int getOpcode(){
50         return opcode;
51     }
52
53     public String getName(){
54         return name;
55     }
56
57     public int getArgumentCount(){
58         return argumentCount;
59     }
60
61     public void addLabel(Label label){
62         if(label == null){
63             throw new NullPointerException();
64         }
65         if(category != Category.BRANCH){
66             throw new IllegalStateException("this method allows only branch category");
67         }
68         labels.add(label);
69     }
70
71     public void setLabels(Label[] labelArray){
72         if(labelArray == null){
73             throw new NullPointerException();
74         }
75         if(category != Category.BRANCH){
76             throw new IllegalStateException("this method allows only branch category");
77         }
78         labels.clear();
79         for(Label label: labelArray){
80             if(label == null){
81                 throw new NullPointerException();
82             }
83             labels.add(label);
84         }
85     }
86
87     public Label getLabel(int index){
88         return labels.get(index);
89     }
90
91     public Iterator<Label> labels(){
92         return Collections.unmodifiableList(labels).iterator();
93     }
94
95     public void setAct(int act){
96         if(category != Category.OBJECT && category != Category.INVOKE){
97             throw new IllegalStateException("setAct can be called only object and invoke category.");
98         }
99         this.act = act;
100     }
101
102     public int getAct(){
103         return act;
104     }
105
106     public Category getCategory(){
107         return category;
108     }
109
110     public String toString(){
111         return String.format("%d:%s:%f(%s)", getOpcode(), getName(), getAct(), getCategory());
112     }
113 }