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.internal.testing; 18 19 import java.lang.reflect.InvocationTargetException; 20 21 /** Stores the {@link TestComponentData} for a Hilt test class. */ 22 public abstract class TestComponentDataSupplier { 23 24 /** Returns a {@link TestComponentData}. */ get()25 protected abstract TestComponentData get(); 26 get(Class<?> testClass)27 static TestComponentData get(Class<?> testClass) { 28 String generatedClassName = getEnclosedClassName(testClass) + "_TestComponentDataSupplier"; 29 try { 30 return Class.forName(generatedClassName) 31 .asSubclass(TestComponentDataSupplier.class) 32 .getDeclaredConstructor() 33 .newInstance() 34 .get(); 35 // We catch each individual exception rather than using a multicatch because multi-catch will 36 // get compiled to the common but new super type ReflectiveOperationException, which is not 37 // allowed on API < 19. See b/187826710. 38 } catch (ClassNotFoundException e) { 39 throw new RuntimeException(errorMessage(testClass, generatedClassName), e); 40 } catch (NoSuchMethodException e) { 41 throw new RuntimeException(errorMessage(testClass, generatedClassName), e); 42 } catch (IllegalAccessException e) { 43 throw new RuntimeException(errorMessage(testClass, generatedClassName), e); 44 } catch (InstantiationException e) { 45 throw new RuntimeException(errorMessage(testClass, generatedClassName), e); 46 } catch (InvocationTargetException e) { 47 throw new RuntimeException(errorMessage(testClass, generatedClassName), e); 48 } 49 } 50 errorMessage(Class<?> testClass, String generatedClassName)51 private static String errorMessage(Class<?> testClass, String generatedClassName) { 52 return String.format( 53 "Hilt test, %s, is missing generated file: %s. Check that the test class is " 54 + " annotated with @HiltAndroidTest and that the processor is running over your" 55 + " test.", 56 testClass.getSimpleName(), 57 generatedClassName); 58 } 59 getEnclosedClassName(Class<?> testClass)60 private static String getEnclosedClassName(Class<?> testClass) { 61 StringBuilder sb = new StringBuilder(); 62 Class<?> currClass = testClass; 63 while (currClass != null) { 64 Class<?> enclosingClass = currClass.getEnclosingClass(); 65 if (enclosingClass != null) { 66 sb.insert(0, "_" + currClass.getSimpleName()); 67 } else { 68 sb.insert(0, currClass.getCanonicalName()); 69 } 70 currClass = enclosingClass; 71 } 72 return sb.toString(); 73 } 74 } 75