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.testing.compile; 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.processor.internal.aggregateddeps.AggregatedDepsProcessor; 26 import dagger.hilt.processor.internal.aliasof.AliasOfProcessor; 27 import dagger.hilt.processor.internal.definecomponent.DefineComponentProcessor; 28 import dagger.hilt.processor.internal.earlyentrypoint.EarlyEntryPointProcessor; 29 import dagger.hilt.processor.internal.generatesrootinput.GeneratesRootInputProcessor; 30 import dagger.hilt.processor.internal.originatingelement.OriginatingElementProcessor; 31 import dagger.hilt.processor.internal.root.ComponentTreeDepsProcessor; 32 import dagger.hilt.processor.internal.root.RootProcessor; 33 import dagger.hilt.processor.internal.uninstallmodules.UninstallModulesProcessor; 34 import dagger.internal.codegen.ComponentProcessor; 35 import dagger.testing.compile.CompilerTests; 36 import java.util.Arrays; 37 import java.util.Collection; 38 import java.util.Map; 39 import javax.annotation.processing.Processor; 40 import com.tschuchort.compiletesting.KotlinCompilation; 41 42 /** {@link Compiler} instances for testing Android Hilt. */ 43 public final class HiltCompilerTests { 44 compiler(Processor... extraProcessors)45 public static Compiler compiler(Processor... extraProcessors) { 46 return compiler(Arrays.asList(extraProcessors)); 47 } 48 compiler(Collection<? extends Processor> extraProcessors)49 public static Compiler compiler(Collection<? extends Processor> extraProcessors) { 50 Map<Class<?>, Processor> processors = 51 defaultProcessors().stream() 52 .collect(toMap((Processor e) -> e.getClass(), (Processor e) -> e)); 53 54 // Adds extra processors, and allows overriding any processors of the same class. 55 extraProcessors.stream().forEach(processor -> processors.put(processor.getClass(), processor)); 56 57 return CompilerTests.compiler().withProcessors(processors.values()); 58 } 59 kotlinCompiler()60 public static KotlinCompilation kotlinCompiler() { 61 KotlinCompilation compilation = new KotlinCompilation(); 62 compilation.setAnnotationProcessors(defaultProcessors()); 63 compilation.setClasspaths( 64 ImmutableList.<java.io.File>builder() 65 .addAll(compilation.getClasspaths()) 66 .add(CompilerTests.compilerDepsJar()) 67 .build() 68 ); 69 return compilation; 70 } 71 defaultProcessors()72 private static ImmutableList<Processor> defaultProcessors() { 73 return ImmutableList.of( 74 new AggregatedDepsProcessor(), 75 new AliasOfProcessor(), 76 new AndroidEntryPointProcessor(), 77 new ComponentProcessor(), 78 new ComponentTreeDepsProcessor(), 79 new CustomTestApplicationProcessor(), 80 new DefineComponentProcessor(), 81 new EarlyEntryPointProcessor(), 82 new GeneratesRootInputProcessor(), 83 new OriginatingElementProcessor(), 84 new RootProcessor(), 85 new UninstallModulesProcessor()); 86 } 87 HiltCompilerTests()88 private HiltCompilerTests() {} 89 } 90