1/* 2 * Copyright (C) 2016 The Android Open Source Project 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 17package com.android.settings.core.codeinspection; 18 19import org.robolectric.shadows.ShadowApplication; 20 21import java.io.BufferedReader; 22import java.io.InputStream; 23import java.io.InputStreamReader; 24import java.lang.reflect.Modifier; 25import java.util.List; 26 27import static com.google.common.truth.Truth.assertWithMessage; 28 29/** 30 * Inspector takes a list of class objects and perform static code analysis in its {@link #run()} 31 * method. 32 */ 33public abstract class CodeInspector { 34 35 protected static final String PACKAGE_NAME = "com.android.settings"; 36 37 protected static final String TEST_CLASS_SUFFIX = "Test"; 38 private static final String TEST_INNER_CLASS_SIGNATURE = "Test$"; 39 40 protected final List<Class<?>> mClasses; 41 42 public CodeInspector(List<Class<?>> classes) { 43 mClasses = classes; 44 } 45 46 /** 47 * Code inspection runner method. 48 */ 49 public abstract void run(); 50 51 protected void assertNoObsoleteInGrandfatherList(String listName, List<String> list) { 52 final StringBuilder obsoleteGrandfatherItems = new StringBuilder( 53 listName + " contains item that should not be grandfathered.\n"); 54 for (String c : list) { 55 obsoleteGrandfatherItems.append(c).append("\n"); 56 } 57 assertWithMessage(obsoleteGrandfatherItems.toString()) 58 .that(list) 59 .isEmpty(); 60 } 61 62 protected boolean isConcreteSettingsClass(Class clazz) { 63 // Abstract classes 64 if (Modifier.isAbstract(clazz.getModifiers())) { 65 return false; 66 } 67 final String packageName = clazz.getPackage().getName(); 68 // Classes that are not in Settings 69 if (!packageName.contains(PACKAGE_NAME + ".") && !packageName.endsWith(PACKAGE_NAME)) { 70 return false; 71 } 72 final String className = clazz.getName(); 73 // Classes from tests 74 if (className.endsWith(TEST_CLASS_SUFFIX)) { 75 return false; 76 } 77 if (className.contains(TEST_INNER_CLASS_SIGNATURE)) { 78 return false; 79 } 80 return true; 81 } 82 83 public static void initializeGrandfatherList(List<String> grandfather, String filename) { 84 try { 85 final InputStream in = ShadowApplication.getInstance().getApplicationContext() 86 .getAssets() 87 .open(filename); 88 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 89 String line; 90 while ((line = reader.readLine()) != null) { 91 grandfather.add(line); 92 } 93 } catch (Exception e) { 94 throw new IllegalArgumentException("Error initializing grandfather " + filename, e); 95 } 96 97 } 98} 99