forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectMetaInfo.java
More file actions
200 lines (180 loc) · 6.59 KB
/
ObjectMetaInfo.java
File metadata and controls
200 lines (180 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.asm.Label;
import act.asm.MethodVisitor;
import act.asm.Opcodes;
import act.asm.Type;
import act.data.annotation.Data;
import org.osgl.$;
import org.osgl.util.C;
import org.osgl.util.S;
/**
* Datastructure captures a class's meta information in related to {@link DataObjectEnhancer}.
* The following info will be captured:
* <ul>
* <li>Is the class annotated with {@link Data} annotation</li>
* <li>A list of {@link FieldMetaInfo} of all declared fields</li>
* </ul>
*/
class ObjectMetaInfo implements Opcodes {
/**
* Datastructure captures a class's declared field meta info
*/
static class FieldMetaInfo implements Opcodes {
private String name;
private boolean isTransient = false;
private boolean equalForce = false;
private boolean equalIgnore = false;
private Type type;
FieldMetaInfo(String name, Type type, boolean isTransient) {
this.name = $.NPE(name);
this.type = $.NPE(type);
this.isTransient = isTransient;
}
void setEqualForce() {
equalForce = true;
}
void setEqualIgnore() {
equalIgnore = true;
}
void addEqualInstructions(Type host, MethodVisitor mv, Label jumpTo) {
if (!eligible()) {
return;
}
String typeDesc = type.getDescriptor();
// ALOAD 0: this
// ALOAD 2: that = (Type) obj (which is 1)
mv.visitVarInsn(ALOAD, 2);
mv.visitFieldInsn(GETFIELD, host.getInternalName(), name, typeDesc);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, host.getInternalName(), name, typeDesc);
String s = typeDesc;
if (s.length() > 1) {
s = OBJECT_TYPE.getDescriptor();
}
String op = "eq";
if (typeDesc.startsWith("[")) {
// array needs deep eq
op = "eq2";
}
mv.visitMethodInsn(INVOKESTATIC, "org/osgl/Osgl", op, S.fmt("(%s%s)Z", s, s), false);
mv.visitJumpInsn(IFEQ, jumpTo);
}
boolean addHashCodeInstruction(Type host, MethodVisitor mv) {
if (!eligible()) {
return false;
}
mv.visitVarInsn(ALOAD, 0); // load this pointer
mv.visitFieldInsn(GETFIELD, host.getInternalName(), name, type.getDescriptor());
convertFromPrimaryType(type, mv);
return true;
}
private void convertFromPrimaryType(Type fieldType, MethodVisitor mv) {
switch (fieldType.getSort()) {
case Type.BOOLEAN:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;",false);
break;
case Type.BYTE:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;",false);
break;
case Type.CHAR:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",false);
break;
case Type.SHORT:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;",false);
break;
case Type.INT:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;",false);
break;
case Type.LONG:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;",false);
break;
case Type.FLOAT:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;",false);
break;
case Type.DOUBLE:
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;",false);
break;
default:
// do nothing
}
}
boolean eligible() {
return !equalIgnore && (!isTransient || equalForce);
}
}
private Type superType;
private Type type;
private boolean callSuper;
private C.List<FieldMetaInfo> fields = C.newList();
private boolean hasEqualMethod = false;
private boolean hasHashCodeMethod = false;
private boolean hasToStringMethod = false;
private boolean hasAutoObjectAnnotation = false;
private static Type OBJECT_TYPE = Type.getType(Object.class);
ObjectMetaInfo(Type type, Type superType) {
this.type = $.NPE(type);
if (null != superType && !OBJECT_TYPE.equals(superType)) {
this.superType = superType;
}
}
Type type() {
return type;
}
Type superType() {
return superType;
}
C.List<FieldMetaInfo> fields() {
return fields;
}
FieldMetaInfo addField(String fieldName, Type fieldType, boolean isTransient) {
FieldMetaInfo fi = new FieldMetaInfo(fieldName, fieldType, isTransient);
fields.add(fi);
return fi;
}
void requireCallSuper() {
callSuper = true;
}
boolean shouldCallSuper() {
return callSuper && null != superType;
}
void equalMethodFound() {
hasEqualMethod = true;
}
void hashCodeMethodFound() {
hasHashCodeMethod = true;
}
void toStringMethodFound() {
hasToStringMethod = true;
}
void autoObjectAnnotationFound() {
hasAutoObjectAnnotation = true;
}
boolean hasDataAnnotation() {
return hasAutoObjectAnnotation;
}
boolean shouldGenerateEqualsMethod() {
return hasAutoObjectAnnotation && !hasEqualMethod;
}
boolean shouldGenerateHashCodeMethod() {
return hasAutoObjectAnnotation && !hasHashCodeMethod;
}
}