1 package serp.bytecode;
2
3 import java.util.*;
4
5 import serp.bytecode.lowlevel.*;
6 import serp.util.*;
7
8
9
10
11
12
13 public abstract class TypedInstruction extends Instruction {
14 private static final Set _opcodeTypes = new HashSet();
15 static {
16 _opcodeTypes.add(int.class.getName());
17 _opcodeTypes.add(long.class.getName());
18 _opcodeTypes.add(float.class.getName());
19 _opcodeTypes.add(double.class.getName());
20 _opcodeTypes.add(Object.class.getName());
21 _opcodeTypes.add(byte.class.getName());
22 _opcodeTypes.add(char.class.getName());
23 _opcodeTypes.add(short.class.getName());
24 _opcodeTypes.add(boolean.class.getName());
25 _opcodeTypes.add(void.class.getName());
26 }
27
28 TypedInstruction(Code owner) {
29 super(owner);
30 }
31
32 TypedInstruction(Code owner, int opcode) {
33 super(owner, opcode);
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47 String mapType(String type, Class[][] mappings, boolean demote) {
48 if (type == null)
49 return null;
50
51 type = getProject().getNameCache().getExternalForm(type, false);
52 if (!_opcodeTypes.contains(type) && demote)
53 type = Object.class.getName();
54
55 if (mappings != null)
56 for (int i = 0; i < mappings.length; i++)
57 if (mappings[i][0].getName().equals(type))
58 type = mappings[i][1].getName();
59 return type;
60 }
61
62
63
64
65
66 public abstract String getTypeName();
67
68
69
70
71
72 public Class getType() {
73 String type = getTypeName();
74 if (type == null)
75 return null;
76 return Strings.toClass(type, getClassLoader());
77 }
78
79
80
81
82
83 public BCClass getTypeBC() {
84 String type = getTypeName();
85 if (type == null)
86 return null;
87 return getProject().loadClass(type, getClassLoader());
88 }
89
90
91
92
93
94
95
96 public abstract TypedInstruction setType(String type);
97
98
99
100
101
102
103
104 public TypedInstruction setType(Class type) {
105 if (type == null)
106 return setType((String) null);
107 return setType(type.getName());
108 }
109
110
111
112
113
114
115
116 public TypedInstruction setType(BCClass type) {
117 if (type == null)
118 return setType((String) null);
119 return setType(type.getName());
120 }
121 }