From d5104df453f5ade7736416d7e1c484800d86e2d4 Mon Sep 17 00:00:00 2001 From: cgoldfeder Date: Fri, 11 Nov 2016 07:35:06 -0800 Subject: [PATCH] Fix two ErrorProne warnings in TypeUtils 1) Prefer .getConstructor().newInstance() over .newInstance() because otherwise checked exceptions can be propagated from the constructor even though they aren't declared. 2) Use the type T in the parameters to instantiate(). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138874730 --- java/google/registry/model/index/ForeignKeyIndex.java | 5 ++++- java/google/registry/util/TypeUtils.java | 7 +++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/java/google/registry/model/index/ForeignKeyIndex.java b/java/google/registry/model/index/ForeignKeyIndex.java index 49a8f7934..e2fd1675a 100644 --- a/java/google/registry/model/index/ForeignKeyIndex.java +++ b/java/google/registry/model/index/ForeignKeyIndex.java @@ -103,7 +103,10 @@ public abstract class ForeignKeyIndex extends BackupGroup */ public static ForeignKeyIndex create( E resource, DateTime deletionTime) { - ForeignKeyIndex instance = instantiate(RESOURCE_CLASS_TO_FKI_CLASS.get(resource.getClass())); + @SuppressWarnings("unchecked") + Class> fkiClass = + (Class>) RESOURCE_CLASS_TO_FKI_CLASS.get(resource.getClass()); + ForeignKeyIndex instance = instantiate(fkiClass); instance.topReference = Key.create(resource); instance.foreignKey = resource.getForeignKey(); instance.deletionTime = deletionTime; diff --git a/java/google/registry/util/TypeUtils.java b/java/google/registry/util/TypeUtils.java index dc979dc63..86aa0655b 100644 --- a/java/google/registry/util/TypeUtils.java +++ b/java/google/registry/util/TypeUtils.java @@ -46,13 +46,12 @@ public class TypeUtils { } } - @SuppressWarnings("unchecked") - public static T instantiate(Class clazz) { + public static T instantiate(Class clazz) { checkArgument(Modifier.isPublic(clazz.getModifiers()), "AppEngine's custom security manager won't let us reflectively access non-public types"); try { - return (T) clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return clazz.getConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }