Skip to content

Commit

Permalink
Adding EmptyLibraryModels abstract class for LibraryModels interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akulk022 committed Jan 16, 2024
1 parent b56733a commit 81f8b7d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 86 deletions.
92 changes: 92 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2017 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.uber.nullaway;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;

/**
* Provides a default implementation(returning appropriate empty sets) of every method defined by
* the LibraryModels interface.
*
* <p>Useful Library Model classes can subclass this class and implement only the methods for the
* hooks they actually need to take action on, rather than having to implement the entire
* LibraryModels interface. Additionally, we can add extensibility points without breaking existing
* library models, as long as we define the corresponding default behavior here.
*/
public abstract class EmptyLibraryModels implements LibraryModels {
@Override
public ImmutableSetMultimap<MethodRef, Integer> failIfNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> explicitlyNullableParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nonNullParameters() {
return ImmutableSetMultimap.of();

Check warning on line 50 in nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java#L50

Added line #L50 was not covered by tests
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesTrueParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesFalseParameters() {
return ImmutableSetMultimap.of();

Check warning on line 60 in nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java#L60

Added line #L60 was not covered by tests
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSet<MethodRef> nullableReturns() {
return ImmutableSet.of();

Check warning on line 70 in nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java#L70

Added line #L70 was not covered by tests
}

@Override
public ImmutableSet<MethodRef> nonNullReturns() {
return ImmutableSet.of();
}

@Override
public ImmutableSetMultimap<String, Integer> typeVariablesWithNullableUpperBounds() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> castToNonNullMethods() {
return ImmutableSetMultimap.of();

Check warning on line 85 in nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java#L85

Added line #L85 was not covered by tests
}

@Override
public ImmutableSet<FieldRef> nullableFields() {
return ImmutableSet.of();

Check warning on line 90 in nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/EmptyLibraryModels.java#L90

Added line #L90 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.sun.tools.javac.util.Names;
import com.uber.nullaway.CodeAnnotationInfo;
import com.uber.nullaway.Config;
import com.uber.nullaway.EmptyLibraryModels;
import com.uber.nullaway.LibraryModels;
import com.uber.nullaway.LibraryModels.MethodRef;
import com.uber.nullaway.NullAway;
Expand Down Expand Up @@ -376,7 +377,7 @@ private static LibraryModels loadLibraryModels(Config config) {
return new CombinedLibraryModels(libModelsBuilder.build(), config);
}

private static class DefaultLibraryModels implements LibraryModels {
private static class DefaultLibraryModels extends EmptyLibraryModels {

private static final ImmutableSetMultimap<MethodRef, Integer> FAIL_IF_NULL_PARAMETERS =
new ImmutableSetMultimap.Builder<MethodRef, Integer>()
Expand Down Expand Up @@ -898,7 +899,7 @@ public ImmutableSet<FieldRef> nullableFields() {
}
}

private static class CombinedLibraryModels implements LibraryModels {
private static class CombinedLibraryModels extends EmptyLibraryModels {

private final Config config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,16 @@
import static com.uber.nullaway.LibraryModels.MethodRef.methodRef;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.uber.nullaway.EmptyLibraryModels;
import com.uber.nullaway.LibraryModels;

@AutoService(LibraryModels.class)
public class ExampleLibraryModels implements LibraryModels {

@Override
public ImmutableSetMultimap<MethodRef, Integer> failIfNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> explicitlyNullableParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nonNullParameters() {
return ImmutableSetMultimap.of();
}

public class ExampleLibraryModels extends EmptyLibraryModels {
@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesTrueParameters() {
return ImmutableSetMultimap.<MethodRef, Integer>builder()
.put(methodRef("org.utilities.StringUtils", "isEmptyOrNull(java.lang.CharSequence)"), 0)
.build();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesFalseParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSet<MethodRef> nullableReturns() {
return ImmutableSet.of();
}

@Override
public ImmutableSet<MethodRef> nonNullReturns() {
return ImmutableSet.of();
}

@Override
public ImmutableSetMultimap<String, Integer> typeVariablesWithNullableUpperBounds() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> castToNonNullMethods() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSet<FieldRef> nullableFields() {
return ImmutableSet.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.uber.nullaway.EmptyLibraryModels;
import com.uber.nullaway.LibraryModels;
import com.uber.nullaway.handlers.stream.StreamModelBuilder;
import com.uber.nullaway.handlers.stream.StreamTypeRecord;

@AutoService(LibraryModels.class)
public class TestLibraryModels implements LibraryModels {

@Override
public ImmutableSetMultimap<MethodRef, Integer> failIfNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> explicitlyNullableParameters() {
return ImmutableSetMultimap.of();
}
public class TestLibraryModels extends EmptyLibraryModels {

@Override
public ImmutableSetMultimap<MethodRef, Integer> nonNullParameters() {
Expand All @@ -50,23 +41,13 @@ public ImmutableSetMultimap<MethodRef, Integer> nonNullParameters() {
.build();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesTrueParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesFalseParameters() {
return ImmutableSetMultimap.of(
methodRef("com.uber.lib.unannotated.UnannotatedWithModels", "isNonNull(java.lang.Object)"),
0);
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> nullImpliesNullParameters() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSet<MethodRef> nullableReturns() {
return ImmutableSet.of(
Expand All @@ -75,16 +56,6 @@ public ImmutableSet<MethodRef> nullableReturns() {
methodRef("com.uber.lib.unannotated.UnannotatedWithModels", "returnsNullUnannotated2()"));
}

@Override
public ImmutableSet<MethodRef> nonNullReturns() {
return ImmutableSet.of();
}

@Override
public ImmutableSetMultimap<String, Integer> typeVariablesWithNullableUpperBounds() {
return ImmutableSetMultimap.of();
}

@Override
public ImmutableSetMultimap<MethodRef, Integer> castToNonNullMethods() {
return ImmutableSetMultimap.<MethodRef, Integer>builder()
Expand Down

0 comments on commit 81f8b7d

Please sign in to comment.