@Override public <S extends TaskInternal> S create(String name, final Class<S> type) { 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())); } 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 { return instantiator.newInstance(generatedType); } catch (ObjectInstantiationException e) { throw new TaskInstantiationException(String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause()); } } })); }
private TaskInternal createTaskObject(ProjectInternal project, final Class<? extends TaskInternal> type, String name, boolean generateGetters) { 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())); } final Class<? extends TaskInternal> generatedType; if (generateGetters) { generatedType = generator.generate(type); } else { generatedType = type; } return AbstractTask.injectIntoNewInstance(project, name, new Callable<TaskInternal>() { public TaskInternal call() throws Exception { try { return instantiator.newInstance(generatedType); } catch (ObjectInstantiationException e) { throw new TaskInstantiationException(String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause()); } } }); }
public void attachActions(PropertyInfo parent, Class<?> type) { Class<?> superclass = type.getSuperclass(); if (!(superclass == null // Avoid reflecting on classes we know we don't need to look at || superclass.equals(ConventionTask.class) || superclass.equals(DefaultTask.class) || superclass.equals(AbstractTask.class) || superclass.equals(Object.class) )) { attachActions(parent, superclass); } for (Method method : type.getDeclaredMethods()) { if (!isGetter(method)) { continue; } String name = method.getName(); int prefixLength = name.startsWith("is") ? 2 : 3; // it's 'get' if not 'is'. String fieldName = StringUtils.uncapitalize(name.substring(prefixLength)); String propertyName = fieldName; if (parent != null) { propertyName = parent.getName() + '.' + propertyName; } PropertyInfo propertyInfo = new PropertyInfo(type, this, parent, propertyName, method); attachValidationActions(propertyInfo, fieldName); if (propertyInfo.required) { properties.add(propertyInfo); } } }