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.componentBuilder; 017 018import com.google.common.collect.ImmutableList; 019import com.sun.codemodel.JBlock; 020import com.sun.codemodel.JExpression; 021import com.sun.codemodel.JInvocation; 022import org.androidtransfuse.adapter.ASTMethod; 023import org.androidtransfuse.adapter.ASTParameter; 024import org.androidtransfuse.adapter.ASTVoidType; 025import org.androidtransfuse.experiment.ComponentBuilder; 026import org.androidtransfuse.experiment.ComponentMethodGenerator; 027import org.androidtransfuse.experiment.GenerationPhase; 028import org.androidtransfuse.model.MethodDescriptor; 029import org.androidtransfuse.model.TypedExpression; 030 031import javax.inject.Inject; 032 033/** 034 * @author John Ericksen 035 */ 036public class ActivityDelegateRegistrationGenerator implements RegistrationGenerator { 037 038 private final ImmutableList<ASTMethod> methods; 039 private final ActivityDelegateASTReference activityDelegateASTReference; 040 041 @Inject 042 public ActivityDelegateRegistrationGenerator(/*@Assisted*/ ActivityDelegateASTReference activityDelegateASTReference, 043 /*@Assisted*/ ImmutableList<ASTMethod> methods) { 044 this.methods = methods; 045 this.activityDelegateASTReference = activityDelegateASTReference; 046 } 047 048 @Override 049 public void build(final ComponentBuilder componentBuilder, ASTMethod creationMethod, final TypedExpression value) { 050 for (ASTMethod method : methods) { 051 052 componentBuilder.add(method, GenerationPhase.REGISTRATION, new ComponentMethodGenerator() { 053 @Override 054 public void generate(MethodDescriptor methodDescriptor, JBlock block) { 055 JExpression targetExpression = activityDelegateASTReference.buildReference(componentBuilder.getDefinedClass(), value); 056 057 JInvocation delegateInvocation = targetExpression.invoke(methodDescriptor.getASTMethod().getName()); 058 059 for (ASTParameter astParameter : methodDescriptor.getASTMethod().getParameters()) { 060 delegateInvocation.arg(methodDescriptor.getParameter(astParameter).getExpression()); 061 } 062 063 if(ASTVoidType.VOID.equals(methodDescriptor.getASTMethod().getReturnType())){ 064 block.add(delegateInvocation); 065 } 066 else{ 067 block._return(delegateInvocation); 068 } 069 } 070 }); 071 } 072 } 073}