Negative examples

FutureReturnValueIgnoredNegativeCases.java

/*
* Copyright 2017 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/packagecom.google.errorprone.bugpatterns.testdata;importstaticcom.google.common.util.concurrent.Futures.immediateFuture;importstaticcom.google.common.util.concurrent.MoreExecutors.directExecutor;importstaticorg.junit.Assert.assertThrows;importstaticorg.mockito.Mockito.doAnswer;importcom.google.common.base.Function;importcom.google.common.base.Functions;importcom.google.common.base.Preconditions;importcom.google.common.base.Predicate;importcom.google.common.base.Supplier;importcom.google.common.base.Ticker;importcom.google.common.reflect.AbstractInvocationHandler;importcom.google.common.util.concurrent.AbstractFuture;importcom.google.common.util.concurrent.AsyncFunction;importcom.google.common.util.concurrent.CheckedFuture;importcom.google.common.util.concurrent.Futures;importcom.google.common.util.concurrent.ListenableFuture;importcom.google.errorprone.annotations.CanIgnoreReturnValue;importio.netty.channel.ChannelFuture;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Arrays;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.concurrent.Callable;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorCompletionService;importjava.util.concurrent.ForkJoinTask;importjava.util.concurrent.Future;importjava.util.concurrent.FutureTask;importjava.util.concurrent.RecursiveAction;importjava.util.concurrent.ScheduledExecutorService;importorg.junit.function.ThrowingRunnable;importorg.mockito.invocation.InvocationOnMock;importorg.mockito.stubbing.Answer;/** */publicclassFutureReturnValueIgnoredNegativeCases{publicFutureReturnValueIgnoredNegativeCases(){}staticListenableFuture<Object>getFuture(){returnimmediateFuture(null);}interfaceCanIgnoreMethod{@CanIgnoreReturnValueFuture<Object>getFuture();}publicstaticclassCanIgnoreImplimplementsCanIgnoreMethod{@OverridepublicFuture<Object>getFuture(){returnnull;}}staticvoidcallIgnoredInterfaceMethod(){newCanIgnoreImpl().getFuture();}@CanIgnoreReturnValuestaticListenableFuture<Object>getFutureIgnore(){returnimmediateFuture(null);}staticvoidputInMap(){Map<Object,Future<?>>map=newHashMap<>();map.put(newObject(),immediateFuture(null));Mapmap2=newHashMap();map2.put(newObject(),immediateFuture(null));}staticvoidpreconditions()throwsNoSuchMethodException,InvocationTargetException,IllegalAccessException{Preconditions.checkNotNull(getFuture());Preconditions.checkNotNull(newObject());FutureReturnValueIgnoredNegativeCases.class.getDeclaredMethod("preconditions").invoke(null);}staticvoidcheckIgnore(){getFutureIgnore();}voidignoreForkJoinTaskFork(ForkJoinTask<?>t){t.fork();}voidignoreForkJoinTaskFork_subclass(RecursiveActiont){t.fork();}voidignoreExecutorCompletionServiceSubmit(ExecutorCompletionServices){s.submit(()->null);}voidignoreChannelFutureAddListener(ChannelFuturecf){cf.addListener((ChannelFuturef)->{});}voidignoreChannelFutureAddListeners(ChannelFuturecf){cf.addListeners((ChannelFuturef)->{},(ChannelFuturef)->{});}<V>ListenableFuture<V>ignoreVarArgs(Callable<V>combiner,Executorexecutor,ListenableFuture<?>...futures){returncombine(combiner,executor,Arrays.asList(futures));}publicstatic<V>ListenableFuture<V>combine(finalCallable<V>combiner,Executorexecutor,Iterable<?extendsListenableFuture<?>>futures){returnnull;}privatestaticfinalclassTypedClass<T>{ListenableFuture<Void>ignoreReturnTypeSetByInputFuture(Tinput){returnreturnsInputType(logAsyncInternal(input),0);}protectedListenableFuture<Void>logAsyncInternal(Trecord){returnnull;}<V>ListenableFuture<V>returnsInputType(ListenableFuture<V>future,finalintn){returnnull;}}publicstatic<T,EextendsException>CheckedFuture<List<T>,E>allAsList(CheckedFuture<?extendsT,E>...futures){returnasListHelper(Futures.allAsList(null,null),futures);}privatestatic<T,EextendsException>CheckedFuture<List<T>,E>asListHelper(ListenableFuture<List<T>>future,CheckedFuture<?extendsT,E>...originalFutures){returnasListHelper(future,Arrays.asList(originalFutures));}/**
* Helper method for Iterable allAsList and successfulAsList calls which special-cases empty
* iterables.
*
* @param future the future to make checked
* @param originalFutures the original futures
* @return a checked version of the future argument
*/privatestatic<T,EextendsException>CheckedFuture<List<T>,E>asListHelper(ListenableFuture<List<T>>future,Iterable<?extendsCheckedFuture<?extendsT,E>>originalFutures){Iterator<?extendsCheckedFuture<?extendsT,E>>iterator=originalFutures.iterator();if(!iterator.hasNext()){returnnull;}returnnull;}publicstaticclassRetryingFuture<T>extendsAbstractFuture<T>{/**
* Enables the user to receive notifications about state changes of a retrying future, and use
* them e.g. for logging.
*/publicinterfaceInterceptor<T>{}/** Creates a builder for {@link RetryingFuture} instances. */publicstaticBuilder<Object>builder(){returnnewBuilder<>();}/** A builder for {@link RetryingFuture} instances. */publicstaticfinalclassBuilder<T>{privateBuilder(){}/** Sets the {@link Executor} in which all tries and retries are performed. */@CanIgnoreReturnValuepublicBuilder<T>setExecutor(Executorexecutor){returnthis;}/**
* Sets the {@link ScheduledExecutorService} used for scheduling retries after delay. It will
* also be used for tries and retries if {@link #setExecutor(Executor)} is not called.
*/@CanIgnoreReturnValuepublicBuilder<T>setScheduledExecutorService(ScheduledExecutorServicescheduledExecutorService){returnthis;}public<UextendsT>Builder<U>setInterceptor(Interceptor<U>interceptor){// Safely limiting the kinds of RetryingFutures this builder can produce,// based on the type of the interceptor.@SuppressWarnings("unchecked")Builder<U>me=(Builder<U>)this;returnme;}publicBuilder<T>setTicker(Tickerticker){returnthis;}public<UextendsT>RetryingFuture<U>build(Supplier<?extendsListenableFuture<U>>futureSupplier,Predicate<?superException>shouldContinue){returnnewRetryingFuture<U>(futureSupplier,null,shouldContinue,null,// We need to maintain Java 7 compatibilitynull,null,null);}public<UextendsT>RetryingFuture<U>build(Supplier<?extendsListenableFuture<U>>futureSupplier,Objectstrategy,Predicate<?superException>shouldContinue){returnnewRetryingFuture<U>(futureSupplier,strategy,shouldContinue,null,// We need to maintain Java 7 compatibilitynull,null,null);}}RetryingFuture(Supplier<?extendsListenableFuture<T>>futureSupplier,Objectstrategy,Predicate<?superException>shouldContinue,Executorexecutor,ScheduledExecutorServicescheduledExecutorService,Tickerticker,finalInterceptor<?superT>interceptor){}publicstatic<T>RetryingFuture<T>retryingFuture(Supplier<?extendsListenableFuture<T>>futureSupplier,Objectstrategy,Predicate<?superException>shouldContinue,Executorexecutor,Interceptor<?superT>interceptor){returnbuilder().setInterceptor(interceptor).setExecutor(executor).build(futureSupplier,strategy,shouldContinue);}}privatestaticclassTypedObject<T>{public<OextendsObject>ListenableFuture<O>transformAndClose(Function<?superT,O>function,Executorexecutor){returnnull;}publicListenableFuture<T>close(){returntransformAndClose(Functions.identity(),directExecutor());}}privatestaticvoidmocking(){doAnswer(invocation->immediateFuture(null)).when(null);doAnswer(invocation->{returnimmediateFuture(null);}).when(null);doAnswer(newAnswer(){@OverridepublicObjectanswer(InvocationOnMockmock){returnimmediateFuture(null);}}).when(null);}privatestaticvoidthrowing(){assertThrows(RuntimeException.class,()->immediateFuture(null));assertThrows(RuntimeException.class,()->{immediateFuture(null);});assertThrows(RuntimeException.class,newThrowingRunnable(){@Overridepublicvoidrun()throwsThrowable{immediateFuture(null);}});}privatestaticAsyncFunction<String,String>provideAsyncFunction(){returnFutures::immediateFuture;}privatestaticRunnableprovideNonFutureInterface(){returnnewFutureTask(null);}privatestaticvoidinvocation(){newAbstractInvocationHandler(){@OverrideprotectedObjecthandleInvocation(Objecto,Methodmethod,Object[]params){returnimmediateFuture(null);}};}}