Search in sources :

Example 1 with ObjectInstantiationException

use of org.gradle.api.reflect.ObjectInstantiationException in project gradle by gradle.

the class TaskFactory method create.

@Override
public <S extends Task> S create(String name, final Class<S> type, final Object... args) {
    if (!Task.class.isAssignableFrom(type)) {
        throw new InvalidUserDataException(String.format("Cannot create task of type '%s' as it does not implement the Task interface.", type.getSimpleName()));
    }
    NameValidator.validate(name, "task name", "");
    final Class<? extends Task> generatedType;
    if (type.isAssignableFrom(DefaultTask.class)) {
        generatedType = generator.generate(DefaultTask.class);
    } else {
        generatedType = generator.generate(type);
    }
    return type.cast(AbstractTask.injectIntoNewInstance(project, name, type, new Callable<Task>() {

        public Task call() throws Exception {
            try {
                if (args != null) {
                    return instantiator.newInstance(generatedType, args);
                }
                return instantiator.newInstance(generatedType);
            } catch (ObjectInstantiationException e) {
                throw new TaskInstantiationException(String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause());
            }
        }
    }));
}
Also used : Task(org.gradle.api.Task) AbstractTask(org.gradle.api.internal.AbstractTask) DefaultTask(org.gradle.api.DefaultTask) ObjectInstantiationException(org.gradle.api.reflect.ObjectInstantiationException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) DefaultTask(org.gradle.api.DefaultTask) Callable(java.util.concurrent.Callable) TaskInstantiationException(org.gradle.api.tasks.TaskInstantiationException)

Example 2 with ObjectInstantiationException

use of org.gradle.api.reflect.ObjectInstantiationException in project gradle by gradle.

the class DirectInstantiator method newInstance.

public <T> T newInstance(Class<? extends T> type, Object... params) {
    try {
        Class<?>[] argTypes = wrapArgs(params);
        Constructor<?> match = null;
        while (match == null) {
            // we need to wrap this into a loop, because there's always a risk
            // that the method, which is weakly referenced, has been collected
            // in between the creation time and now
            match = constructorCache.get(type, argTypes).getMethod();
        }
        return type.cast(match.newInstance(params));
    } catch (InvocationTargetException e) {
        throw new ObjectInstantiationException(type, e.getCause());
    } catch (Throwable t) {
        throw new ObjectInstantiationException(type, t);
    }
}
Also used : ObjectInstantiationException(org.gradle.api.reflect.ObjectInstantiationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with ObjectInstantiationException

use of org.gradle.api.reflect.ObjectInstantiationException in project gradle by gradle.

the class DependencyInjectingInstantiator method newInstance.

public <T> T newInstance(final Class<? extends T> type, Object... parameters) {
    try {
        CachedConstructor cached = constructorCache.get(type, new Transformer<CachedConstructor, Class<?>>() {

            @Override
            public CachedConstructor transform(Class<?> aClass) {
                try {
                    validateType(type);
                    Class<? extends T> implClass = classGenerator.generate(type);
                    Constructor<?> constructor = selectConstructor(type, implClass);
                    constructor.setAccessible(true);
                    return CachedConstructor.of(constructor);
                } catch (Throwable e) {
                    return CachedConstructor.of(e);
                }
            }
        });
        if (cached.error != null) {
            throw cached.error;
        }
        Constructor<?> constructor = cached.constructor;
        Object[] resolvedParameters = convertParameters(type, constructor, parameters);
        try {
            Object instance = constructor.newInstance(resolvedParameters);
            if (instance instanceof WithServiceRegistry) {
                ((WithServiceRegistry) instance).setServices(services);
            }
            return type.cast(instance);
        } catch (InvocationTargetException e) {
            throw e.getCause();
        }
    } catch (Throwable t) {
        throw new ObjectInstantiationException(type, t);
    }
}
Also used : ObjectInstantiationException(org.gradle.api.reflect.ObjectInstantiationException) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ObjectInstantiationException (org.gradle.api.reflect.ObjectInstantiationException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Constructor (java.lang.reflect.Constructor)1 Callable (java.util.concurrent.Callable)1 DefaultTask (org.gradle.api.DefaultTask)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 Task (org.gradle.api.Task)1 AbstractTask (org.gradle.api.internal.AbstractTask)1 TaskInstantiationException (org.gradle.api.tasks.TaskInstantiationException)1