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.module; 017 018import org.androidtransfuse.ConfigurationRepository; 019import org.androidtransfuse.TransfuseAnalysisException; 020import org.androidtransfuse.TransfusePlugin; 021import org.androidtransfuse.adapter.ASTAnnotation; 022import org.androidtransfuse.adapter.ASTType; 023import org.androidtransfuse.analysis.repository.InjectionNodeBuilderRepository; 024import org.androidtransfuse.bootstrap.Bootstrap; 025import org.androidtransfuse.bootstrap.Bootstraps; 026import org.androidtransfuse.scope.Scopes; 027 028import javax.inject.Inject; 029 030/** 031 * @author John Ericksen 032 */ 033public class PluginProcessor implements TypeProcessor { 034 035 private final ConfigurationRepository repository; 036 private final Scopes scopes; 037 038 @Inject 039 public PluginProcessor(ConfigurationRepository repository, Scopes scopes) { 040 this.repository = repository; 041 this.scopes = scopes; 042 } 043 044 @Override 045 public ModuleConfiguration process(ASTType moduleType, ASTAnnotation typeAnnotation) { 046 047 final ASTType pluginType = typeAnnotation.getProperty("value", ASTType.class); 048 049 return new ModuleConfiguration() { 050 @Override 051 public void setConfiguration(InjectionNodeBuilderRepository configurationRepository) { 052 try { 053 Class<TransfusePlugin> pluginClass = (Class<TransfusePlugin>) Class.forName(pluginType.getName()); 054 TransfusePlugin plugin = pluginClass.newInstance(); 055 if(pluginClass.isAnnotationPresent(Bootstrap.class)) { 056 Bootstraps.getInjector(pluginClass).inject(scopes, plugin); 057 } 058 plugin.run(repository); 059 } catch (InstantiationException e) { 060 throw new TransfuseAnalysisException("Unable to instantiate", e); 061 } catch (IllegalAccessException e) { 062 throw new TransfuseAnalysisException("IllegalAccessException", e); 063 } catch (ClassNotFoundException e) { 064 throw new TransfuseAnalysisException("ClassNotFoundException", e); 065 } 066 } 067 }; 068 } 069}