Search in sources :

Example 1 with NamedArg

use of javafx.beans.NamedArg in project intellij-community by JetBrains.

the class JavaFxGenerateDefaultPropertyValuesScript method constructorNamedArgValues.

@NotNull
private static Map<String, Object> constructorNamedArgValues(Class<?> aClass) {
    if (aClass.isInterface() || aClass.isAnnotation() || WritableValue.class.isAssignableFrom(aClass) || Event.class.isAssignableFrom(aClass)) {
        return Collections.emptyMap();
    }
    final Constructor<?>[] constructors = aClass.getConstructors();
    final Args args = new Args();
    for (Constructor<?> constructor : constructors) {
        final Parameter[] parameters = constructor.getParameters();
        for (Parameter parameter : parameters) {
            final Class<?> type = parameter.getType();
            if (type.isPrimitive() || type.isEnum() || type == String.class) {
                final NamedArg namedArg = parameter.getAnnotation(NamedArg.class);
                if (namedArg == null)
                    continue;
                final String name = namedArg.value();
                if (!name.isEmpty()) {
                    final String defaultValue = namedArg.defaultValue();
                    if ((type == String.class || type.isEnum()) && !defaultValue.isEmpty() && !"\"\"".equals(defaultValue)) {
                        args.add(name, type, defaultValue);
                    } else if (type == boolean.class) {
                        args.add(name, type, Boolean.valueOf(defaultValue));
                    } else if (type == int.class) {
                        try {
                            args.add(name, type, Integer.valueOf(defaultValue));
                        } catch (NumberFormatException e) {
                            args.add(name, type, Integer.valueOf(0));
                        }
                    } else if (type == long.class) {
                        try {
                            args.add(name, type, Long.valueOf(defaultValue));
                        } catch (NumberFormatException e) {
                            args.add(name, type, Long.valueOf(0));
                        }
                    } else if (type == double.class) {
                        try {
                            args.add(name, type, Double.valueOf(defaultValue));
                        } catch (NumberFormatException e) {
                            args.add(name, type, Double.valueOf(0));
                        }
                    } else if (type == float.class) {
                        try {
                            args.add(name, type, Float.valueOf(defaultValue));
                        } catch (NumberFormatException e) {
                            args.add(name, type, Float.valueOf(0));
                        }
                    } else if (!type.isEnum() && type != String.class) {
                        System.err.println("pri " + type);
                    }
                }
            }
        }
    }
    if (args.isEmpty())
        return Collections.emptyMap();
    Map<String, Object> result = new TreeMap<>();
    for (String name : args) {
        final Type type = args.getType(name);
        if (type != null) {
            final Object value = args.getValue(name);
            if (value != null) {
                result.put(name, value);
            }
        }
    }
    return result;
}
Also used : NamedArg(javafx.beans.NamedArg) Event(javafx.event.Event) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NamedArg (javafx.beans.NamedArg)1 Event (javafx.event.Event)1 NotNull (org.jetbrains.annotations.NotNull)1