• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.uber.nullaway.handlers.temporary.FluentFutureHandler;
32 
33 /** Utility static methods for the handlers package. */
34 public class Handlers {
35 
Handlers()36   private Handlers() {}
37 
38   /**
39    * Builds the default handler for the checker.
40    *
41    * @param config NullAway config
42    * @return A {@code CompositeHandler} including the standard handlers for the nullness checker.
43    */
buildDefault(Config config)44   public static Handler buildDefault(Config config) {
45     ImmutableList.Builder<Handler> handlerListBuilder = ImmutableList.builder();
46     final MethodNameUtil methodNameUtil = new MethodNameUtil();
47 
48     if (config.acknowledgeRestrictiveAnnotations()) {
49       // This runs before LibraryModelsHandler, so that library models can override third-party
50       // bytecode annotations
51       handlerListBuilder.add(new RestrictiveAnnotationHandler(config));
52     }
53     if (config.isJarInferEnabled()) {
54       handlerListBuilder.add(new InferredJARModelsHandler(config));
55     }
56     if (config.handleTestAssertionLibraries()) {
57       handlerListBuilder.add(new AssertionHandler(methodNameUtil));
58     }
59     handlerListBuilder.add(new GuavaAssertionsHandler());
60     LibraryModelsHandler libraryModelsHandler = new LibraryModelsHandler(config);
61     handlerListBuilder.add(libraryModelsHandler);
62     handlerListBuilder.add(StreamNullabilityPropagatorFactory.getRxStreamNullabilityPropagator());
63     handlerListBuilder.add(StreamNullabilityPropagatorFactory.getJavaStreamNullabilityPropagator());
64     handlerListBuilder.add(
65         StreamNullabilityPropagatorFactory.fromSpecs(
66             libraryModelsHandler.getStreamNullabilitySpecs()));
67     handlerListBuilder.add(new ContractHandler(config));
68     handlerListBuilder.add(new ApacheThriftIsSetHandler());
69     handlerListBuilder.add(new GrpcHandler());
70     handlerListBuilder.add(new RequiresNonNullHandler());
71     handlerListBuilder.add(new EnsuresNonNullHandler());
72     if (config.serializationIsActive() && config.getSerializationConfig().fieldInitInfoEnabled) {
73       handlerListBuilder.add(
74           new FieldInitializationSerializationHandler(config.getSerializationConfig()));
75     }
76     if (config.checkOptionalEmptiness()) {
77       handlerListBuilder.add(new OptionalEmptinessHandler(config, methodNameUtil));
78     }
79     if (config.checkContracts()) {
80       handlerListBuilder.add(new ContractCheckHandler(config));
81     }
82     handlerListBuilder.add(new LombokHandler(config));
83     handlerListBuilder.add(new FluentFutureHandler());
84 
85     return new CompositeHandler(handlerListBuilder.build());
86   }
87 
88   /**
89    * Builds an empty handler chain (used for the NullAway dummy empty constructor).
90    *
91    * @return An empty {@code CompositeHandler}.
92    */
buildEmpty()93   public static Handler buildEmpty() {
94     return new CompositeHandler(ImmutableList.of());
95   }
96 }
97