Examples of ConstrettoException


Examples of org.constretto.exception.ConstrettoException

        } else if (value instanceof CArray) {
            return convertList(valueClazz, (CArray) value);
        } else if (value instanceof CObject) {
            return convertMap(keyClazz, valueClazz, (CObject) value);
        } else {
            throw new ConstrettoException("invalid datatype, parsing haz failed");
        }

    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

    @SuppressWarnings("unchecked")
    public static <T> T convertPrimitive(Class<T> clazz, CPrimitive value) throws ConstrettoException {
        if (!converters.containsKey(clazz)) {
            if (!Enum.class.isAssignableFrom(clazz)) {
                throw new ConstrettoException("No converter found for class: " + clazz.getName());
            }

            return (T) convertEnum((Class) clazz, value.value());
        }
        ValueConverter<?> converter = converters.get(clazz);
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

    }

    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> convertMap(Class<K> keyClazz, Class<V> valueClazz, CObject value) throws ConstrettoException {
        if (!converters.containsKey(keyClazz)) {
            throw new ConstrettoException("No converter found for class: " + keyClazz.getName());
        }
        Map<K, V> result = new HashMap<K, V>();
        ValueConverter<K> keyConverter = (ValueConverter<K>) converters.get(keyClazz);

        for (Map.Entry<String, CValue> valueEntry : value.data().entrySet()) {
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

            objectToConfigure = createInstance(configurationClass);
        } catch (ConstrettoException e) {
            throw e;
        }
        catch (Exception e) {
            throw new ConstrettoException("Could not instansiate class of type: " + configurationClass.getName()
                    + " when trying to inject it with configuration, It may be missing a default or @Configure annotated constructor", e);
        }
        injectConfiguration(objectToConfigure);
        return objectToConfigure;
    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

    //
    private <T> T createInstance(final Class<T> configurationClass) throws InstantiationException, IllegalAccessException {


        if(configurationClass.isInterface()) {
            throw new ConstrettoException("Can not instantiate interfaces. You need to create an concrete implementing class first");
        }
        if (configurationClass.isAnonymousClass()) {
            throw new ConstrettoException("Can not instantiate anonymous classes using as(Class<T>. To inject configuration in to inner or anonymous classes, " +
                                                  "instantiate it first and call the on(T configuredObjecT) method");
        }
        Constructor<T>[] annotatedConstructors = findAnnotatedConstructorsOnClass(configurationClass);
        if(configurationClass.isMemberClass() && annotatedConstructors != null) {
            throw new ConstrettoException("Can not instantiate inner classes using a @Configure annotated constructor. " +
                                                  "To inject configuration, construct the instance yourself use the \"on(T configuredObject)\" method");
        }
        if(annotatedConstructors == null) {
            return configurationClass.newInstance();
        } else {
            if(annotatedConstructors.length > 1) {
                throw new ConstrettoException("More than one @Configure annotated constructor defined for class \"" + configurationClass.getName() + "\". It can only be one");
            }
            Constructor<T> constructor = annotatedConstructors[0];
            final Object[] resolvedParameters = resolveParameters(constructor);
            try {
                constructor.setAccessible(true);
                return constructor.newInstance(resolvedParameters);
            } catch (InvocationTargetException e) {
                throw new ConstrettoException("Could not instantiate class with @Configure annotated constructor");
            }

        }
    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

            Constructor constructor = (Constructor) accessibleObject;
            methodAnnotations = constructor.getParameterAnnotations();
            parameterNames = paranamer.lookupParameterNames(constructor);
            parameterTargetTypes = constructor.getParameterTypes();
        } else {
            throw new ConstrettoException("Could not resolve parameter names ");
        }

        Object[] resolvedArguments = new Object[methodAnnotations.length];
        int i = 0;
        for (Annotation[] parameterAnnotations : methodAnnotations) {
            Object defaultValue = null;
            boolean required = true;
            String expression = "";
            Class<?> parameterTargetClass = parameterTargetTypes[i];
            if (parameterAnnotations.length != 0) {
                for (Annotation parameterAnnotation : parameterAnnotations) {
                    if (parameterAnnotation.annotationType() == Configuration.class) {
                        Configuration configurationAnnotation = (Configuration) parameterAnnotation;
                        expression = configurationAnnotation.value();
                        required = configurationAnnotation.required();
                        if (hasAnnotationDefaults(configurationAnnotation)) {
                            if (configurationAnnotation.defaultValueFactory().equals(Configuration.EmptyValueFactory.class)) {
                                defaultValue = ValueConverterRegistry.convert(parameterTargetClass, parameterTargetClass, new CPrimitive(configurationAnnotation.defaultValue()));
                            } else {
                                ConfigurationDefaultValueFactory valueFactory = configurationAnnotation.defaultValueFactory().newInstance();
                                defaultValue = valueFactory.getDefaultValue();
                            }
                        }
                    }
                }
            }
            if (expression.equals("")) {
                if (parameterNames == null) {
                    throw new ConstrettoException("Could not resolve the expression of the property to look up. " +
                                                          "The cause of this could be that the class is compiled without debug enabled. " +
                                                          "when a class is compiled without debug, the @Configuration with a value attribute is required " +
                                                          "to correctly resolve the property expression.");
                } else {
                    expression = parameterNames[i];
                }
            }
            if (hasValue(expression)) {
                if (parameterTargetClass.isAssignableFrom(List.class)) {
                    Class<?> collectionParameterType = getCollectionParameterType(createMethodParameter(accessibleObject, i));
                    resolvedArguments[i] = evaluateToList(collectionParameterType, expression);
                } else if (parameterTargetClass.isAssignableFrom(Map.class)) {
                    Class<?> mapKeyType = getMapKeyParameterType(createMethodParameter(accessibleObject, i));
                    Class<?> mapValueType = getMapValueParameterType(createMethodParameter(accessibleObject, i));
                    resolvedArguments[i] = evaluateToMap(mapKeyType, mapValueType, expression);
                } else {
                    resolvedArguments[i] = processAndConvert(parameterTargetClass, expression);
                }

            } else {
                if (defaultValue != null || !required) {
                    resolvedArguments[i] = defaultValue;
                } else {
                    if(accessibleObject instanceof Constructor) {
                        Constructor constructor = (Constructor) accessibleObject;
                        throw new ConstrettoException("Missing value or default value for expression [" + expression + "], in annotated constructor in class [" + constructor.getClass().getName() + "], with tags " + currentTags + ".");

                    }
                    else {
                        Method method = (Method) accessibleObject;
                        throw new ConstrettoException("Missing value or default value for expression [" + expression + "], in method [" + method.getName() + "], in class [" + method.getClass().getName() + "], with tags " + currentTags + ".");

                    }
                }
            }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

                    method.setAccessible(true);
                    method.invoke(objectToConfigure, resolvedArguments);

                }
            } catch (IllegalAccessException e) {
                throw new ConstrettoException("Cold not invoke method ["
                                                      + method.getName() + "] annotated with @Configured,", e);
            } catch (InvocationTargetException e) {
                throw new ConstrettoException("Cold not invoke method ["
                                                      + method.getName() + "] annotated with @Configured,", e);
            } catch (InstantiationException e) {
                throw new ConstrettoException("Cold not invoke method ["
                                                      + method.getName() + "] annotated with @Configured,", e);
            }
        }
    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

                                } else {
                                    ConfigurationDefaultValueFactory valueFactory = configurationAnnotation.defaultValueFactory().newInstance();
                                    field.set(objectToConfigure, valueFactory.getDefaultValue());
                                }
                            } else if (configurationAnnotation.required()) {
                                throw new ConstrettoException("Missing value or default value for expression [" + expression + "] for field [" + field.getName() + "], in class [" + objectToConfigure.getClass().getName() + "] with tags " + currentTags + ".");
                            }
                        }
                    } else if (field.isAnnotationPresent(Tags.class)) {
                        field.setAccessible(true);
                        field.set(objectToConfigure, currentTags);
                    }
                } catch (IllegalAccessException e) {
                    throw new ConstrettoException("Cold not inject configuration into field ["
                            + field.getName() + "] annotated with @Configuration, in class [" + objectToConfigure.getClass().getName() + "] with tags " + currentTags, e);
                } catch (InstantiationException e) {
                    throw new ConstrettoException("Cold not inject configuration into field ["
                            + field.getName() + "] annotated with @Configuration, in class [" + objectToConfigure.getClass().getName() + "] with tags " + currentTags, e);
                }
            }
        } while ((objectToConfigureClass = objectToConfigureClass.getSuperclass()) != null);
    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

    private Attributes readAttributesFromLdap(String distinguishedName) {
        try {
            return dirContext.getAttributes(createName(distinguishedName));
        } catch (NamingException e) {
            throw new ConstrettoException(String.format("Could not find LDAP attributes for DSN \"%1$s\"",
                                                        distinguishedName), e);
        }
    }
View Full Code Here

Examples of org.constretto.exception.ConstrettoException

            while (searchResultNamingEnumeration.hasMore()) {
                final SearchResult result = searchResultNamingEnumeration.next();
                final Attributes attributes = result.getAttributes();
                final Attribute attribute = attributes.get(keyAttribute);
                if (attribute == null) {
                    throw new ConstrettoException(String.format(
                            "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"",
                            result.getName(),
                            keyAttribute));
                }
                final String key = attribute.get().toString();
                ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags);
            }
        } catch (NamingException e) {
            throw new ConstrettoException(
                    String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"",
                                  searchBase,
                                  filter), e);
        }
        return this;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.