Skip to content

Commit

Permalink
Prohibit Throwables.propagate() (apache#7121)
Browse files Browse the repository at this point in the history
* Throw caught exception.

* Throw caught exceptions.

* Related checkstyle rule is added to prevent further bugs.

* RuntimeException() is used instead of Throwables.propagate().

* Missing import is added.

* Throwables are propogated if possible.

* Throwables are propogated if possible.

* Throwables are propogated if possible.

* Throwables are propogated if possible.

* * Checkstyle definition is improved.
* Throwables.propagate() usages are removed.

* Checkstyle pattern is changed for only scanning "Throwables.propagate(" instead of checking lookbehind.

* Throwable is kept before firing a Runtime Exception.

* Fix unused assignments.
  • Loading branch information
kamaci authored and leventov committed Mar 14, 2019
1 parent e113648 commit 7ada1c4
Show file tree
Hide file tree
Showing 229 changed files with 462 additions and 646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.druid.benchmark.datagen.BenchmarkDataGenerator;
Expand Down Expand Up @@ -442,7 +441,7 @@ public void tearDown()
}
catch (IOException e) {
log.warn(e, "Failed to tear down, temp dir was: %s", tmpDir);
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.druid.benchmark.datagen;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
Expand Down Expand Up @@ -170,7 +169,7 @@ public QueryableIndex generate(
return merged;
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
Expand Down Expand Up @@ -535,7 +534,7 @@ public void tearDown()
}
catch (IOException e) {
log.warn(e, "Failed to tear down, temp dir was: %s", tmpDir);
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions codestyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@
<property name="message" value="Use org.apache.druid.server.http.HttpMediaType#TEXT_PLAIN_UTF8 instead"/>
</module>

<module name="Regexp">
<property name="format" value='^Throwables.propagate\('/>
<property name="illegalPattern" value="true"/>
<property name="message" value="Throwables.propagate() shouldn't be used in new code"/>
</module>

<module name="PackageName">
<property name="format" value="^org.apache.druid.*$"/>
</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.collections;

import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Ordering;
import org.apache.druid.java.util.common.guava.Accumulator;
import org.apache.druid.java.util.common.guava.CloseQuietly;
Expand Down Expand Up @@ -116,7 +115,7 @@ public T accumulate(T accumulated, T in)
retVal.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
return null;
} else {
Expand Down Expand Up @@ -165,7 +164,7 @@ private <OutType> Yielder<OutType> makeYielder(
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
} else {
pQueue.add(yielder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.druid.collections;

import com.google.common.base.Throwables;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.logger.Logger;
import sun.misc.Cleaner;
Expand Down Expand Up @@ -137,7 +136,7 @@ private void decrement()
closer.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.common.config;

import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
import org.apache.druid.java.util.common.concurrent.ScheduledExecutors;
import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
Expand Down Expand Up @@ -159,10 +158,10 @@ public ConfigHolder<T> call()
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
catch (ExecutionException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
import org.apache.druid.audit.AuditEntry;
import org.apache.druid.audit.AuditInfo;
Expand Down Expand Up @@ -94,7 +93,7 @@ public byte[] serialize(T obj)
return jsonMapper.writeValueAsBytes(obj);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -105,7 +104,7 @@ public String serializeToString(T obj)
return jsonMapper.writeValueAsString(obj);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -120,7 +119,7 @@ public T deserialize(byte[] bytes)
return jsonMapper.readValue(bytes, clazz);
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
};
Expand All @@ -137,7 +136,7 @@ public byte[] serialize(T obj)
return jsonMapper.writeValueAsBytes(obj);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -148,7 +147,7 @@ public String serializeToString(T obj)
return jsonMapper.writeValueAsString(obj);
}
catch (JsonProcessingException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -163,7 +162,7 @@ public T deserialize(byte[] bytes)
return jsonMapper.readValue(bytes, clazz);
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.druid.common.config;

import com.google.common.base.Throwables;
import org.apache.druid.java.util.common.ISE;
import org.apache.logging.log4j.core.LifeCycle;
import org.apache.logging.log4j.core.util.Cancellable;
Expand Down Expand Up @@ -184,7 +183,7 @@ private synchronized State waitForTransition(State expected, State transition, l
}
}
catch (InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
return current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.data.input.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
Expand Down Expand Up @@ -82,7 +81,7 @@ public LineIterator next()
"Exception reading object[%s]",
object
);
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.data.input.impl.prefetch;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.logger.Logger;

Expand Down Expand Up @@ -224,7 +223,7 @@ private OpenedObject<T> openObjectFromLocal() throws IOException
}
}
catch (InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
final FetchedFile<T> maybeCached = cacheIfPossible(fetchedFile);
Expand Down Expand Up @@ -252,7 +251,7 @@ private OpenedObject<T> openObjectFromRemote() throws IOException
return new OpenedObject<>(cached);
}
catch (Exception e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
} else {
final T object = objects.get(nextFetchIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.ProvisionException;
Expand Down Expand Up @@ -167,7 +166,7 @@ public <T> T configurate(
}
}
catch (NoSuchFieldException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}

messages.add(StringUtils.format("%s - %s", path.toString(), violation.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.java.util.common;

import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
Expand Down Expand Up @@ -85,7 +84,7 @@ public static FileCopyResult retryCopy(
return new FileCopyResult(outFile);
}
catch (Exception e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static <T> T retry(
awaitNextRetry(e, messageOnRetry, nTry, maxRetries, nTry <= quietTries);
} else {
Throwables.propagateIfInstanceOf(e, Exception.class);
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.java.util.common;

import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.io.ByteSink;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -118,7 +117,7 @@ public static long retryCopy(
);
}
catch (Exception e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.java.util.common;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;

import javax.annotation.Nullable;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -80,7 +79,7 @@ public static String fromUtf8(final byte[] bytes)
}
catch (UnsupportedEncodingException e) {
// Should never happen
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -103,7 +102,7 @@ public static byte[] toUtf8(final String string)
}
catch (UnsupportedEncodingException e) {
// Should never happen
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.druid.java.util.common.concurrent;

import com.google.common.base.Throwables;
import org.apache.druid.java.util.common.lifecycle.Lifecycle;

import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void stop()
);
}
catch (Exception e) {
Throwables.propagate(e);
throw new RuntimeException(e);
}
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.apache.druid.java.util.common.guava;

import com.google.common.base.Throwables;

import java.io.Closeable;
import java.io.IOException;

Expand Down Expand Up @@ -93,7 +91,7 @@ public <OutType> Yielder<OutType> makeYielder(
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}

yielderYielder = yielderYielder.next(null);
Expand All @@ -114,7 +112,7 @@ private <OutType> Yielder<OutType> wrapYielder(
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}

return makeYielder(yielderYielder.next(null), nextInit, accumulator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.druid.java.util.common.guava;

import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Ordering;
import org.apache.druid.java.util.common.io.Closer;

Expand Down Expand Up @@ -110,7 +109,7 @@ private <OutType> Yielder<OutType> makeYielder(
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
} else {
pQueue.add(yielder);
Expand Down
Loading

0 comments on commit 7ada1c4

Please sign in to comment.