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.gen.componentBuilder; 017 018import com.sun.codemodel.*; 019import org.androidtransfuse.gen.*; 020import org.androidtransfuse.layout.LayoutHandlerDelegate; 021import org.androidtransfuse.model.InjectionNode; 022import org.androidtransfuse.model.TypedExpression; 023import org.androidtransfuse.scope.Scopes; 024import org.androidtransfuse.util.Logger; 025 026import javax.inject.Inject; 027import java.util.Map; 028 029/** 030 * @author John Ericksen 031 */ 032public class LayoutHandlerBuilder implements LayoutBuilder { 033 034 private final InjectionFragmentGenerator injectionFragmentGenerator; 035 private final InstantiationStrategyFactory instantiationStrategyFactory; 036 private final InjectionNode layoutHandlerInjectionNode; 037 private final Logger logger; 038 private final UniqueVariableNamer namer; 039 private final ClassGenerationUtil generationUtil; 040 041 @Inject 042 public LayoutHandlerBuilder(InjectionFragmentGenerator injectionFragmentGenerator, 043 InstantiationStrategyFactory instantiationStrategyFactory, 044 /*@Assisted*/ InjectionNode layoutHandlerInjectionNode, 045 Logger logger, 046 UniqueVariableNamer namer, 047 ClassGenerationUtil generationUtil) { 048 this.injectionFragmentGenerator = injectionFragmentGenerator; 049 this.instantiationStrategyFactory = instantiationStrategyFactory; 050 this.layoutHandlerInjectionNode = layoutHandlerInjectionNode; 051 this.logger = logger; 052 this.namer = namer; 053 this.generationUtil = generationUtil; 054 } 055 056 @Override 057 public void buildLayoutCall(JDefinedClass definedClass, JBlock block) { 058 059 try { 060 // Scopes instance 061 JClass scopesRef = generationUtil.ref(Scopes.class); 062 JInvocation scopesBuildInvocation = generationUtil.ref(ScopesGenerator.TRANSFUSE_SCOPES_UTIL).staticInvoke(ScopesGenerator.GET_INSTANCE); 063 JVar scopesVar = block.decl(scopesRef, namer.generateName(Scopes.class), scopesBuildInvocation); 064 065 Map<InjectionNode, TypedExpression> expressionMap = injectionFragmentGenerator.buildFragment(block, 066 instantiationStrategyFactory.buildMethodStrategy(block, scopesVar), 067 definedClass, 068 layoutHandlerInjectionNode, 069 scopesVar); 070 071 //LayoutHandlerDelegate.invokeLayout() 072 JExpression layoutHandlerDelegate = expressionMap.get(layoutHandlerInjectionNode).getExpression(); 073 074 block.add(layoutHandlerDelegate.invoke(LayoutHandlerDelegate.INVOKE_LAYOUT_METHOD)); 075 076 } catch (JClassAlreadyExistsException e) { 077 logger.error("JClassAlreadyExistsException while trying to generate LayoutHandler", e); 078 } 079 } 080}