Skip to content

Commit

Permalink
[hotfix] Some Java 7 cleanups in InstantiationUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanEwen committed Aug 21, 2015
1 parent bac21bf commit d05d386
Showing 1 changed file with 5 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static <T> T instantiate(Class<T> clazz) {
try {
return clazz.newInstance();
}
catch (InstantiationException iex) {
catch (InstantiationException | IllegalAccessException iex) {
// check for the common problem causes
checkForInstantiation(clazz);

Expand All @@ -147,15 +147,6 @@ public static <T> T instantiate(Class<T> clazz) {
throw new RuntimeException("Could not instantiate type '" + clazz.getName() +
"' due to an unspecified exception: " + iex.getMessage(), iex);
}
catch (IllegalAccessException iaex) {
// check for the common problem causes
checkForInstantiation(clazz);

// here we are, if non of the common causes was the problem. then the error was
// most likely an exception in the constructor or field initialization
throw new RuntimeException("Could not instantiate type '" + clazz.getName() +
"' due to an unspecified exception: " + iaex.getMessage(), iaex);
}
catch (Throwable t) {
String message = t.getMessage();
throw new RuntimeException("Could not instantiate type '" + clazz.getName() +
Expand All @@ -172,9 +163,9 @@ public static <T> T instantiate(Class<T> clazz) {
*/
public static boolean hasPublicNullaryConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getConstructors();
for (int i = 0; i < constructors.length; i++) {
if (constructors[i].getParameterTypes().length == 0 &&
Modifier.isPublic(constructors[i].getModifiers())) {
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterTypes().length == 0 &&
Modifier.isPublic(constructor.getModifiers())) {
return true;
}
}
Expand Down Expand Up @@ -310,14 +301,10 @@ public static Object deserializeObject(byte[] bytes, ClassLoader cl) throws IOEx

public static byte[] serializeObject(Object o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

try {
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(o);
}
finally {
oos.close();
}

return baos.toByteArray();
}
Expand Down

0 comments on commit d05d386

Please sign in to comment.