1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.android.processor; 18 19 import static java.util.stream.Collectors.toMap; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.testing.compile.Compiler; 23 import dagger.hilt.android.processor.internal.androidentrypoint.AndroidEntryPointProcessor; 24 import dagger.hilt.android.processor.internal.customtestapplication.CustomTestApplicationProcessor; 25 import dagger.hilt.android.processor.internal.uninstallmodules.UninstallModulesProcessor; 26 import dagger.hilt.processor.internal.aggregateddeps.AggregatedDepsProcessor; 27 import dagger.hilt.processor.internal.definecomponent.DefineComponentProcessor; 28 import dagger.hilt.processor.internal.generatesrootinput.GeneratesRootInputProcessor; 29 import dagger.hilt.processor.internal.originatingelement.OriginatingElementProcessor; 30 import dagger.hilt.processor.internal.root.RootProcessor; 31 import dagger.internal.codegen.ComponentProcessor; 32 import dagger.testing.compile.CompilerTests; 33 import java.util.Arrays; 34 import java.util.Map; 35 import javax.annotation.processing.Processor; 36 import com.tschuchort.compiletesting.KotlinCompilation; 37 38 /** {@link Compiler} instances for testing Android Hilt. */ 39 public final class AndroidCompilers { 40 compiler(Processor... extraProcessors)41 public static Compiler compiler(Processor... extraProcessors) { 42 Map<Class<?>, Processor> processors = 43 defaultProcessors().stream() 44 .collect(toMap((Processor e) -> e.getClass(), (Processor e) -> e)); 45 46 // Adds extra processors, and allows overriding any processors of the same class. 47 Arrays.stream(extraProcessors) 48 .forEach(processor -> processors.put(processor.getClass(), processor)); 49 50 return CompilerTests.compiler().withProcessors(processors.values()); 51 } 52 kotlinCompiler()53 public static KotlinCompilation kotlinCompiler() { 54 KotlinCompilation compilation = new KotlinCompilation(); 55 compilation.setAnnotationProcessors(defaultProcessors()); 56 compilation.setClasspaths( 57 ImmutableList.<java.io.File>builder() 58 .addAll(compilation.getClasspaths()) 59 .add(CompilerTests.compilerDepsJar()) 60 .build() 61 ); 62 return compilation; 63 } 64 defaultProcessors()65 private static ImmutableList<Processor> defaultProcessors() { 66 return ImmutableList.of( 67 new AggregatedDepsProcessor(), 68 new AndroidEntryPointProcessor(), 69 new ComponentProcessor(), 70 new DefineComponentProcessor(), 71 new GeneratesRootInputProcessor(), 72 new OriginatingElementProcessor(), 73 new CustomTestApplicationProcessor(), 74 new UninstallModulesProcessor(), 75 new RootProcessor()); 76 } 77 AndroidCompilers()78 private AndroidCompilers() {} 79 } 80