List of usage examples for java.beans PropertyEditor getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.pentaho.reporting.designer.core.util.table.ElementMetaDataTable.java
public TableCellEditor getCellEditor(final int row, final int viewColumn) { final int column = convertColumnIndexToModel(viewColumn); final Object value = getModel().getValueAt(row, column); if (value instanceof GroupingHeader) { return getDefaultEditor(GroupingHeader.class); }// ww w . jav a2 s.co m final ElementMetaDataTableModel model = (ElementMetaDataTableModel) getModel(); final PropertyEditor propertyEditor = model.getEditorForCell(row, column); final Class columnClass = model.getClassForCell(row, column); if (propertyEditor != null) { final String[] tags = propertyEditor.getTags(); if (columnClass.isArray()) { arrayCellEditor.setPropertyEditorType(propertyEditor.getClass()); } else if (tags == null || tags.length == 0) { propertyEditorCellEditor.setPropertyEditor(propertyEditor); return propertyEditorCellEditor; } else { taggedPropertyEditorCellEditor.setPropertyEditor(propertyEditor); return taggedPropertyEditorCellEditor; } } final TableColumn tableColumn = getColumnModel().getColumn(column); final TableCellEditor renderer = tableColumn.getCellEditor(); if (renderer != null) { return renderer; } if (columnClass.isArray()) { return arrayCellEditor; } final TableCellEditor editor = getDefaultEditor(columnClass); if (editor != null && logger.isTraceEnabled()) { logger.trace("Using preconfigured default editor for column class " + columnClass + ": " + editor); // NON-NLS } return editor; }
From source file:org.pentaho.reporting.libraries.designtime.swing.table.PropertyTable.java
public TableCellEditor getCellEditor(final int row, final int viewColumn) { final TableModel tableModel = getModel(); if (tableModel instanceof PropertyTableModel) { final PropertyTableModel model = (PropertyTableModel) getModel(); final int column = convertColumnIndexToModel(viewColumn); final PropertyEditor propertyEditor = model.getEditorForCell(row, column); final Class columnClass = model.getClassForCell(row, column); if (propertyEditor != null) { final String[] tags = propertyEditor.getTags(); if (columnClass.isArray()) { arrayCellEditor.setPropertyEditorType(propertyEditor.getClass()); } else if (tags == null || tags.length == 0) { propertyEditorCellEditor.setPropertyEditor(propertyEditor); return propertyEditorCellEditor; } else { taggedPropertyEditorCellEditor.setPropertyEditor(propertyEditor); return taggedPropertyEditorCellEditor; }/*from w w w.ja v a 2 s .c o m*/ } final TableColumn tableColumn = getColumnModel().getColumn(column); final TableCellEditor renderer = tableColumn.getCellEditor(); if (renderer != null) { return renderer; } if (columnClass.isArray()) { return arrayCellEditor; } final TableCellEditor editor = getDefaultEditor(columnClass); if (editor != null && logger.isTraceEnabled()) { logger.trace("Using preconfigured default editor for column class " + columnClass + ": " + editor); // NON-NLS } return editor; } return super.getCellEditor(row, viewColumn); }
From source file:org.springframework.beans.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * for the specified property./* w ww. j a v a2 s. c o m*/ * @param propertyName name of the property * @param oldValue the previous value, if available (may be {@code null}) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or {@code null} if not known, for example in case of a collection element) * @param typeDescriptor the descriptor for the target property or field * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ @SuppressWarnings("unchecked") @Nullable public <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException { // Custom editor for this type? PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName); ConversionFailedException conversionAttemptEx = null; // No custom editor but custom ConversionService specified? ConversionService conversionService = this.propertyEditorRegistry.getConversionService(); if (editor == null && conversionService != null && newValue != null && typeDescriptor != null) { TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue); if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) { try { return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor); } catch (ConversionFailedException ex) { // fallback to default conversion logic below conversionAttemptEx = ex; } } } Object convertedValue = newValue; // Value not of required type? if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) { if (typeDescriptor != null && requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) { TypeDescriptor elementTypeDesc = typeDescriptor.getElementTypeDescriptor(); if (elementTypeDesc != null) { Class<?> elementType = elementTypeDesc.getType(); if (Class.class == elementType || Enum.class.isAssignableFrom(elementType)) { convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue); } } } if (editor == null) { editor = findDefaultEditor(requiredType); } convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor); } boolean standardConversion = false; if (requiredType != null) { // Try to apply some standard type conversion rules if appropriate. if (convertedValue != null) { if (Object.class == requiredType) { return (T) convertedValue; } else if (requiredType.isArray()) { // Array required -> apply appropriate conversion of elements. if (convertedValue instanceof String && Enum.class.isAssignableFrom(requiredType.getComponentType())) { convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue); } return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType()); } else if (convertedValue instanceof Collection) { // Convert elements to target type, if determined. convertedValue = convertToTypedCollection((Collection<?>) convertedValue, propertyName, requiredType, typeDescriptor); standardConversion = true; } else if (convertedValue instanceof Map) { // Convert keys and values to respective target type, if determined. convertedValue = convertToTypedMap((Map<?, ?>) convertedValue, propertyName, requiredType, typeDescriptor); standardConversion = true; } if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) { convertedValue = Array.get(convertedValue, 0); standardConversion = true; } if (String.class == requiredType && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) { // We can stringify any primitive value... return (T) convertedValue.toString(); } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) { if (conversionAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) { try { Constructor<T> strCtor = requiredType.getConstructor(String.class); return BeanUtils.instantiateClass(strCtor, convertedValue); } catch (NoSuchMethodException ex) { // proceed with field lookup if (logger.isTraceEnabled()) { logger.trace("No String constructor found on type [" + requiredType.getName() + "]", ex); } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "Construction via String failed for type [" + requiredType.getName() + "]", ex); } } } String trimmedValue = ((String) convertedValue).trim(); if (requiredType.isEnum() && "".equals(trimmedValue)) { // It's an empty enum identifier: reset the enum value to null. return null; } convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue); standardConversion = true; } else if (convertedValue instanceof Number && Number.class.isAssignableFrom(requiredType)) { convertedValue = NumberUtils.convertNumberToTargetClass((Number) convertedValue, (Class<Number>) requiredType); standardConversion = true; } } else { // convertedValue == null if (requiredType == Optional.class) { convertedValue = Optional.empty(); } } if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) { if (conversionAttemptEx != null) { // Original exception from former ConversionService call above... throw conversionAttemptEx; } else if (conversionService != null && typeDescriptor != null) { // ConversionService not tried before, probably custom editor found // but editor couldn't produce the required type... TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue); if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) { return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor); } } // Definitely doesn't match: throw IllegalArgumentException/IllegalStateException StringBuilder msg = new StringBuilder(); msg.append("Cannot convert value of type '").append(ClassUtils.getDescriptiveType(newValue)); msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append("'"); if (propertyName != null) { msg.append(" for property '").append(propertyName).append("'"); } if (editor != null) { msg.append(": PropertyEditor [").append(editor.getClass().getName()) .append("] returned inappropriate value of type '") .append(ClassUtils.getDescriptiveType(convertedValue)).append("'"); throw new IllegalArgumentException(msg.toString()); } else { msg.append(": no matching editors or conversion strategy found"); throw new IllegalStateException(msg.toString()); } } } if (conversionAttemptEx != null) { if (editor == null && !standardConversion && requiredType != null && Object.class != requiredType) { throw conversionAttemptEx; } logger.debug("Original ConversionService attempt failed - ignored since " + "PropertyEditor based conversion eventually succeeded", conversionAttemptEx); } return (T) convertedValue; }
From source file:org.springframework.beans.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * using the given property editor.//from w w w . jav a 2s . co m * @param oldValue the previous value, if available (may be {@code null}) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or {@code null} if not known, for example in case of a collection element) * @param editor the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ @Nullable private Object doConvertValue(@Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<?> requiredType, @Nullable PropertyEditor editor) { Object convertedValue = newValue; if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { editor.setValue(convertedValue); Object newConvertedValue = editor.getValue(); if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } Object returnValue = convertedValue; if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (convertedValue instanceof String) { if (editor != null) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace( "Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; return doConvertTextValue(oldValue, newTextValue, editor); } else if (String.class == requiredType) { returnValue = convertedValue; } } return returnValue; }
From source file:org.springframework.beans.TypeConverterDelegate.java
/** * Convert the given text value using the given property editor. * @param oldValue the previous value, if available (may be {@code null}) * @param newTextValue the proposed text value * @param editor the PropertyEditor to use * @return the converted value//from w ww. j a va2 s .co m */ private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) { try { editor.setValue(oldValue); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } editor.setAsText(newTextValue); return editor.getValue(); }
From source file:org.springmodules.cache.provider.tangosol.CoherenceFacadeTests.java
public void testGetCachingModelEditor() { PropertyEditor editor = facade.getCachingModelEditor(); assertNotNull(editor);/* w w w . j a v a2s . c om*/ assertEquals(ReflectionCacheModelEditor.class, editor.getClass()); ReflectionCacheModelEditor modelEditor = (ReflectionCacheModelEditor) editor; assertEquals(CoherenceCachingModel.class, modelEditor.getCacheModelClass()); assertNull(modelEditor.getCacheModelPropertyEditors()); }
From source file:org.springmodules.cache.provider.tangosol.CoherenceFacadeTests.java
public void testGetFlushingModelEditor() { PropertyEditor editor = facade.getFlushingModelEditor(); assertNotNull(editor);/*from w ww . j a v a2 s . co m*/ assertEquals(ReflectionCacheModelEditor.class, editor.getClass()); ReflectionCacheModelEditor modelEditor = (ReflectionCacheModelEditor) editor; assertEquals(CoherenceFlushingModel.class, modelEditor.getCacheModelClass()); Map propertyEditors = modelEditor.getCacheModelPropertyEditors(); assertEquals(1, propertyEditors.size()); assertSame(StringArrayPropertyEditor.class, propertyEditors.get("cacheNames").getClass()); }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * for the specified property.// w w w . jav a 2 s . c om * @param propertyName name of the property * @param oldValue the previous value, if available (may be <code>null</code>) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or <code>null</code> if not known, for example in case of a collection element) * @param descriptor the JavaBeans descriptor for the property * @param methodParam the method parameter that is the target of the conversion * (may be <code>null</code>) * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ protected Object convertIfNecessary(String propertyName, Object oldValue, Object newValue, Class requiredType, PropertyDescriptor descriptor, MethodParameter methodParam) throws IllegalArgumentException { Object convertedValue = newValue; // Custom editor for this type? PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName); // Value not of required type? if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) { if (editor == null) { editor = findDefaultEditor(requiredType, descriptor); } convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor); } if (requiredType != null) { // Try to apply some standard type conversion rules if appropriate. if (convertedValue != null) { if (String.class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) { // We can stringify any primitive value... return convertedValue.toString(); } else if (requiredType.isArray()) { // Array required -> apply appropriate conversion of elements. return convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType()); } else if (convertedValue instanceof Collection && CollectionFactory.isApproximableCollectionType(requiredType)) { // Convert elements to target type, if determined. convertedValue = convertToTypedCollection((Collection) convertedValue, propertyName, methodParam); } else if (convertedValue instanceof Map && CollectionFactory.isApproximableMapType(requiredType)) { // Convert keys and values to respective target type, if determined. convertedValue = convertToTypedMap((Map) convertedValue, propertyName, methodParam); } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) { String strValue = ((String) convertedValue).trim(); if (JdkVersion.isAtLeastJava15() && requiredType.isEnum() && "".equals(strValue)) { // It's an empty enum identifier: reset the enum value to null. return null; } // Try field lookup as fallback: for JDK 1.5 enum or custom enum // with values defined as static fields. Resulting value still needs // to be checked, hence we don't return it right away. try { Field enumField = requiredType.getField(strValue); convertedValue = enumField.get(null); } catch (Exception ex) { if (logger.isTraceEnabled()) { logger.trace("Field [" + convertedValue + "] isn't an enum value", ex); } } } } if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) { // Definitely doesn't match: throw IllegalArgumentException. StringBuffer msg = new StringBuffer(); msg.append("Cannot convert value of type [").append(ClassUtils.getDescriptiveType(newValue)); msg.append("] to required type [").append(ClassUtils.getQualifiedName(requiredType)).append("]"); if (propertyName != null) { msg.append(" for property '" + propertyName + "'"); } if (editor != null) { msg.append( ": PropertyEditor [" + editor.getClass().getName() + "] returned inappropriate value"); } else { msg.append(": no matching editors or conversion strategy found"); } throw new IllegalArgumentException(msg.toString()); } } return convertedValue; }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Find a default editor for the given type. * @param requiredType the type to find an editor for * @param descriptor the JavaBeans descriptor for the property * @return the corresponding editor, or <code>null</code> if none *//*ww w .jav a 2 s. c om*/ protected PropertyEditor findDefaultEditor(Class requiredType, PropertyDescriptor descriptor) { PropertyEditor editor = null; if (descriptor != null) { if (JdkVersion.isAtLeastJava15()) { editor = descriptor.createPropertyEditor(this.targetObject); } else { Class editorClass = descriptor.getPropertyEditorClass(); if (editorClass != null) { editor = (PropertyEditor) BeanUtils.instantiateClass(editorClass); } } } if (editor == null && requiredType != null) { // No custom editor -> check BeanWrapperImpl's default editors. editor = (PropertyEditor) this.propertyEditorRegistry.getDefaultEditor(requiredType); if (editor == null && !String.class.equals(requiredType)) { // No BeanWrapper default editor -> check standard JavaBean editor. editor = BeanUtils.findEditorByConvention(requiredType); if (editor == null && !unknownEditorTypes.containsKey(requiredType)) { // Deprecated global PropertyEditorManager fallback... editor = PropertyEditorManager.findEditor(requiredType); if (editor == null) { // Regular case as of Spring 2.5 unknownEditorTypes.put(requiredType, Boolean.TRUE); } else { logger.warn("PropertyEditor [" + editor.getClass().getName() + "] found through deprecated global PropertyEditorManager fallback - " + "consider using a more isolated form of registration, e.g. on the BeanWrapper/BeanFactory!"); } } } } return editor; }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * using the given property editor.//from w w w . ja v a2 s. c o m * @param oldValue the previous value, if available (may be <code>null</code>) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or <code>null</code> if not known, for example in case of a collection element) * @param editor the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ protected Object doConvertValue(Object oldValue, Object newValue, Class requiredType, PropertyEditor editor) { Object convertedValue = newValue; boolean sharedEditor = false; if (editor != null) { sharedEditor = this.propertyEditorRegistry.isSharedEditor(editor); } if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { Object newConvertedValue = null; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } } else { // Unsynchronized access to non-shared editor instance. editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (editor != null && convertedValue instanceof String) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace("Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { return doConvertTextValue(oldValue, newTextValue, editor); } } else { // Unsynchronized access to non-shared editor instance. return doConvertTextValue(oldValue, newTextValue, editor); } } return convertedValue; }