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.registration; 017 018import com.google.common.collect.ImmutableList; 019import org.androidtransfuse.TransfuseAnalysisException; 020import org.androidtransfuse.adapter.*; 021import org.androidtransfuse.analysis.AnalysisContext; 022import org.androidtransfuse.analysis.astAnalyzer.ASTInjectionAspect; 023import org.androidtransfuse.analysis.repository.RegistrationGeneratorFactory; 024import org.androidtransfuse.gen.componentBuilder.ActivityDelegateASTReference; 025import org.androidtransfuse.gen.componentBuilder.ComponentBuilderFactory; 026import org.androidtransfuse.gen.componentBuilder.RegistrationGenerator; 027import org.androidtransfuse.listeners.CallThrough; 028import org.androidtransfuse.model.InjectionNode; 029 030import javax.inject.Inject; 031 032public class ActivityDelegateRegistrationGeneratorFactory implements RegistrationGeneratorFactory { 033 034 private final ImmutableList<ASTMethod> methods; 035 private final ComponentBuilderFactory componentBuilderFactory; 036 037 @Inject 038 public ActivityDelegateRegistrationGeneratorFactory(/*@Assisted*/ ASTType listenerInterface, ComponentBuilderFactory componentBuilderFactory) { 039 this.componentBuilderFactory = componentBuilderFactory; 040 041 ImmutableList.Builder<ASTMethod> delegateMethods = ImmutableList.builder(); 042 for (ASTMethod method : listenerInterface.getMethods()) { 043 if (method.isAnnotated(CallThrough.class)) { 044 delegateMethods.add(method); 045 } 046 } 047 048 this.methods = delegateMethods.build(); 049 } 050 051 @Override 052 public RegistrationGenerator buildRegistrationGenerator(InjectionNode injectionNode, ASTBase astBase, ASTAnnotation registerAnnotation, AnalysisContext context) { 053 054 ActivityDelegateASTReference delegateASTReference; 055 056 if (astBase instanceof ASTType) { 057 delegateASTReference = componentBuilderFactory.buildActivityTypeDelegateASTReference(); 058 } else if (astBase instanceof ASTMethod) { 059 delegateASTReference = componentBuilderFactory.buildActivityMethodDelegateASTReference((ASTMethod) astBase); 060 } else if (astBase instanceof ASTField) { 061 delegateASTReference = componentBuilderFactory.buildActivityFieldDelegateASTReference((ASTField) astBase); 062 } else { 063 throw new TransfuseAnalysisException("ASTBase type not mapped"); 064 } 065 066 //set injection node to field 067 if (!injectionNode.containsAspect(ASTInjectionAspect.class)) { 068 injectionNode.addAspect(new ASTInjectionAspect()); 069 } 070 injectionNode.getAspect(ASTInjectionAspect.class).setAssignmentType(ASTInjectionAspect.InjectionAssignmentType.FIELD); 071 072 return componentBuilderFactory.buildActivityRegistrationGenerator(delegateASTReference, methods); 073 } 074}