/** * Create object value instance. * * @param clazz object class. */ ObjectValue(Converter converter, Class<?> clazz) { this(converter); this.clazz = clazz; this.instance = Classes.newInstance(clazz); }
/** * Helper method used to invoke concrete output stream handler method. Please note that given output stream is closed * after handler invocation, just before this method return. * * @param outputStream wrapped output stream instance. * @throws IOException any output stream exception is bubbled up. */ public void invokeHandler(OutputStream outputStream) throws IOException { @SuppressWarnings("unchecked") T t = OutputStream.class.equals(this.streamClass) ? (T)outputStream : Classes.newInstance(streamClass, outputStream); handle(t); t.flush(); t.close(); } }
/** * Create a new instance. Handy utility for hidden classes creation. Constructor accepting given arguments, if any, * must exists. * * @param className fully qualified class name, * @param arguments variable number of arguments to be passed to constructor. * @param <T> instance type. * @return newly created instance. * @throws NoSuchBeingException if class or constructor not found. */ @SuppressWarnings("unchecked") public static <T> T newInstance(String className, Object... arguments) { return (T)newInstance(Classes.forName(className), arguments); }
/** * Create new map of given type. * * @param type map type. * @param <T> map type. * @return newly created map. */ @SuppressWarnings("unchecked") public static <T extends Map<?, ?>> T newMap(Type type) { Class<?> implementation = MAPS.get(type); if(implementation == null) { throw new BugError("No registered implementation for map |%s|.", type); } return (T)newInstance(implementation); }
/** * Load service of requested interface using given class loader. Throws exception if service implementation not found * on run-time. * * @param serviceInterface service interface, * @param classLoader class loader. * @param <S> service type. * @return service instance. * @throws NoProviderException if service provider not found on run-time. */ public static <S> S loadService(Class<S> serviceInterface, ClassLoader classLoader) { Iterator<S> services = ServiceLoader.load(serviceInterface, classLoader).iterator(); if(services.hasNext()) { return services.next(); } // although not official, Android does not support ServiceLoader // there is no way to insert service descriptor files into apk META-INF // as an workaround uses a hard coded 'services' package to store service descriptors try { String serviceDescriptor = getResourceAsString("/services/" + serviceInterface.getName()); return newInstance(serviceDescriptor); } catch(Throwable e) { // log.dump("Service not found", e); } return null; }
private static Format createFormatter(String className) { Class<? extends Format> formatterClass = Classes.forName(className); if (Types.isKindOf(formatterClass, Format.class)) { return Classes.newInstance(formatterClass); } return null; } }
/** * Create an object instance of the requested type. If type is a {@link List} uses * {@link Classes#getListDefaultImplementation(Type)}. * * @param type object type. * @return newly created instance. */ public Object newInstance(Class<?> type) { if (Types.isKindOf(type, List.class)) { type = Classes.getListDefaultImplementation(type); } return Classes.newInstance(type); }
/** * Initialize internal interceptor instance for given interceptor class. If interceptor class is managed, instance can * be reused if instance scope is not {@link InstanceScope#LOCAL}. Otherwise a new interceptor instance is created. * * @param interceptorClass interceptor class, managed or plain Java class. */ @SuppressWarnings("unchecked") public InterceptedInvoker(Class<? extends Interceptor> interceptorClass) { if (container.isManagedClass(interceptorClass)) { interceptor = container.getInstance((Class<? super Interceptor>) interceptorClass); } else { interceptor = Classes.newInstance(interceptorClass); } }
/** * Create form object instance of requested type and initialized it from HTTP request. HTTP request content type should be * {@link ContentType#MULTIPART_FORM}. Field values are loaded in memory as object fields. If form contains streams they are * saved on temporary files. Be aware that temporary files are removed at JVM exit. * * @param httpRequest HTTP request carrying the multipart form, * @param type object instance type. * @throws IOException if HTTP request input stream read operation fails. */ public FormObject(HttpServletRequest httpRequest, Class<?> type) throws IOException { this(Classes.newInstance(type)); FormIterator formIterator = new FormIteratorImpl(httpRequest); formIterator.forEach(new FormHandler() { @Override protected void stream(String name, String fileName, String contentType, InputStream inputStream) throws Throwable { setValue(name, new UploadedFileImpl(name, fileName, contentType, Files.copy(inputStream))); } @Override protected void field(String name, String value) throws Throwable { setValue(name, value); } }); }
return newInstance(t.getRawType(), arguments); return (T)newInstance((Class<?>)type, arguments);
return Classes.newInstance(type);
/** * Register user defined converter associated to value type. Converter class should be instantiable, i.e. not * abstract, interface or void, must have no arguments constructor, even if private and its constructor should of * course execute without exception. Otherwise unchecked exception may arise as described on throws section. There are * no other constrains on value type class. * <p> * Note: it is caller responsibility to enforce proper bound, i.e. given converter class to properly handle requested * value type. Otherwise exception may throw when this particular converter is enacted. * * @param valueType value type class, * @param converterClass specialized converter class. * @throws BugError if converter class is not instantiable. * @throws NoSuchBeingException if converter class has no default constructor. * @throws InvocationException with target exception if converter class construction fails on its execution. */ public void registerConverter(Class<?> valueType, Class<? extends Converter> converterClass) { Converter converter = Classes.newInstance(converterClass); if(Types.isConcrete(valueType)) { registerConverterInstance(valueType, converter); } else { if(abstractConverters.put(valueType, converter) == null) { log.debug("Register abstract converter |%s| for value type |%s|.", converterClass, valueType); } else { log.warn("Override abstract converter |%s| for value type |%s|.", converterClass, valueType); } } }
Object object = Classes.newInstance(type); for (Parameter parameter : parameters) { Field field = Classes.getField(type, Strings.toMemberName(parameter.getName()));
/** * Create view instance based on view meta data identified by name and request locale. View instance is created from view * implementation class - {@link ViewMeta#getImplementation()}. * * @param viewName application unique view name. * @return newly create view instance. * @throws BugError if given view name does not designate an existing view. */ @Override public View getView(String viewName) { RequestContext context = Factory.getInstance(RequestContext.class); ViewMeta meta = viewsMetaPool.get(viewName, context.getLocale()); if (meta == null) { throw new BugError("View |%s| not found. View name may be misspelled, forgot to add template file or template name doesn't match views files pattern.", viewName); } AbstractView view = (AbstractView) Classes.newInstance(meta.getImplementation()); view.setMeta(meta); return view; } }