diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java')
-rw-r--r-- | src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java | 111 |
1 files changed, 49 insertions, 62 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java b/src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java index bdd9924f..90b5ac62 100644 --- a/src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java +++ b/src/de/lmu/ifi/dbs/elki/utilities/ClassGenericsUtil.java @@ -65,7 +65,7 @@ public final class ClassGenericsUtil { /** * Static logger to use. */ - private static final Logging logger = Logging.getLogger(ClassGenericsUtil.class); + private static final Logging LOG = Logging.getLogger(ClassGenericsUtil.class); /** * Class loader. @@ -78,6 +78,13 @@ public final class ClassGenericsUtil { public static final String FACTORY_METHOD_NAME = "parameterize"; /** + * Fake Constructor. Use static methods. + */ + private ClassGenericsUtil() { + // Do not instantiate + } + + /** * <p> * Returns a new instance of the given type for the specified className. * </p> @@ -99,22 +106,17 @@ public final class ClassGenericsUtil { try { try { instance = type.cast(loader.loadClass(className).newInstance()); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { // try package of type instance = type.cast(loader.loadClass(type.getPackage().getName() + "." + className).newInstance()); } - } - catch(InstantiationException e) { + } catch (InstantiationException e) { throw new UnableToComplyException(e); - } - catch(IllegalAccessException e) { + } catch (IllegalAccessException e) { throw new UnableToComplyException(e); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new UnableToComplyException(e); - } - catch(ClassCastException e) { + } catch (ClassCastException e) { throw new UnableToComplyException(e); } return instance; @@ -150,22 +152,17 @@ public final class ClassGenericsUtil { try { try { instance = ((Class<T>) type).cast(loader.loadClass(className).newInstance()); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { // try package of type instance = ((Class<T>) type).cast(loader.loadClass(type.getPackage().getName() + "." + className).newInstance()); } - } - catch(InstantiationException e) { + } catch (InstantiationException e) { throw new UnableToComplyException(e); - } - catch(IllegalAccessException e) { + } catch (IllegalAccessException e) { throw new UnableToComplyException(e); - } - catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new UnableToComplyException(e); - } - catch(ClassCastException e) { + } catch (ClassCastException e) { throw new UnableToComplyException(e); } return instance; @@ -184,15 +181,15 @@ public final class ClassGenericsUtil { * doesn't fit the constraints. * @throws Exception On other errors such as security exceptions */ - public static <C> Method getParameterizationFactoryMethod(Class<C> c, Class<?> ret) throws NoSuchMethodException, Exception { + public static <C> Method getParameterizationFactoryMethod(Class<C> c, Class<?> ret) throws NoSuchMethodException { Method m = c.getMethod(FACTORY_METHOD_NAME, Parameterization.class); - if(m == null) { + if (m == null) { throw new NoSuchMethodException("No parameterization method found."); } - if(!ret.isAssignableFrom(m.getReturnType())) { + if (!ret.isAssignableFrom(m.getReturnType())) { throw new NoSuchMethodException("Return type doesn't match: " + m.getReturnType().getName() + ", expected: " + ret.getName()); } - if(!java.lang.reflect.Modifier.isStatic(m.getModifiers())) { + if (!java.lang.reflect.Modifier.isStatic(m.getModifiers())) { throw new NoSuchMethodException("Factory method is not static."); } return m; @@ -205,13 +202,12 @@ public final class ClassGenericsUtil { * @return Parameterizer or null. */ public static Parameterizer getParameterizer(Class<?> c) { - for(Class<?> inner : c.getDeclaredClasses()) { - if(Parameterizer.class.isAssignableFrom(inner)) { + for (Class<?> inner : c.getDeclaredClasses()) { + if (Parameterizer.class.isAssignableFrom(inner)) { try { return inner.asSubclass(Parameterizer.class).newInstance(); - } - catch(Exception e) { - logger.warning("Non-usable Parameterizer in class: " + c.getName()); + } catch (Exception e) { + LOG.warning("Non-usable Parameterizer in class: " + c.getName()); } } } @@ -233,14 +229,14 @@ public final class ClassGenericsUtil { * @throws Exception when other instantiation errors occurred */ public static <C> C tryInstantiate(Class<C> r, Class<?> c, Parameterization config) throws InvocationTargetException, NoSuchMethodException, Exception { - if(c == null) { + if (c == null) { // TODO: better class? AbortException maybe? throw new UnsupportedOperationException("Trying to instantiate 'null' class!"); } // Try a V3 parameterization class Parameterizer par = getParameterizer(c); // TODO: API good? - if(par != null && par instanceof AbstractParameterizer) { + if (par instanceof AbstractParameterizer) { final Object instance = ((AbstractParameterizer) par).make(config); return r.cast(instance); } @@ -249,8 +245,7 @@ public final class ClassGenericsUtil { final Method factory = getParameterizationFactoryMethod(c, r); final Object instance = factory.invoke(null, config); return r.cast(instance); - } - catch(NoSuchMethodException e) { + } catch (NoSuchMethodException e) { // continue. } // Try a regular "parameterization" constructor @@ -258,8 +253,7 @@ public final class ClassGenericsUtil { final Constructor<?> constructor = c.getConstructor(Parameterization.class); final Object instance = constructor.newInstance(config); return r.cast(instance); - } - catch(NoSuchMethodException e) { + } catch (NoSuchMethodException e) { // continue } // Try a default constructor. @@ -282,8 +276,7 @@ public final class ClassGenericsUtil { public static <C> C parameterizeOrAbort(Class<?> c, Parameterization config) { try { return tryInstantiate((Class<C>) c, c, config); - } - catch(Exception e) { + } catch (Exception e) { throw new AbortException("Instantiation failed", e); } } @@ -328,10 +321,10 @@ public final class ClassGenericsUtil { * @param len array size * @return new array of ArrayLists */ - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> ArrayList<T>[] newArrayOfEmptyArrayList(int len) { - ArrayList<T>[] result = new ArrayList[len]; - for(int i = 0; i < len; i++) { + ArrayList[] result = new ArrayList[len]; + for (int i = 0; i < len; i++) { result[i] = new ArrayList<T>(); } return result; @@ -347,10 +340,10 @@ public final class ClassGenericsUtil { * @param len array size * @return new array of HashSets */ - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> HashSet<T>[] newArrayOfEmptyHashSet(int len) { - HashSet<T>[] result = new HashSet[len]; - for(int i = 0; i < len; i++) { + HashSet[] result = new HashSet[len]; + for (int i = 0; i < len; i++) { result[i] = new HashSet<T>(); } return result; @@ -405,8 +398,8 @@ public final class ClassGenericsUtil { */ @SuppressWarnings("unchecked") public static <BASE, FROM extends BASE, TO extends BASE> Class<TO> uglyCrossCast(Class<FROM> cls, Class<BASE> base) { - if(!base.isAssignableFrom(cls)) { - if(cls == null) { + if (!base.isAssignableFrom(cls)) { + if (cls == null) { throw new ClassCastException("Attempted to use 'null' as class."); } throw new ClassCastException(cls.getName() + " is not a superclass of " + base); @@ -431,8 +424,7 @@ public final class ClassGenericsUtil { public static <B, T extends B> T castWithGenericsOrNull(Class<B> base, Object obj) { try { return (T) base.cast(obj); - } - catch(ClassCastException e) { + } catch (ClassCastException e) { return null; } } @@ -453,8 +445,7 @@ public final class ClassGenericsUtil { try { Object n = obj.getClass().getConstructor().newInstance(); return (T) n; - } - catch(NullPointerException e) { + } catch (NullPointerException e) { throw new IllegalArgumentException("Null pointer exception in newInstance()", e); } } @@ -482,7 +473,7 @@ public final class ClassGenericsUtil { */ @SuppressWarnings("unchecked") public static <T> T[] newArray(Class<? extends T> k, int size) { - if(k.isPrimitive()) { + if (k.isPrimitive()) { throw new IllegalArgumentException("Argument cannot be primitive: " + k); } Object a = java.lang.reflect.Array.newInstance(k, size); @@ -514,17 +505,13 @@ public final class ClassGenericsUtil { C copy = newInstance(coll); copy.addAll(coll); return copy; - } - catch(InstantiationException e) { + } catch (InstantiationException e) { throw new RuntimeException(e); - } - catch(IllegalAccessException e) { + } catch (IllegalAccessException e) { throw new RuntimeException(e); - } - catch(InvocationTargetException e) { + } catch (InvocationTargetException e) { throw new RuntimeException(e); - } - catch(NoSuchMethodException e) { + } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } @@ -538,15 +525,15 @@ public final class ClassGenericsUtil { * @return new array containing the collection elements */ public static <T> T[] collectionToArray(Collection<T> c, T[] a) { - if(a.length < c.size()) { + if (a.length < c.size()) { a = newArray(a, c.size()); } int i = 0; - for(T x : c) { + for (T x : c) { a[i] = x; i++; } - if(i < a.length) { + if (i < a.length) { a[i] = null; } return a; |