summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/gen/DelegateStorableGenerator.java
blob: e6ac8ef2cc2e52a93b0ae7546719ab9605727fa6 (plain)
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
/*
 * Copyright 2008-2012 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * 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.
 */

package com.amazon.carbonado.gen;

import java.util.EnumSet;
import java.util.Map;

import org.cojen.classfile.ClassFile;
import org.cojen.classfile.CodeBuilder;
import org.cojen.classfile.MethodInfo;
import org.cojen.classfile.Modifiers;
import org.cojen.classfile.TypeDesc;

import org.cojen.util.ClassInjector;
import org.cojen.util.KeyFactory;

import com.amazon.carbonado.Storable;
import com.amazon.carbonado.SupportException;

import com.amazon.carbonado.util.SoftValuedCache;

/**
 * Generates and caches concrete implementations of {@link Storable} types
 * which delegate to {@link DelegateSupport}. The delegating classes extend
 * those generated by {@link MasterStorableGenerator}.
 *
 * @author Brian S O'Neill
 * @since 1.2
 */
public class DelegateStorableGenerator<S extends Storable> {
    private static final SoftValuedCache<Object, Class> cCache;

    static {
        cCache = SoftValuedCache.newCache(11);
    }

    /**
     * Delegate class has a constructor that accepts a {@link DelegateSupport}
     * instance.
     *
     * <pre>
     * public &lt;init&gt;(DelegateSupport);
     * </pre>
     * 
     */
    public static <S extends Storable> Class<? extends S>
        getDelegateClass(Class<S> type, EnumSet<MasterFeature> features)
        throws SupportException
    {
        if (features == null) {
            features = EnumSet.noneOf(MasterFeature.class);
        }

        Object key = KeyFactory.createKey(new Object[] {type, features});

        synchronized (cCache) {
            Class<? extends S> generatedClass = (Class<? extends S>) cCache.get(key);
            if (generatedClass != null) {
                return generatedClass;
            }
            generatedClass = new DelegateStorableGenerator<S>(type, features)
                .generateAndInjectClass();
            cCache.put(key, generatedClass);
            return generatedClass;
        }
    }

    private final Class<S> mStorableType;

    private final ClassInjector mClassInjector;
    private final ClassFile mClassFile;

    private DelegateStorableGenerator(Class<S> type, EnumSet<MasterFeature> features)
        throws SupportException
    {
        mStorableType = type;

        final Class<? extends S> abstractClass =
            MasterStorableGenerator.getAbstractClass(mStorableType, features);

        mClassInjector = ClassInjector.create(mStorableType.getName(),
                                              abstractClass.getClassLoader());

        mClassFile = new ClassFile(mClassInjector.getClassName(), abstractClass);
        mClassFile.markSynthetic();
        mClassFile.setSourceFile(DelegateStorableGenerator.class.getName());
        mClassFile.setTarget("1.5");
    }

    private Class<? extends S> generateAndInjectClass() {
        TypeDesc masterSupportType = TypeDesc.forClass(MasterSupport.class);
        TypeDesc delegateSupportType = TypeDesc.forClass(DelegateSupport.class);

        // Add constructor that accepts a DelegateSupport.
        {
            TypeDesc[] params = {delegateSupportType};
            MethodInfo mi = mClassFile.addConstructor(Modifiers.PUBLIC, params);
            CodeBuilder b = new CodeBuilder(mi);
            b.loadThis();
            b.loadLocal(b.getParameter(0));
            b.invokeSuperConstructor(new TypeDesc[] {masterSupportType});
            b.returnVoid();
        }

        CodeBuilderUtil.definePrepareMethod(mClassFile, mStorableType, delegateSupportType);

        // Implement abstract methods which all delegate to DelegateSupport instance.

        generateDelegatedMethod
            (MasterStorableGenerator.DO_TRY_LOAD_MASTER_METHOD_NAME, "doTryLoad");
        generateDelegatedMethod
            (MasterStorableGenerator.DO_TRY_INSERT_MASTER_METHOD_NAME, "doTryInsert");
        generateDelegatedMethod
            (MasterStorableGenerator.DO_TRY_UPDATE_MASTER_METHOD_NAME, "doTryUpdate");
        generateDelegatedMethod
            (MasterStorableGenerator.DO_TRY_DELETE_MASTER_METHOD_NAME, "doTryDelete");

        Class<? extends S> generatedClass = mClassInjector.defineClass(mClassFile);

        return generatedClass;
    }

    private void generateDelegatedMethod(String masterMethodName, String supportMethodName) {
        TypeDesc triggerSupportType = TypeDesc.forClass(TriggerSupport.class);
        TypeDesc delegateSupportType = TypeDesc.forClass(DelegateSupport.class);

        TypeDesc[] storableParam = {TypeDesc.forClass(Storable.class)};

        MethodInfo mi = mClassFile.addMethod
            (Modifiers.PROTECTED, masterMethodName, TypeDesc.BOOLEAN, null);
        CodeBuilder b = new CodeBuilder(mi);

        b.loadThis();
        b.loadField(StorableGenerator.SUPPORT_FIELD_NAME, triggerSupportType);
        b.checkCast(delegateSupportType);
        b.loadThis();
        b.invokeInterface(delegateSupportType, supportMethodName, TypeDesc.BOOLEAN, storableParam);
        b.returnValue(TypeDesc.BOOLEAN);
    }
}