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 org.androidtransfuse.TransfuseAnalysisException;
019import org.androidtransfuse.adapter.ASTMethod;
020import org.androidtransfuse.adapter.ASTParameter;
021import org.androidtransfuse.adapter.ASTType;
022import org.androidtransfuse.analysis.AnalysisContext;
023import org.androidtransfuse.annotations.Observes;
024import org.androidtransfuse.model.InjectionNode;
025import org.androidtransfuse.validation.Validator;
026
027import javax.inject.Inject;
028
029/**
030 * Analysis class to find the methods annotated with @Observes.  When found, an ObservesAspect is populated with the
031 * observing method.  Multiple observer methods could be defined per class and per InjectionNode
032 *
033 * @author John Ericksen
034 */
035public class ObservesAnalysis extends ASTAnalysisAdaptor {
036
037    private final Validator validator;
038
039    @Inject
040    public ObservesAnalysis(Validator validator) {
041        this.validator = validator;
042    }
043
044    @Override
045    public void analyzeMethod(InjectionNode injectionNode, ASTType concreteType, ASTMethod astMethod, AnalysisContext context) {
046
047        ASTParameter firstParameter = null;
048        if (astMethod.getParameters().size() > 0) {
049            firstParameter = astMethod.getParameters().get(0);
050        }
051        for (int i = 1; i < astMethod.getParameters().size(); i++) {
052            if (astMethod.getParameters().get(i).isAnnotated(Observes.class)) {
053                //don't accept @Observes outside of the first parameter
054                validator.error("@Observes methods must annotate either the method or first method parameter")
055                                               .element(astMethod.getParameters().get(i))
056                                               .build();
057                throw new TransfuseAnalysisException("Malformed event Observer found on " + astMethod.getName());
058            }
059        }
060
061        if (firstParameter != null && (firstParameter.isAnnotated(Observes.class) || astMethod.isAnnotated(Observes.class))) {
062            //don't accept @Observes with more than one parameter
063            if (astMethod.getParameters().size() != 1) {
064                validator.error("@Observes methods must contain one and only one event parameter")
065                        .element(astMethod)
066                        .build();
067                throw new TransfuseAnalysisException("Malformed event Observer found on " + astMethod.getName());
068            }
069            if (!injectionNode.containsAspect(ObservesAspect.class)) {
070                injectionNode.addAspect(new ObservesAspect());
071            }
072            ObservesAspect aspect = injectionNode.getAspect(ObservesAspect.class);
073
074            aspect.addObserver(firstParameter.getASTType(), astMethod);
075        }
076    }
077}