Search in sources :

Example 1 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class OutFieldsTableModel method validateJavaTypeColumn.

/**
 * Validate java type in the given row.
 * @param row the row to check
 * @return error message if error occurs
 */
@SuppressWarnings("rawtypes")
private String validateJavaTypeColumn(final int row) {
    Object value = this.getValueAt(row, Column.JAVA_TYPE);
    if (null == value) {
        return "Please select a value";
    }
    if (value instanceof JavaToDataCellConverterFactory) {
        if (getValueAt(row, Column.FIELD_TYPE) == FieldType.FlowVariable) {
            return "Cannot use DataCell converters for flow variables.";
        }
    } else if (value instanceof String) {
        final String id = (String) value;
        final Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
        return factory.isPresent() ? null : "Converter factory " + id + " was not found.";
    } else if (value instanceof Class) {
        Class javaType = (Class) value;
        Object outType = getValueAt(row, Column.DATA_TYPE);
        if (outType instanceof DataType) {
            DataType elemType = (DataType) outType;
            boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
            DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
            final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType);
            if (factories.isEmpty()) {
                return "The java type \"" + javaType.getSimpleName() + "\" is not supported for output columns.";
            }
            if (dataType.isCollectionType() && !javaType.isArray()) {
                return "Please choose an java array for collection types.";
            }
            if (!dataType.isCollectionType() && javaType.isArray()) {
                return "An array cannot be written to a non collection column";
            }
        } else if (outType instanceof Type) {
            Type type = (Type) outType;
            TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
            if (!typeConversion.canProvideJavaType(javaType)) {
                return "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
            }
        }
    } else {
        return "Cannot find class " + value.toString();
    }
    // no error found
    return null;
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 2 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class OutFieldsTableModel method setValueAt.

/**
 * {@inheritDoc}
 */
