001/**
002 * Copyright 2013 John Ericksen
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.androidtransfuse.experiment.generators;
017
018import com.sun.codemodel.*;
019import org.androidtransfuse.adapter.ASTMethod;
020import org.androidtransfuse.adapter.ASTType;
021import org.androidtransfuse.adapter.element.ASTElementFactory;
022import org.androidtransfuse.experiment.*;
023import org.androidtransfuse.gen.ClassGenerationUtil;
024import org.androidtransfuse.gen.UniqueVariableNamer;
025import org.androidtransfuse.model.InjectionSignature;
026import org.androidtransfuse.model.MethodDescriptor;
027import org.androidtransfuse.scope.ApplicationScope;
028import org.androidtransfuse.scope.ScopeKey;
029import org.androidtransfuse.scope.Scopes;
030import org.androidtransfuse.util.AndroidLiterals;
031
032import javax.inject.Inject;
033
034/**
035 * @author John Ericksen
036 */
037public class ApplicationScopeSeedGenerator implements Generation {
038
039    private final ASTElementFactory astElementFactory;
040    private final UniqueVariableNamer namer;
041    private final ClassGenerationUtil generationUtil;
042
043    @Inject
044    public ApplicationScopeSeedGenerator(UniqueVariableNamer namer, ASTElementFactory astElementFactory, ClassGenerationUtil generationUtil) {
045        this.namer = namer;
046        this.astElementFactory = astElementFactory;
047        this.generationUtil = generationUtil;
048    }
049
050    @Override
051    public void schedule(final ComponentBuilder builder, ComponentDescriptor descriptor) {
052        ASTMethod onCreateMethod = astElementFactory.findMethod(AndroidLiterals.APPLICATION, "onCreate");
053        builder.add(onCreateMethod, GenerationPhase.POSTSCOPES, new ComponentMethodGenerator() {
054            @Override
055            public void generate(MethodDescriptor methodDescriptor, JBlock block) {
056                // get ApplicationScope
057                JClass applicationScopeType = generationUtil.ref(ApplicationScope.class);
058                JVar scopeVar = block.decl(applicationScopeType, namer.generateName(ApplicationScope.class),
059                        JExpr.cast(applicationScopeType, builder.getScopes().invoke(Scopes.GET_SCOPE)
060                                .arg(generationUtil.ref(ApplicationScope.ApplicationScopeQualifier.class).dotclass()))
061                );
062
063                //seed Application to "this"
064                block.invoke(scopeVar, ApplicationScope.SEED_METHOD)
065                        .arg(buildScopeKey(AndroidLiterals.APPLICATION))
066                        .arg(JExpr._this());
067
068                block.invoke(scopeVar, ApplicationScope.SEED_METHOD)
069                        .arg(buildScopeKey(AndroidLiterals.CONTEXT))
070                        .arg(JExpr._this());
071            }
072        });
073    }
074
075    private JInvocation buildScopeKey(ASTType target){
076
077        JClass injectionNodeClassRef = generationUtil.ref(target);
078
079        return generationUtil.ref(ScopeKey.class).staticInvoke(ScopeKey.GET_METHOD).arg(injectionNodeClassRef.dotclass()).arg(JExpr.lit(new InjectionSignature(target).buildScopeKeySignature()));
080    }
081}