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.JType;
019import org.androidtransfuse.adapter.ASTAnnotation;
020import org.androidtransfuse.adapter.ASTBase;
021import org.androidtransfuse.analysis.AnalysisContext;
022import org.androidtransfuse.analysis.Analyzer;
023import org.androidtransfuse.annotations.Resource;
024import org.androidtransfuse.gen.ClassGenerationUtil;
025import org.androidtransfuse.gen.variableBuilder.resource.ResourceExpressionBuilder;
026import org.androidtransfuse.gen.variableBuilder.resource.ResourceExpressionBuilderFactory;
027import org.androidtransfuse.model.InjectionNode;
028import org.androidtransfuse.model.InjectionSignature;
029
030import javax.inject.Inject;
031
032/**
033 * @author John Ericksen
034 */
035public class ResourceInjectionNodeBuilder extends InjectionNodeBuilderSingleAnnotationAdapter {
036
037    private final ClassGenerationUtil generationUtil;
038    private final VariableInjectionBuilderFactory variableInjectionBuilderFactory;
039    private final ResourceExpressionBuilderFactory resourceExpressionBuilderFactory;
040    private final Analyzer analyzer;
041
042    @Inject
043    public ResourceInjectionNodeBuilder(ClassGenerationUtil generationUtil,
044                                        VariableInjectionBuilderFactory variableInjectionBuilderFactory,
045                                        ResourceExpressionBuilderFactory resourceExpressionBuilderFactory,
046                                        Analyzer analyzer) {
047        super(Resource.class);
048        this.generationUtil = generationUtil;
049        this.variableInjectionBuilderFactory = variableInjectionBuilderFactory;
050        this.resourceExpressionBuilderFactory = resourceExpressionBuilderFactory;
051        this.analyzer = analyzer;
052    }
053
054    @Override
055    public InjectionNode buildInjectionNode(ASTBase target, InjectionSignature signature, AnalysisContext context, ASTAnnotation annotation) {
056        Integer resourceId = annotation.getProperty("value", Integer.class);
057
058        InjectionNode injectionNode = analyzer.analyze(signature, context);
059
060        JType resourceType = generationUtil.type(signature.getType());
061
062        ResourceExpressionBuilder resourceExpressionBuilder =
063                resourceExpressionBuilderFactory.buildResourceExpressionBuilder(resourceType, context);
064
065        injectionNode.addAspect(VariableBuilder.class,
066                variableInjectionBuilderFactory.buildResourceVariableBuilder(resourceId, resourceExpressionBuilder));
067
068        return injectionNode;
069    }
070
071
072}