Skip to content

Commit

Permalink
Tolerate nulls in type matchers
Browse files Browse the repository at this point in the history
RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215458585
  • Loading branch information
cushon authored and epmjohnston committed Oct 2, 2018
1 parent ce83a47 commit f2e7d31
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.errorprone.suppliers.Suppliers.JAVA_LANG_BOOLEAN_TYPE;
import static com.google.errorprone.suppliers.Suppliers.STRING_TYPE;
import static com.google.errorprone.suppliers.Suppliers.typeFromClass;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.stripParentheses;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -64,7 +65,6 @@
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
Expand Down Expand Up @@ -458,7 +458,8 @@ public static <T extends Tree> Matcher<T> isArrayType() {
return new Matcher<T>() {
@Override
public boolean matches(Tree t, VisitorState state) {
return state.getTypes().isArray(((JCTree) t).type);
Type type = getType(t);
return type != null && state.getTypes().isArray(type);
}
};
}
Expand All @@ -468,8 +469,10 @@ public static <T extends Tree> Matcher<T> isPrimitiveArrayType() {
return new Matcher<T>() {
@Override
public boolean matches(Tree t, VisitorState state) {
Type type = ((JCTree) t).type;
return state.getTypes().isArray(type) && state.getTypes().elemtype(type).isPrimitive();
Type type = getType(t);
return type != null
&& state.getTypes().isArray(type)
&& state.getTypes().elemtype(type).isPrimitive();
}
};
}
Expand All @@ -479,7 +482,8 @@ public static <T extends Tree> Matcher<T> isPrimitiveType() {
return new Matcher<T>() {
@Override
public boolean matches(Tree t, VisitorState state) {
return ((JCTree) t).type.isPrimitive();
Type type = getType(t);
return type != null && type.isPrimitive();
}
};
}
Expand All @@ -489,7 +493,8 @@ public static <T extends Tree> Matcher<T> isPrimitiveOrVoidType() {
return new Matcher<T>() {
@Override
public boolean matches(T t, VisitorState state) {
return ((JCTree) t).type.isPrimitiveOrVoid();
Type type = getType(t);
return type != null && type.isPrimitiveOrVoid();
}
};
}
Expand All @@ -499,7 +504,8 @@ public static <T extends Tree> Matcher<T> isVoidType() {
return new Matcher<T>() {
@Override
public boolean matches(T t, VisitorState state) {
return state.getTypes().isSameType(((JCTree) t).type, state.getSymtab().voidType);
Type type = getType(t);
return type != null && state.getTypes().isSameType(type, state.getSymtab().voidType);
}
};
}
Expand All @@ -511,7 +517,8 @@ public static <T extends Tree> Matcher<T> isPrimitiveOrBoxedPrimitiveType() {
return new Matcher<T>() {
@Override
public boolean matches(Tree t, VisitorState state) {
return state.getTypes().unboxedTypeOrType(((JCTree) t).type).isPrimitive();
Type type = getType(t);
return type != null && state.getTypes().unboxedTypeOrType(type).isPrimitive();
}
};
}
Expand Down

0 comments on commit f2e7d31

Please sign in to comment.