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());
}
}
}));
}
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);
}
}
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);
}
}
Aggregations