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 org.androidtransfuse.adapter.ASTAnnotation;
019import org.androidtransfuse.adapter.ASTBase;
020import org.androidtransfuse.analysis.AnalysisContext;
021import org.androidtransfuse.analysis.Analyzer;
022import org.androidtransfuse.analysis.InjectionPointFactory;
023import org.androidtransfuse.annotations.Preference;
024import org.androidtransfuse.model.InjectionNode;
025import org.androidtransfuse.model.InjectionSignature;
026import org.androidtransfuse.util.AndroidLiterals;
027
028import javax.inject.Inject;
029
030/**
031 * @author John Ericksen
032 */
033public class PreferenceInjectionNodeBuilder extends InjectionNodeBuilderSingleAnnotationAdapter {
034
035    private final VariableInjectionBuilderFactory variableInjectionBuilderFactory;
036    private final InjectionPointFactory injectionPointFactory;
037    private final Analyzer analyzer;
038
039    @Inject
040    public PreferenceInjectionNodeBuilder(VariableInjectionBuilderFactory variableInjectionBuilderFactory,
041                                          InjectionPointFactory injectionPointFactory,
042                                          Analyzer analyzer) {
043        super(Preference.class);
044        this.variableInjectionBuilderFactory = variableInjectionBuilderFactory;
045        this.injectionPointFactory = injectionPointFactory;
046        this.analyzer = analyzer;
047    }
048
049    @Override
050    public InjectionNode buildInjectionNode(ASTBase target, InjectionSignature signature, AnalysisContext context, ASTAnnotation annotation) {
051        String preferenceName = annotation.getProperty("value", String.class);
052
053        InjectionNode injectionNode = analyzer.analyze(signature, context);
054
055        InjectionNode preferenceManagerInjectionNode = injectionPointFactory.buildInjectionNode(AndroidLiterals.SHARED_PREFERENCES, context);
056
057        injectionNode.addAspect(VariableBuilder.class,
058                variableInjectionBuilderFactory.buildPreferenceVariableBuilder(signature.getType(), preferenceName, preferenceManagerInjectionNode));
059
060        return injectionNode;
061    }
062}