From 0adcd8e8e3ce2517558a919b44ba2c6ab7991503 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Sat, 13 Mar 2021 15:55:08 -0800 Subject: Comply with nullness analysis Startblock: unknown commit is submitted unknown commit is submitted unknown commit is submitted PiperOrigin-RevId: 362735853 --- java/com/google/turbine/binder/Processing.java | 21 ++++++++++++++------- .../turbine/binder/bytecode/BytecodeBoundClass.java | 8 ++++++++ .../turbine/options/TurbineOptionsParser.java | 6 +++--- .../turbine/processing/TurbineAnnotationProxy.java | 6 ++++-- .../com/google/turbine/processing/TurbineFiler.java | 3 ++- 5 files changed, 31 insertions(+), 13 deletions(-) (limited to 'java') diff --git a/java/com/google/turbine/binder/Processing.java b/java/com/google/turbine/binder/Processing.java index f4700b3..d48b800 100644 --- a/java/com/google/turbine/binder/Processing.java +++ b/java/com/google/turbine/binder/Processing.java @@ -58,7 +58,6 @@ import java.nio.file.Paths; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -139,10 +138,8 @@ public class Processing { } } - Map wanted = new HashMap<>(); - for (Processor processor : processorInfo.processors()) { - wanted.put(processor, SupportedAnnotationTypes.create(processor)); - } + ImmutableMap wanted = + initializeSupportedAnnotationTypes(processorInfo); Set allSymbols = new HashSet<>(); @@ -162,9 +159,10 @@ public class Processing { } ImmutableSetMultimap allAnnotations = getAllAnnotations(env, syms); TurbineRoundEnvironment roundEnv = null; - for (Processor processor : processorInfo.processors()) { + for (Map.Entry e : wanted.entrySet()) { + Processor processor = e.getKey(); + SupportedAnnotationTypes supportedAnnotationTypes = e.getValue(); Set annotations = new HashSet<>(); - SupportedAnnotationTypes supportedAnnotationTypes = wanted.get(processor); boolean run = supportedAnnotationTypes.everything() || toRun.contains(processor); for (ClassSymbol a : allAnnotations.keys()) { if (supportedAnnotationTypes.everything() @@ -271,6 +269,15 @@ public class Processing { return result; } + private static ImmutableMap + initializeSupportedAnnotationTypes(ProcessorInfo processorInfo) { + ImmutableMap.Builder result = ImmutableMap.builder(); + for (Processor processor : processorInfo.processors()) { + result.put(processor, SupportedAnnotationTypes.create(processor)); + } + return result.build(); + } + @AutoValue abstract static class SupportedAnnotationTypes { diff --git a/java/com/google/turbine/binder/bytecode/BytecodeBoundClass.java b/java/com/google/turbine/binder/bytecode/BytecodeBoundClass.java index 8939780..a3be4f0 100644 --- a/java/com/google/turbine/binder/bytecode/BytecodeBoundClass.java +++ b/java/com/google/turbine/binder/bytecode/BytecodeBoundClass.java @@ -18,6 +18,7 @@ package com.google.turbine.binder.bytecode; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Verify.verify; +import static java.util.Objects.requireNonNull; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; @@ -512,6 +513,9 @@ public class BytecodeBoundClass implements TypeBoundClass { private static RetentionPolicy bindRetention(AnnotationInfo annotation) { ElementValue val = annotation.elementValuePairs().get("value"); + if (val == null) { + return null; + } if (val.kind() != Kind.ENUM) { return null; } @@ -525,6 +529,7 @@ public class BytecodeBoundClass implements TypeBoundClass { private static ImmutableSet bindTarget(AnnotationInfo annotation) { ImmutableSet.Builder result = ImmutableSet.builder(); ElementValue val = annotation.elementValuePairs().get("value"); + requireNonNull(val); switch (val.kind()) { case ARRAY: for (ElementValue element : ((ArrayValue) val).elements()) { @@ -551,6 +556,9 @@ public class BytecodeBoundClass implements TypeBoundClass { private static ClassSymbol bindRepeatable(AnnotationInfo annotation) { ElementValue val = annotation.elementValuePairs().get("value"); + if (val == null) { + return null; + } switch (val.kind()) { case CLASS: String className = ((ConstTurbineClassValue) val).className(); diff --git a/java/com/google/turbine/options/TurbineOptionsParser.java b/java/com/google/turbine/options/TurbineOptionsParser.java index f2bd5e0..e710bb7 100644 --- a/java/com/google/turbine/options/TurbineOptionsParser.java +++ b/java/com/google/turbine/options/TurbineOptionsParser.java @@ -203,7 +203,7 @@ public final class TurbineOptionsParser { /** Returns the value of an option, or {@code null}. */ @Nullable private static String readOne(Deque argumentDeque) { - if (argumentDeque.isEmpty() || argumentDeque.peekFirst().startsWith("-")) { + if (argumentDeque.isEmpty() || argumentDeque.getFirst().startsWith("-")) { return null; } return argumentDeque.pollFirst(); @@ -212,7 +212,7 @@ public final class TurbineOptionsParser { /** Returns a list of option values. */ private static ImmutableList readList(Deque argumentDeque) { ImmutableList.Builder result = ImmutableList.builder(); - while (!argumentDeque.isEmpty() && !argumentDeque.peekFirst().startsWith("--")) { + while (!argumentDeque.isEmpty() && !argumentDeque.getFirst().startsWith("--")) { result.add(argumentDeque.pollFirst()); } return result.build(); @@ -225,7 +225,7 @@ public final class TurbineOptionsParser { private static ImmutableList readJavacopts(Deque argumentDeque) { ImmutableList.Builder result = ImmutableList.builder(); while (!argumentDeque.isEmpty()) { - String arg = argumentDeque.pollFirst(); + String arg = argumentDeque.removeFirst(); if (arg.equals("--")) { return result.build(); } diff --git a/java/com/google/turbine/processing/TurbineAnnotationProxy.java b/java/com/google/turbine/processing/TurbineAnnotationProxy.java index c39f310..967ead9 100644 --- a/java/com/google/turbine/processing/TurbineAnnotationProxy.java +++ b/java/com/google/turbine/processing/TurbineAnnotationProxy.java @@ -17,6 +17,7 @@ package com.google.turbine.processing; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; import com.google.turbine.binder.bound.EnumConstantValue; import com.google.turbine.binder.bound.TurbineAnnotationValue; @@ -131,14 +132,15 @@ class TurbineAnnotationProxy implements InvocationHandler { private static Object constArrayValue( Class returnType, ModelFactory factory, ClassLoader loader, ArrayInitValue value) { - if (returnType.getComponentType().equals(Class.class)) { + Class componentType = requireNonNull(returnType.getComponentType()); + if (componentType.equals(Class.class)) { List result = new ArrayList<>(); for (Const element : value.elements()) { result.add(factory.asTypeMirror(((TurbineClassValue) element).type())); } throw new MirroredTypesException(result); } - Object result = Array.newInstance(returnType.getComponentType(), value.elements().size()); + Object result = Array.newInstance(componentType, value.elements().size()); int idx = 0; for (Const element : value.elements()) { Object v = constValue(returnType, factory, loader, element); diff --git a/java/com/google/turbine/processing/TurbineFiler.java b/java/com/google/turbine/processing/TurbineFiler.java index 186eb7f..45cdc22 100644 --- a/java/com/google/turbine/processing/TurbineFiler.java +++ b/java/com/google/turbine/processing/TurbineFiler.java @@ -18,6 +18,7 @@ package com.google.turbine.processing; import static com.google.common.base.Preconditions.checkArgument; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Objects.requireNonNull; import com.google.common.base.Function; import com.google.common.base.Supplier; @@ -361,7 +362,7 @@ public class TurbineFiler implements Filer { @Override public URI toUri() { try { - return loader.getResource(path).toURI(); + return requireNonNull(loader.getResource(path)).toURI(); } catch (URISyntaxException e) { throw new AssertionError(e); } -- cgit v1.2.3