1 /* 2 * Copyright (c) 2017 Uber Technologies, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 * THE SOFTWARE. 21 */ 22 23 package com.uber.nullaway.handlers; 24 25 import com.google.common.collect.ImmutableList; 26 import com.uber.nullaway.Config; 27 import com.uber.nullaway.handlers.contract.ContractCheckHandler; 28 import com.uber.nullaway.handlers.contract.ContractHandler; 29 import com.uber.nullaway.handlers.contract.fieldcontract.EnsuresNonNullHandler; 30 import com.uber.nullaway.handlers.contract.fieldcontract.RequiresNonNullHandler; 31 32 /** Utility static methods for the handlers package. */ 33 public class Handlers { 34 Handlers()35 private Handlers() {} 36 37 /** 38 * Builds the default handler for the checker. 39 * 40 * @param config NullAway config 41 * @return A {@code CompositeHandler} including the standard handlers for the nullness checker. 42 */ buildDefault(Config config)43 public static Handler buildDefault(Config config) { 44 ImmutableList.Builder<Handler> handlerListBuilder = ImmutableList.builder(); 45 final MethodNameUtil methodNameUtil = new MethodNameUtil(); 46 47 if (config.acknowledgeRestrictiveAnnotations()) { 48 // This runs before LibraryModelsHandler, so that library models can override third-party 49 // bytecode annotations 50 handlerListBuilder.add(new RestrictiveAnnotationHandler(config)); 51 } 52 if (config.isJarInferEnabled()) { 53 handlerListBuilder.add(new InferredJARModelsHandler(config)); 54 } 55 if (config.handleTestAssertionLibraries()) { 56 handlerListBuilder.add(new AssertionHandler(methodNameUtil)); 57 } 58 handlerListBuilder.add(new LibraryModelsHandler(config)); 59 handlerListBuilder.add(StreamNullabilityPropagatorFactory.getRxStreamNullabilityPropagator()); 60 handlerListBuilder.add(StreamNullabilityPropagatorFactory.getJavaStreamNullabilityPropagator()); 61 handlerListBuilder.add(new ContractHandler(config)); 62 handlerListBuilder.add(new ApacheThriftIsSetHandler()); 63 handlerListBuilder.add(new GrpcHandler()); 64 handlerListBuilder.add(new RequiresNonNullHandler()); 65 handlerListBuilder.add(new EnsuresNonNullHandler()); 66 if (config.checkOptionalEmptiness()) { 67 handlerListBuilder.add(new OptionalEmptinessHandler(config, methodNameUtil)); 68 } 69 if (config.checkContracts()) { 70 handlerListBuilder.add(new ContractCheckHandler(config)); 71 } 72 73 return new CompositeHandler(handlerListBuilder.build()); 74 } 75 76 /** 77 * Builds an empty handler chain (used for the NullAway dummy empty constructor). 78 * 79 * @return An empty {@code CompositeHandler}. 80 */ buildEmpty()81 public static Handler buildEmpty() { 82 return new CompositeHandler(ImmutableList.of()); 83 } 84 } 85