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.variableBuilder;
017
018import com.sun.codemodel.JExpr;
019import com.sun.codemodel.JExpression;
020import org.androidtransfuse.gen.InjectionBuilderContext;
021import org.androidtransfuse.gen.InjectionExpressionBuilder;
022import org.androidtransfuse.gen.variableDecorator.TypedExpressionFactory;
023import org.androidtransfuse.model.InjectionNode;
024import org.androidtransfuse.model.TypedExpression;
025
026import javax.inject.Inject;
027
028/**
029 * @author John Ericksen
030 */
031public class SystemServiceVariableBuilder extends ConsistentTypeVariableBuilder {
032
033    private static final String GET_SYSTEM_SERVICE = "getSystemService";
034
035    private final String systemService;
036    private final InjectionNode contextInjectionNode;
037    private final InjectionExpressionBuilder injectionExpressionBuilder;
038
039    @Inject
040    public SystemServiceVariableBuilder(/*@Assisted*/ String systemService,
041                                        /*@Assisted*/ InjectionNode contextInjectionNode,
042                                        InjectionExpressionBuilder injectionExpressionBuilder,
043                                        TypedExpressionFactory typedExpressionFactory) {
044        super(Object.class, typedExpressionFactory);
045        this.systemService = systemService;
046        this.contextInjectionNode = contextInjectionNode;
047        this.injectionExpressionBuilder = injectionExpressionBuilder;
048    }
049
050    @Override
051    public JExpression buildExpression(InjectionBuilderContext injectionBuilderContext, InjectionNode injectionNode) {
052        TypedExpression contextVar = injectionExpressionBuilder.buildVariable(injectionBuilderContext, contextInjectionNode);
053
054        return contextVar.getExpression().invoke(GET_SYSTEM_SERVICE).arg(JExpr.lit(systemService));
055    }
056}