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.analysis.astAnalyzer;
017
018import com.google.common.collect.ImmutableList;
019import org.androidtransfuse.adapter.ASTAnnotation;
020import org.androidtransfuse.adapter.ASTBase;
021import org.androidtransfuse.adapter.ASTMethod;
022import org.androidtransfuse.adapter.ASTType;
023import org.androidtransfuse.analysis.AnalysisContext;
024import org.androidtransfuse.annotations.ManualSuper;
025import org.androidtransfuse.model.InjectionNode;
026
027/**
028 * @author John Ericksen
029 */
030public class ManualSuperAnalysis extends ASTAnalysisAdaptor {
031
032    @Override
033    public void analyzeType(InjectionNode injectionNode, ASTType astType, AnalysisContext context) {
034        analyze(astType, injectionNode);
035    }
036
037    @Override
038    public void analyzeMethod(InjectionNode injectionNode, ASTType concreteType, ASTMethod astMethod, AnalysisContext context) {
039        analyze(astMethod, injectionNode);
040    }
041
042    private void analyze(ASTBase astBase, InjectionNode injectionNode){
043        if(astBase.isAnnotated(ManualSuper.class)){
044            ASTAnnotation annotation = astBase.getASTAnnotation(ManualSuper.class);
045
046            ManualSuperAspect aspect = nullSafeGet(injectionNode);
047            ImmutableList.Builder<ASTType> parametersBuilder = ImmutableList.builder();
048            ASTType[] parameters = annotation.getProperty("parameters", ASTType[].class);
049            if(parameters != null) {
050                for (ASTType parameterType : parameters) {
051                    parametersBuilder.add(parameterType);
052                }
053            }
054
055            aspect.add(annotation.getProperty("name", String.class), parametersBuilder.build());
056        }
057    }
058
059    private ManualSuperAspect nullSafeGet(InjectionNode injectionNode){
060        if(!injectionNode.containsAspect(ManualSuperAspect.class)){
061            injectionNode.addAspect(new ManualSuperAspect());
062        }
063        return injectionNode.getAspect(ManualSuperAspect.class);
064    }
065}