Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom path to Optional class for Optional emptiness handler #288

Merged
merged 5 commits into from Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java
Expand Up @@ -70,6 +70,8 @@ public abstract class AbstractConfig implements Config {

protected boolean checkOptionalEmptiness;

protected Set<String> optionalClassPaths;

protected boolean assertsEnabled;

/**
Expand Down Expand Up @@ -189,6 +191,11 @@ public boolean checkOptionalEmptiness() {
return checkOptionalEmptiness;
}

@Override
public Set<String> getOptionalClassPaths() {
return optionalClassPaths;
}

@Override
public boolean assertsEnabled() {
return assertsEnabled;
Expand Down
7 changes: 7 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/Config.java
Expand Up @@ -24,6 +24,7 @@

import com.google.common.collect.ImmutableSet;
import com.sun.tools.javac.code.Symbol;
import java.util.Set;
import javax.annotation.Nullable;

/** Provides configuration parameters for the nullability checker. */
Expand Down Expand Up @@ -115,6 +116,12 @@ public interface Config {
*/
boolean checkOptionalEmptiness();

/**
* @return the paths for Optional class. The list always contains the path of {@link
* java.util.Optional}.
*/
Set<String> getOptionalClassPaths();

/**
* @return the fully qualified name of a method which will take a @Nullable version of a value and
* return an @NonNull copy (likely through an unsafe downcast, but performing runtime checking
Expand Down
Expand Up @@ -27,6 +27,7 @@

import com.google.common.collect.ImmutableSet;
import com.sun.tools.javac.code.Symbol;
import java.util.Set;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -117,6 +118,11 @@ public boolean checkOptionalEmptiness() {
throw new IllegalStateException(error_msg);
}

@Override
public Set<String> getOptionalClassPaths() {
throw new IllegalStateException(error_msg);
}

@Override
@Nullable
public String getCastToNonNullMethod() {
Expand Down
Expand Up @@ -56,6 +56,7 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig {
static final String FL_ACKNOWLEDGE_RESTRICTIVE =
EP_FL_NAMESPACE + ":AcknowledgeRestrictiveAnnotations";
static final String FL_CHECK_OPTIONAL_EMPTINESS = EP_FL_NAMESPACE + ":CheckOptionalEmptiness";
static final String FL_OPTIONAL_CLASS_PATHS = EP_FL_NAMESPACE + ":OptionalClassPaths";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this should be CustomOptionalClasses, otherwise I keep parsing it on my head as "Optional (class paths)" rather than "(Optional class) paths"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that my alternative is perfect, but... mmm... maybe CheckOptionalEmptinessCustomClasses? A bit verbose, but at least ties the two options together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to CheckOptionalEmptinessCustomClasses, sounds better

static final String FL_SUPPRESS_COMMENT = EP_FL_NAMESPACE + ":AutoFixSuppressionComment";
/** --- JarInfer configs --- */
static final String FL_JI_ENABLED = EP_FL_NAMESPACE + ":JarInferEnabled";
Expand Down Expand Up @@ -141,6 +142,11 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig {
getFlagStringSet(flags, FL_EXCLUDED_FIELD_ANNOT, DEFAULT_EXCLUDED_FIELD_ANNOT));
castToNonNullMethod = flags.get(FL_CTNN_METHOD).orElse(null);
autofixSuppressionComment = flags.get(FL_SUPPRESS_COMMENT).orElse("");
optionalClassPaths =
new ImmutableSet.Builder<String>()
.addAll(getFlagStringSet(flags, FL_OPTIONAL_CLASS_PATHS))
.add("java.util.Optional")
.build();
if (autofixSuppressionComment.contains("\n")) {
throw new IllegalStateException(
"Invalid -XepOpt" + FL_SUPPRESS_COMMENT + " value. Comment must be single line.");
Expand Down
2 changes: 1 addition & 1 deletion nullaway/src/main/java/com/uber/nullaway/NullAway.java
Expand Up @@ -1109,7 +1109,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
matchWithinClass = !isExcludedClass(classSymbol, state);
// since we are processing a new top-level class, invalidate any cached
// results for previous classes
handler.onMatchTopLevelClass(this, tree, state, classSymbol);
handler.onMatchTopLevelClass(this, tree, state, classSymbol, config);
getNullnessAnalysis(state).invalidateCaches();
initTree2PrevFieldInit.clear();
class2Entities.clear();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.dataflow.AccessPath;
Expand All @@ -54,7 +55,11 @@ public class ApacheThriftIsSetHandler extends BaseNoOpHandler {

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't pass this here, pass it to the handler's constructor in Handlers.buildDefault(...), that's the convention used by existing handlers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I go ahead and do a full pass, it looks to me like this remains to be addressed :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done now 😅

if (tbaseType == null) {
tbaseType =
Optional.ofNullable(state.getTypeFromString(TBASE_NAME)).map(state.getTypes()::erasure);
Expand Down
Expand Up @@ -34,6 +34,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.dataflow.AccessPath;
Expand All @@ -60,7 +61,11 @@ protected BaseNoOpHandler() {

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
// NoOp
}

Expand Down
Expand Up @@ -35,6 +35,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.dataflow.AccessPath;
Expand Down Expand Up @@ -62,9 +63,13 @@ class CompositeHandler implements Handler {

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
for (Handler h : handlers) {
h.onMatchTopLevelClass(analysis, tree, state, classSymbol);
h.onMatchTopLevelClass(analysis, tree, state, classSymbol, config);
}
}

Expand Down
Expand Up @@ -30,6 +30,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
Expand Down Expand Up @@ -83,7 +84,11 @@ public class ContractHandler extends BaseNoOpHandler {

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
this.analysis = analysis;
this.state = state;
}
Expand Down
Expand Up @@ -34,6 +34,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
Expand Down Expand Up @@ -63,9 +64,14 @@ public interface Handler {
* @param tree The AST node for the class being matched.
* @param state The current visitor state.
* @param classSymbol The class symbol for the class being matched.
* @param config The nullaway config.
*/
void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol);
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config);

/**
* Called when NullAway first matches a particular method node.
Expand Down
Expand Up @@ -21,7 +21,6 @@
*/
package com.uber.nullaway.handlers;

import com.google.common.base.Preconditions;
import com.google.errorprone.VisitorState;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
Expand All @@ -31,12 +30,15 @@
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.Config;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.dataflow.AccessPath;
import com.uber.nullaway.dataflow.AccessPathNullnessPropagation;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
Expand All @@ -49,9 +51,7 @@
*/
public class OptionalEmptinessHandler extends BaseNoOpHandler {

private static String OPTIONAL_PATH = "java.util.Optional";

@Nullable private Optional<Type> optionalType;
@Nullable private Set<Optional<Type>> optionalTypes;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a Set<Optional<Type>> rather than a Set<Type>? We could just filter out any type paths for which state.getTypeFromString(type) returns null.

Also, it probably should be an ImmutableSet, since it won't change after the call to onMatchTopLevelClass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍


@Override
public boolean onOverrideMayBeNullExpr(
Expand All @@ -65,12 +65,20 @@ && optionalIsGetCall((Symbol.MethodSymbol) ASTHelpers.getSymbol(expr), state.get

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
if (optionalType == null) {
optionalType =
Optional.ofNullable(state.getTypeFromString(OPTIONAL_PATH))
.map(state.getTypes()::erasure);
}
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
optionalTypes =
config
.getOptionalClassPaths()
.stream()
.map(
type ->
Optional.ofNullable(state.getTypeFromString(type))
.map(state.getTypes()::erasure))
.collect(Collectors.toSet());
}

@Override
Expand Down Expand Up @@ -141,20 +149,22 @@ private void updateNonNullAPsForElement(
}

private boolean optionalIsPresentCall(Symbol.MethodSymbol symbol, Types types) {
Preconditions.checkNotNull(optionalType);
// noinspection ConstantConditions
return optionalType.isPresent()
&& symbol.getSimpleName().toString().equals("isPresent")
&& symbol.getParameters().length() == 0
&& types.isSubtype(symbol.owner.type, optionalType.get());
for (Optional<Type> optionalType : optionalTypes) {
if (optionalType.isPresent()
&& symbol.getSimpleName().toString().equals("isPresent")
&& symbol.getParameters().length() == 0
&& types.isSubtype(symbol.owner.type, optionalType.get())) return true;
}
return false;
}

private boolean optionalIsGetCall(Symbol.MethodSymbol symbol, Types types) {
Preconditions.checkNotNull(optionalType);
// noinspection ConstantConditions
return optionalType.isPresent()
&& symbol.getSimpleName().toString().equals("get")
&& symbol.getParameters().length() == 0
&& types.isSubtype(symbol.owner.type, optionalType.get());
for (Optional<Type> optionalType : optionalTypes) {
if (optionalType.isPresent()
&& symbol.getSimpleName().toString().equals("get")
&& symbol.getParameters().length() == 0
&& types.isSubtype(symbol.owner.type, optionalType.get())) return true;
}
return false;
}
}
Expand Up @@ -45,6 +45,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import com.uber.nullaway.Config;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.NullabilityUtil;
import com.uber.nullaway.Nullness;
Expand Down Expand Up @@ -203,7 +204,7 @@ class RxNullabilityPropagator extends BaseNoOpHandler {
private final Map<ReturnTree, Tree> returnToEnclosingMethodOrLambda =
new LinkedHashMap<ReturnTree, Tree>();

// Similar to above, but mapping espression-bodies to their enclosing lambdas
// Similar to above, but mapping expression-bodies to their enclosing lambdas
private final Map<ExpressionTree, LambdaExpressionTree> expressionBodyToFilterLambda =
new LinkedHashMap<ExpressionTree, LambdaExpressionTree>();

Expand All @@ -213,7 +214,11 @@ class RxNullabilityPropagator extends BaseNoOpHandler {

@Override
public void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol) {
NullAway analysis,
ClassTree tree,
VisitorState state,
Symbol.ClassSymbol classSymbol,
Config config) {
// Clear compilation unit specific state
this.filterMethodOrLambdaSet.clear();
this.observableOuterCallInChain.clear();
Expand Down