/** * Creates an object of the specified type. * @param type the class to instantiate * @param parameters the constructor parameters * @param <T> the class of the object to instantiate * @return an object of the specified class */ public static <T> T newInstance(Class<T> type, Object[] parameters) { return newInstance(type, true, parameters); }
/** * writes the current file field content to the property. */ void update() { if (!locked) { locked = true; File file = getFile(); if (!NullSafeComparator.equals(file, BeanUtil.getPropertyValue(bean, propertyName))) BeanUtil.setPropertyValue(bean, propertyName, file); locked = false; } }
private static <T> Comparator<T> createComparator(String className) { Class<Comparator<T>> cls = BeanUtil.forName(className); Comparator<T> comparator = BeanUtil.newInstance(cls); Type[] genTypes = BeanUtil.getGenericInterfaceParams(cls, Comparator.class); addComparator((Class<T>) genTypes[0], comparator); return comparator; }
/** * Sets a static attribute value of a class. * @param objectType the class to modify * @param fieldName the name of the attribute to set * @param value the value to assign to the field */ public static void setStaticAttributeValue(Class<?> objectType, String fieldName, Object value) { Field field = getField(objectType, fieldName); setAttributeValue(null, field, value); }
/** * Instantiates a class by the default constructor. * @param className the name of the class to instantiate * @return an instance of the class */ public static Object newInstance(String className) { Class<?> type = BeanUtil.forName(className); return newInstanceFromDefaultConstructor(type); }
@SuppressWarnings("rawtypes") public static Object invoke(boolean strict, Object target, String methodName, Object ... args) { if (target == null) throw new IllegalArgumentException("target is null"); Class[] argTypes = getTypes(args); Method method; if (target instanceof Class) method = getMethod((Class) target, methodName, argTypes); else method = getMethod(target.getClass(), methodName, argTypes); return invoke(target, method, strict, args); }
@Override public DataContainer<E> next(DataContainer<E> container) { DataContainer<String[]> wrapper = nextOfSource(); if (wrapper == null) return null; String[] cells = wrapper.getData(); E result = BeanUtil.newInstance(beanClass); for (int i = 0; i < columnDescriptors.length; i++) BeanUtil.setPropertyValue(result, columnDescriptors[i].getName(), cells[i], true, true); return container.setData(result); }
public static Object setFeatureDefault(Object target, String featureName) { // try JavaBean property PropertyDescriptor propertyDescriptor = BeanUtil.getPropertyDescriptor(target.getClass(), featureName); if (propertyDescriptor != null) { try { Object value = BeanUtil.newInstance(propertyDescriptor.getPropertyType()); Method writeMethod = propertyDescriptor.getWriteMethod(); writeMethod.invoke(target, value); return value; } catch (Exception e) { throw new ConfigurationError("Unable to write feature '" + featureName + "'", e); } } else { // try attribute Class<?> type = ((target instanceof Class) ? (Class<?>) target : target.getClass()); Field field = BeanUtil.getField(type, featureName); if (field != null) { Object value = BeanUtil.newInstance(field.getType()); BeanUtil.setAttributeValue(target, field, value); return value; } else { throw new ConfigurationError("Feature '" + featureName + "' not found in class " + type.getName()); } } }
else { Method genericGetMethod = BeanUtil.findMethod(target.getClass(), "get", String.class); if (genericGetMethod != null) return BeanUtil.invoke(target, genericGetMethod, new Object[] { featureName }); PropertyDescriptor propertyDescriptor = BeanUtil.getPropertyDescriptor(target.getClass(), featureName); if (propertyDescriptor != null && propertyDescriptor.getReadMethod() != null) { try { Field field = BeanUtil.getField(type, featureName); if (field != null) return BeanUtil.getFieldValue(field, target, false);
@SuppressWarnings({ "unchecked", "rawtypes" }) private static <C, V> void setLocal(C target, String featureName, V value, boolean required, boolean autoConvert) { if (BeanUtil.hasWriteableProperty(target.getClass(), featureName)) BeanUtil.setPropertyValue(target, featureName, value, required, autoConvert); else if (target instanceof Context) ((Context) target).set(featureName, value); else { Method genericSetMethod = BeanUtil.findMethod(target.getClass(), "set", String.class, Object.class); if (genericSetMethod != null) { BeanUtil.invoke(target, genericSetMethod, true, new Object[] { featureName, value }); return;
/** * Invokes a method on a bean. * @param target the object on which to invoke the mthod * @param methodName the name of the method * @param args the arguments to provide to the method * @return the invoked method's return value. */ public static Object invoke(Object target, String methodName, Object ... args) { return invoke(true, target, methodName, args); }
/** * Copies a Map's values to the properties of a JavaBean, * using the Map entries' key values as bean property names. * @param sourceBean the bean from which to read the properties * @param targetBean the bean on which to set the properties */ public static void copyPropertyValues(Object sourceBean, Object targetBean) { PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(sourceBean.getClass()); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String name = propertyDescriptor.getName(); if (propertyDescriptor.getReadMethod() != null) { Object value = getPropertyValue(sourceBean, propertyDescriptor); setPropertyValue(targetBean, name, value, false, true); } } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T newInstance(Class<T> type, boolean strict, Object[] parameters) { if (parameters == null || parameters.length == 0) return newInstanceFromDefaultConstructor(type); Constructor<T> constructorToUse = null; try { paramTypes[i] = parameters[i].getClass(); for (Constructor c : type.getConstructors()) { if (typesMatch(c.getParameterTypes(), paramTypes)) { constructorToUse = c; break; for (Constructor<T> candidate : candidates) { try { return newInstance(candidate, strict, parameters); } catch (Exception e) { mostRecentException = e; parameters = convertArray(parameters, constructorToUse.getParameterTypes()); return newInstance(constructorToUse, parameters); } catch (SecurityException e) { throw ExceptionMapper.configurationException(e, constructorToUse);
public static void configureApplication(JavaApplication application) { // Get OSX Application Class<?> applicationClass = BeanUtil.forName("com.apple.eawt.Application"); Object osxApplication = BeanUtil.invokeStatic(applicationClass, "getApplication"); if (application.supportsPreferences()) BeanUtil.invoke(osxApplication, "setEnabledPreferencesMenu", true); // add ApplicationListener Class<?> applicationListenerClass = BeanUtil.forName("com.apple.eawt.ApplicationListener"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Object osxAdapterProxy = Proxy.newProxyInstance( classLoader, new Class[] { applicationListenerClass }, new OSXInvocationHandler(application)); BeanUtil.invoke(osxApplication, "addApplicationListener", new Object[] { osxAdapterProxy }); // set dock icon image String iconPath = application.iconPath(); if (iconPath != null) { try { InputStream icon = ClassLoader.getSystemResourceAsStream(iconPath); BeanUtil.invoke(osxApplication, "setDockIconImage", ImageIO.read(icon)); } catch (IOException e) { // ignore errors } } }
@SuppressWarnings("unchecked") @Override public DataContainer<E> next(DataContainer<E> wrapper) { int i = 0; String value = null; try { DataContainer<String[]> tmp = nextRaw(dataContainer.get()); if (tmp == null) return null; String[] line = tmp.getData(); if (line.length == 0) return null; Class<E> beanClass = (classIndex >= 0 ? (Class<E>) BeanUtil.forName(line[classIndex]) : type); E bean = BeanUtil.newInstance(beanClass); int columns = Math.min(line.length, mutators.length); for (i = 0; i < columns; i++) { if (i != classIndex) { value = line[i]; if (value != null && value.length() == 0) value = emptyValue; mutators[i].setValue(bean, value); } } return wrapper.setData(bean); } catch (Exception e) { e.printStackTrace(); throw new ConfigurationError("Failed to set property '" + mutators[i].getName() + "' to '" + value + "' on class " + type, e); } }
public static void mapAttributesToProperties(Element element, Object bean, boolean unescape, Converter<String, String> nameNormalizer) { for (Map.Entry<String, String> attribute : getAttributes(element).entrySet()) { String name = StringUtil.lastToken(attribute.getKey(), ':'); name = nameNormalizer.convert(name); String value = attribute.getValue(); if (unescape) value = StringUtil.unescape(value); Class<?> type = bean.getClass(); if (BeanUtil.hasProperty(type, name)) BeanUtil.setPropertyValue(bean, name, value, true, true); } }
@Override public Class convert(String className) throws ConversionException { return BeanUtil.forName(className); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { String methodName = method.getName(); Method localMethod = BeanUtil.findMethod(this.getClass(), methodName, method.getParameterTypes()); if (localMethod != null) return BeanUtil.invoke(this, localMethod, args); else { if ("setNull".equals(methodName) && args != null && args.length >= 2) params[(Integer) args[0] - 1] = null; else if (methodName.startsWith("set") && args != null && args.length >= 2 && args[0] instanceof Integer) params[(Integer) args[0] - 1] = args[1]; Object result = BeanUtil.invoke(realStatement, method, args); if (result instanceof ResultSet) result = DBUtil.createLoggingResultSet((ResultSet) result, (PreparedStatement) proxy); return result; } } catch (ConfigurationError e) { if (e.getCause() instanceof InvocationTargetException && e.getCause().getCause() instanceof SQLException) throw e.getCause().getCause(); else throw e; } }
public static Map<String, ?> getPropertyValues(Object bean, boolean includeClass) { Map<String, Object> result = new HashMap<String, Object>(); PropertyDescriptor[] descriptors = getPropertyDescriptors(bean.getClass()); for (PropertyDescriptor descriptor : descriptors) { String propertyName = descriptor.getName(); if (includeClass || !"class".equals(propertyName)) result.put(propertyName, getPropertyValue(bean, descriptor)); } return result; }
private static String[] getPropertyNames(Class<?> beanType) { PropertyDescriptor[] descriptors = BeanUtil.getPropertyDescriptors(beanType); String[] names = new String[descriptors.length]; for (int i = 0; i < descriptors.length; i++) names[i] = descriptors[i].getName(); return names; }