@Override public <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName(), e)); return null; } } }
@Override public <C> C tryInstantiate(Class<C> r, Class<?> c) { try { return ClassGenericsUtil.tryInstantiate(r, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName(), e)); return null; } }
@Override public <C> C tryInstantiate(Class<C> r, Class<?> c) { try { return ClassGenericsUtil.tryInstantiate(r, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName(), e)); return null; } }
@Override public <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } } }
@Override public <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName(), e)); return null; } } }
@Override public <C> C tryInstantiate(Class<C> r, Class<?> c) { try { return ClassGenericsUtil.tryInstantiate(r, c, this); } catch(Exception e) { reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } }
/** * Try to instantiate a particular class. * * @param <C> return type * @param c Base class * @return class instance or null */ default <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { String msg = e.getMessage(); reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName() // + " " + (msg != null ? msg : e.getClass().getName()), e)); return null; } } }
/** * Try to instantiate a particular class. * * @param <C> return type * @param c Base class * @return class instance or null */ default <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { String msg = e.getMessage(); reportError(new InternalParameterizationErrors("Error instantiating internal class: " + c.getName() // + " " + (msg != null ? msg : e.getClass().getName()), e)); return null; } } }
@Override public <C> C tryInstantiate(Class<C> r, Class<?> c) { try { return ClassGenericsUtil.tryInstantiate(r, c, this); } catch(Exception e) { LOG.exception(e); reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } }
@Override public <C> C tryInstantiate(Class<C> c) { try { return ClassGenericsUtil.tryInstantiate(c, c, this); } catch(Exception e) { LOG.exception(e); reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e)); return null; } } }
/** * Collect and instantiate all visualizer factories. * * @param config Parameterization * @param filter Filter * @return List of all adapters found. */ private static <O> Collection<VisualizationProcessor> collectFactorys(MergedParameterization config, Pattern filter) { ArrayList<VisualizationProcessor> factories = new ArrayList<>(); for(Class<?> c : ELKIServiceRegistry.findAllImplementations(VisualizationProcessor.class)) { if(filter != null && !filter.matcher(c.getCanonicalName()).find()) { continue; } try { config.rewind(); VisualizationProcessor a = ClassGenericsUtil.tryInstantiate(VisualizationProcessor.class, c, config); factories.add(a); } catch(Throwable e) { if(LOG.isDebugging()) { LOG.exception("Error instantiating visualization processor " + c.getName(), e.getCause()); } else { LOG.warning("Error instantiating visualization processor " + c.getName() + ": " + e.getMessage()); } } } return factories; }
/** * Collect and instantiate all visualizer factories. * * @param config Parameterization * @param filter Filter * @return List of all adapters found. */ private static <O> Collection<VisualizationProcessor> collectFactorys(MergedParameterization config, Pattern filter) { ArrayList<VisualizationProcessor> factories = new ArrayList<>(); for(Class<?> c : ELKIServiceRegistry.findAllImplementations(VisualizationProcessor.class)) { if(filter != null && !filter.matcher(c.getCanonicalName()).find()) { continue; } try { config.rewind(); VisualizationProcessor a = ClassGenericsUtil.tryInstantiate(VisualizationProcessor.class, c, config); factories.add(a); } catch(Throwable e) { if(LOG.isDebugging()) { LOG.exception("Error instantiating visualization processor " + c.getName(), e.getCause()); } else { LOG.warning("Error instantiating visualization processor " + c.getName() + ": " + e.getMessage()); } } } return factories; }
@Override public List<C> instantiateClasses(Parameterization config) { if (instances == null) { // instantiateClasses will descend itself. instances = new ArrayList<>(super.instantiateClasses(config)); } else { Parameterization cfg = null; for (int i = 0; i < instances.size(); i++) { if (instances.get(i) == null) { Class<? extends C> cls = getValue().get(i); try { // Descend at most once, and only when needed if (cfg == null) { cfg = config.descend(this); } C instance = ClassGenericsUtil.tryInstantiate(restrictionClass, cls, cfg); instances.set(i, instance); } catch(Exception e) { config.reportError(new WrongParameterValueException(this, cls.getName(), e)); } } } } return new ArrayList<>(instances); } }
@Override public List<C> instantiateClasses(Parameterization config) { if(instances == null) { // instantiateClasses will descend itself. return instances = super.instantiateClasses(config); } Parameterization cfg = null; final List<Class<? extends C>> classes = getValue(); for(int i = 0; i < classes.size(); i++) { if(instances.get(i) == null) { Class<? extends C> cls = classes.get(i); try { // Descend at most once, and only when needed cfg = (cfg == null) ? config.descend(this) : cfg; instances.set(i, ClassGenericsUtil.tryInstantiate(restrictionClass, cls, cfg)); } catch(Exception e) { config.reportError(new WrongParameterValueException(this, cls.getName(), e.getMessage(), e)); } } } return instances; } }
@Override public List<C> instantiateClasses(Parameterization config) { if(instances == null) { // instantiateClasses will descend itself. return instances = super.instantiateClasses(config); } Parameterization cfg = null; final List<Class<? extends C>> classes = getValue(); for(int i = 0; i < classes.size(); i++) { if(instances.get(i) == null) { Class<? extends C> cls = classes.get(i); try { // Descend at most once, and only when needed cfg = (cfg == null) ? config.descend(this) : cfg; instances.set(i, ClassGenericsUtil.tryInstantiate(restrictionClass, cls, cfg)); } catch(Exception e) { config.reportError(new WrongParameterValueException(this, cls.getName(), e.getMessage(), e)); } } } return instances; } }
/** * Returns a list of new instances for the value (i.e., the class name) of * this class list parameter. The instances have the type of the restriction * class of this class list parameter. * <p> * If the Class for the class names is not found, the instantiation is tried * using the package of the restriction class as package of the class name. * * @param config Parameterization to use (if Parameterizable)) * @return a list of new instances for the value of this class list parameter */ public List<C> instantiateClasses(Parameterization config) { config = config.descend(this); List<C> instances = new ArrayList<>(); if(getValue() == null) { config.reportError(new UnspecifiedParameterException(this)); return instances; // empty list. } for(Class<? extends C> cls : getValue()) { // NOTE: There is a duplication of this code in ObjectListParameter - keep // in sync! try { instances.add(ClassGenericsUtil.tryInstantiate(restrictionClass, cls, config)); } catch(Exception e) { config.reportError(new WrongParameterValueException(this, cls.getName(), e.getMessage(), e)); } } return instances; }
/** * Returns a new instance for the value (i.e., the class name) of this class * parameter. The instance has the type of the restriction class of this class * parameter. * <p> * If the Class for the class name is not found, the instantiation is tried * using the package of the restriction class as package of the class name. * * @param config Parameterization to use (if Parameterizable)) * @return a new instance for the value of this class parameter */ public C instantiateClass(Parameterization config) { if(getValue() == null /* && !optionalParameter */) { config.reportError(new UnspecifiedParameterException(this)); return null; } try { config = config.descend(this); return ClassGenericsUtil.tryInstantiate(restrictionClass, getValue(), config); } catch(ClassInstantiationException e) { config.reportError(new WrongParameterValueException(this, getValue().getCanonicalName(), "Error instantiating class.", e)); return null; } }
/** * Force parameterization method. * * Please use this only in "runner" classes such as unit tests, since the * error handling is not very flexible. * * @param <C> Type * @param c Class to instantiate * @param config Parameters * @return Instance or throw an AbortException */ @SuppressWarnings("unchecked") public static <C> C parameterizeOrAbort(Class<?> c, Parameterization config) { try { C ret = tryInstantiate((Class<C>) c, c, config); if(ret == null) { throw new AbortException("Could not instantiate class. Check parameters."); } return ret; } catch(Exception e) { if (config.hasErrors()) { for (ParameterException err : config.getErrors()) { LOG.warning(err.toString()); } } throw new AbortException("Instantiation failed", e); } }
/** * Force parameterization method. * <p> * Please use this only in "runner" classes such as unit tests, since the * error handling is not very flexible. * * @param <C> Type * @param c Class to instantiate * @param config Parameters * @return Instance or throw an AbortException */ @SuppressWarnings("unchecked") public static <C> C parameterizeOrAbort(Class<?> c, Parameterization config) { try { C ret = tryInstantiate((Class<C>) c, c, config); if(ret == null) { throw new AbortException("Could not instantiate class. Check parameters."); } return ret; } catch(Exception e) { if(config.hasErrors()) { for(ParameterException err : config.getErrors()) { LOG.warning(err.toString()); } } throw e instanceof AbortException ? (AbortException) e : new AbortException("Instantiation failed", e); } }
/** * Force parameterization method. * <p> * Please use this only in "runner" classes such as unit tests, since the * error handling is not very flexible. * * @param <C> Type * @param c Class to instantiate * @param config Parameters * @return Instance or throw an AbortException */ @SuppressWarnings("unchecked") public static <C> C parameterizeOrAbort(Class<?> c, Parameterization config) { try { C ret = tryInstantiate((Class<C>) c, c, config); if(ret == null) { throw new AbortException("Could not instantiate class. Check parameters."); } return ret; } catch(Exception e) { if(config.hasErrors()) { for(ParameterException err : config.getErrors()) { LOG.warning(err.toString()); } } throw e instanceof AbortException ? (AbortException) e : new AbortException("Instantiation failed", e); } }