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;
017
018import com.sun.codemodel.JBlock;
019import com.sun.codemodel.JClass;
020import com.sun.codemodel.JInvocation;
021import com.sun.codemodel.JVar;
022import org.androidtransfuse.adapter.ASTMethod;
023import org.androidtransfuse.annotations.Factory;
024import org.androidtransfuse.gen.ClassGenerationUtil;
025import org.androidtransfuse.gen.ScopesGenerator;
026import org.androidtransfuse.gen.UniqueVariableNamer;
027import org.androidtransfuse.model.MethodDescriptor;
028import org.androidtransfuse.scope.Scopes;
029
030import javax.inject.Inject;
031
032/**
033 * @author John Ericksen
034 */
035public class ScopesGeneration implements Generation {
036
037    private final ClassGenerationUtil generationUtil;
038    private final UniqueVariableNamer namer;
039    private final ASTMethod method;
040
041    @Inject
042    public ScopesGeneration(ClassGenerationUtil generationUtil, UniqueVariableNamer namer, ASTMethod method) {
043        this.generationUtil = generationUtil;
044        this.namer = namer;
045        this.method = method;
046    }
047
048    @Factory public interface ScopesGenerationFactory{
049        ScopesGeneration build(ASTMethod method);
050    }
051
052    @Override
053    public void schedule(final ComponentBuilder builder, ComponentDescriptor descriptor) {
054        builder.add(method, GenerationPhase.SCOPES, new ComponentMethodGenerator() {
055            @Override
056            public void generate(MethodDescriptor methodDescriptor, JBlock block) {
057                // Scopes instance
058                JClass scopes = generationUtil.ref(Scopes.class);
059                JInvocation scopesBuildInvocation = generationUtil.ref(ScopesGenerator.TRANSFUSE_SCOPES_UTIL).staticInvoke(ScopesGenerator.GET_INSTANCE);
060                JVar scopesVar = block.decl(scopes, namer.generateName(Scopes.class), scopesBuildInvocation);
061
062                builder.setScopes(scopesVar);
063            }
064        });
065    }
066}