|
| 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