Skip to content

Commit f6cfa7e

Browse files
committed
update to act-1.8;catch ebean2 change: Support act timestamp annotation #19
1 parent adec8e9 commit f6cfa7e

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# act-ebean CHANGE LOG
22

3+
1.5.0
4+
* update to act-1.8.0
5+
* catch ebean2 change: Support act timestamp annotation #19
6+
37
1.4.0 - 19/Feb/2018
48
* update to act-1.7.0
59
* update to act-sql-common-1.3.0

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<artifactId>act-ebean</artifactId>
2222
<packaging>jar</packaging>
23-
<version>1.4.1-SNAPSHOT</version>
23+
<version>1.5.0-SNAPSHOT</version>
2424

2525
<name>ACT Ebean</name>
2626
<description>Provides SQL database access through Ebean ORM library (java 7+)</description>
@@ -35,7 +35,7 @@
3535
<parent>
3636
<groupId>org.actframework</groupId>
3737
<artifactId>parent</artifactId>
38-
<version>1.7.0</version>
38+
<version>1.8.0</version>
3939
</parent>
4040

4141
<properties>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package act.db.ebean.util;
2+
3+
/*-
4+
* #%L
5+
* ACT Ebean2
6+
* %%
7+
* Copyright (C) 2015 - 2018 ActFramework
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import act.app.App;
24+
import act.asm.AnnotationVisitor;
25+
import act.asm.FieldVisitor;
26+
import act.asm.Type;
27+
import act.db.meta.EntityClassMetaInfo;
28+
import act.db.meta.EntityFieldMetaInfo;
29+
import act.db.meta.EntityMetaInfoRepo;
30+
import act.util.AppByteCodeEnhancer;
31+
import org.osgl.util.S;
32+
33+
// Adapt act timestamp annotation to ebean timestamp annotation
34+
public class TimestampAuditorEnhancer extends AppByteCodeEnhancer<TimestampAuditorEnhancer> {
35+
36+
private EntityMetaInfoRepo metaInfoRepo;
37+
private String className;
38+
private String createdAt;
39+
private String lastModifiedAt;
40+
41+
public TimestampAuditorEnhancer() {
42+
super(S.F.startsWith("act.").negate());
43+
}
44+
45+
@Override
46+
protected Class<TimestampAuditorEnhancer> subClass() {
47+
return TimestampAuditorEnhancer.class;
48+
}
49+
50+
@Override
51+
public AppByteCodeEnhancer app(App app) {
52+
metaInfoRepo = app.entityMetaInfoRepo();
53+
return super.app(app);
54+
}
55+
56+
@Override
57+
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
58+
String classDesc = "L" + name + ";";
59+
className = Type.getType(classDesc).getClassName();
60+
EntityClassMetaInfo classMetaInfo = metaInfoRepo.classMetaInfo(className);
61+
if (null != classMetaInfo) {
62+
EntityFieldMetaInfo fieldMetaInfo = classMetaInfo.createdAtField();
63+
if (null != fieldMetaInfo) {
64+
createdAt = fieldMetaInfo.fieldName();
65+
}
66+
fieldMetaInfo = classMetaInfo.lastModifiedAtField();
67+
if (null != fieldMetaInfo) {
68+
lastModifiedAt = fieldMetaInfo.fieldName();
69+
}
70+
}
71+
super.visit(version, access, name, signature, superName, interfaces);
72+
}
73+
74+
@Override
75+
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
76+
FieldVisitor fv = super.visitField(access, name, desc, signature, value);
77+
if (null == createdAt && null == lastModifiedAt) {
78+
return fv;
79+
}
80+
final boolean isCreatedAt = name.equals(createdAt);
81+
final boolean isLastModified = !isCreatedAt && name.equals(lastModifiedAt);
82+
if (!isCreatedAt && !isLastModified) {
83+
return fv;
84+
}
85+
return new FieldVisitor(ASM5, fv) {
86+
@Override
87+
public void visitEnd() {
88+
AnnotationVisitor av;
89+
if (isCreatedAt) {
90+
av = fv.visitAnnotation("Lcom/avaje/ebean/annotation/WhenCreated;", true);
91+
} else {
92+
av = fv.visitAnnotation("Lcom/avaje/ebean/annotation/WhenModified;", true);
93+
}
94+
av.visitEnd();
95+
super.visitEnd();
96+
}
97+
};
98+
}
99+
}

0 commit comments

Comments
 (0)