@Override
public void setValueAt(final Object aValue, final int row, final int column) {
    // make sure setValue(Object, int, Column) is always called.
    Column col = getColumnForIndex(column);
    final FieldType fieldType = (m_flowVarsOnly) ? FieldType.FlowVariable : (FieldType) getValueAt(row, Column.FIELD_TYPE);
    if (col == Column.COLUMN) {
        if (aValue instanceof FlowVariable) {
            // make sure we do not keep a ConverterFactory in the JavaType column
            // when changing from DataColumnSpec.
            Object type = getValueAt(row, Column.JAVA_TYPE);
            if (type instanceof JavaToDataCellConverterFactory) {
                // set java type to dest type of converter factory
                setValueAt(((JavaToDataCellConverterFactory<?>) type).getSourceType(), row, Column.JAVA_TYPE);
            }
        } else if (aValue instanceof DataColumnSpec) {
            // make sure we do not keep a Java class in the JavaType column for Columns
            final Object type = getValueAt(row, Column.JAVA_TYPE);
            final DataType dataType = (DataType) getValueAt(row, Column.DATA_TYPE);
            if (type instanceof Class) {
                // find a DataCell converter which is able to convert from the new column type to the current java type
                final Optional<?> factory = ConverterUtil.getConverterFactory((Class<?>) type, dataType);
                if (factory.isPresent()) {
                    setValueAt(factory.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    } else if (fieldType == FieldType.Column && col == Column.DATA_TYPE) {
        Object type = getValueAt(row, Column.JAVA_TYPE);
        Boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
        if (isCollection != null && type instanceof JavaToDataCellConverterFactory && aValue instanceof DataType) {
            final JavaToDataCellConverterFactory<?> converterFactory = (JavaToDataCellConverterFactory<?>) type;
            DataType dataType = (DataType) aValue;
            Class<?> javaType = converterFactory.getSourceType();
            if (isCollection && !dataType.isCollectionType()) {
                dataType = ListCell.getCollectionType(dataType);
                if (!(converterFactory instanceof ArrayToCollectionConverterFactory)) {
                    javaType = Array.newInstance(javaType, 0).getClass();
                }
            } else if (!isCollection && javaType.isArray()) {
                if (converterFactory instanceof ArrayToCollectionConverterFactory) {
                    javaType = javaType.getComponentType();
                }
            }
            // Try to find a converter factory which converts the newly selected DataType to the selected JavaType.
            final Optional<?> factory = ConverterUtil.getConverterFactory(javaType, dataType);
            if (factory.isPresent()) {
                setValueAt(factory.get(), row, Column.JAVA_TYPE);
            } else {
                // If there is no way to convert to the existing Java type, use the preferred type instead.
                final Optional<JavaToDataCellConverterFactory<?>> preferred = ConverterUtil.getPreferredFactoryForDestinationType(dataType);
                if (preferred.isPresent()) {
                    setValueAt(preferred.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    }
    super.setValueAt(aValue, row, column);
    /* If this was the collection check box, post process to set the appropriate converter factory. */
    if (col == Column.IS_COLLECTION) {
        Object type = getValueAt(row, Column.DATA_TYPE);
        if (type != null) {
            // make sure DataType is updated to a Collection type or back to a single element type
            setValueAt(type, row, getIndex(Column.DATA_TYPE));
        }
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) FlowVariable(org.knime.core.node.workflow.FlowVariable) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 3 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class OutFieldsTableModel method getAllowedJavaTypes.

/**
 * {@inheritDoc}
 */
@Override
public Object[] getAllowedJavaTypes(final int row) {
    Object input = getValueAt(row, Column.DATA_TYPE);
    if (input instanceof DataType) {
        DataType elemType = (DataType) input;
        boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
        final DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
        final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).collect(Collectors.toList());
        return factories.toArray(new Object[factories.size()]);
    } else if (input instanceof Type) {
        Type type = (Type) input;
        TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
        return typeConversion.canCreatedFromJavaTypes();
    } else {
        return new Class[] { String.class };
    }
}
Also used : Array(java.lang.reflect.Array) ListCell(org.knime.core.data.collection.ListCell) Collection(java.util.Collection) FlowVariable(org.knime.core.node.workflow.FlowVariable) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) Type(org.knime.core.node.workflow.FlowVariable.Type) JavaSnippet(org.knime.base.node.jsnippet.JavaSnippet) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) DataColumnSpec(org.knime.core.data.DataColumnSpec) Map(java.util.Map) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) DataType(org.knime.core.data.DataType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 4 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class ConverterFactoryJavaTypeListCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
    // The super method will reset the icon if we call this method
    // last. So we let super do its job first and then we take care
    // that everything is properly set.
    final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    assert c == this;
    if (value instanceof String) {
        setText((String) value);
    } else if (value instanceof Class) {
        setText(((Class) value).getSimpleName());
    } else if (value instanceof JavaToDataCellConverterFactory<?>) {
        final JavaToDataCellConverterFactory<?> factory = (JavaToDataCellConverterFactory<?>) value;
        setText(factory.getName());
    } else if (value instanceof DataCellToJavaConverterFactory) {
        final DataCellToJavaConverterFactory<?, ?> factory = (DataCellToJavaConverterFactory<?, ?>) value;
        setText(factory.getName());
    }
    super.setToolTipText(getText());
    return this;
}
Also used : Component(java.awt.Component) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory)

Example 5 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class JavaSnippet method getClassLoadersFor.

/**
 * Get the class loaders required for a specific converter factory
 *
 * @param converterFactoryId ID of the converter factory
 * @return A list of class loaders required for given converter factory
 * @noreference This method is not intended to be referenced by clients.
 */
private static Collection<ClassLoader> getClassLoadersFor(final String converterFactoryId) {
    final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(converterFactoryId);
    if (factory.isPresent()) {
        final ArrayList<ClassLoader> clsLoaders = new ArrayList<>(2);
        final ClassLoader sourceCL = factory.get().getSourceType().getClassLoader();
        if (sourceCL != null) {
            clsLoaders.add(sourceCL);
        }
        final ClassLoader destCL = factory.get().getDestinationType().getClassLoader();
        if (destCL != null) {
            clsLoaders.add(destCL);
        }
        return clsLoaders;
    } else {
        final Optional<JavaToDataCellConverterFactory<?>> factory2 = ConverterUtil.getJavaToDataCellConverterFactory(converterFactoryId);
        if (factory2.isPresent()) {
            final ClassLoader cl = factory2.get().getSourceType().getClassLoader();
            if (cl != null) {
                return Collections.singleton(cl);
            }
        }
        return Collections.emptyList();
    }
}
Also used : ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Aggregations

JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)14 DataType (org.knime.core.data.DataType)6 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)4 DataCellToJavaConverterFactory (org.knime.core.data.convert.java.DataCellToJavaConverterFactory)4 FlowVariable (org.knime.core.node.workflow.FlowVariable)4 Optional (java.util.Optional)3 Test (org.junit.Test)3 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)3 DataCell (org.knime.core.data.DataCell)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 Type (org.knime.core.node.workflow.FlowVariable.Type)3 Component (java.awt.Component)2 ArrayList (java.util.ArrayList)2 TypeProvider (org.knime.base.node.jsnippet.type.TypeProvider)2 TypeConverter (org.knime.base.node.jsnippet.type.flowvar.TypeConverter)2 OutColList (org.knime.base.node.jsnippet.util.JavaFieldList.OutColList)2 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)2 BinaryObjectDataCell (org.knime.core.data.blob.BinaryObjectDataCell)2 ListCell (org.knime.core.data.collection.ListCell)2 ArrayToCollectionConverterFactory (org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory)2