use of org.gradle.model.internal.manage.schema.ModelProperty in project gradle by gradle.
the class ManagedProxyClassGenerator method writeViewMethods.
private void writeViewMethods(ClassVisitor visitor, Type generatedType, Collection<ModelType<?>> viewTypes, StructBindings<?> bindings) {
Type delegateType;
StructSchema<?> delegateSchema = bindings.getDelegateSchema();
if (delegateSchema != null) {
Class<?> delegateClass = delegateSchema.getType().getRawClass();
declareDelegateField(visitor, delegateClass);
delegateType = Type.getType(delegateClass);
} else {
delegateType = null;
}
Multimap<String, ModelProperty<?>> viewPropertiesByNameBuilder = ArrayListMultimap.create();
Set<Wrapper<Method>> viewMethods = Sets.newLinkedHashSet();
for (StructSchema<?> viewSchema : bindings.getImplementedViewSchemas()) {
for (ModelType<?> viewType : viewTypes) {
if (viewType.equals(viewSchema.getType())) {
for (ModelProperty<?> property : viewSchema.getProperties()) {
String propertyName = property.getName();
viewPropertiesByNameBuilder.put(propertyName, property);
}
for (WeaklyTypeReferencingMethod<?, ?> viewMethod : viewSchema.getAllMethods()) {
viewMethods.add(SIGNATURE_EQUIVALENCE.wrap(viewMethod.getMethod()));
}
break;
}
}
}
Class<?> viewClass = bindings.getPublicSchema().getType().getConcreteClass();
for (Collection<ModelProperty<?>> viewProperties : viewPropertiesByNameBuilder.asMap().values()) {
writeViewPropertyDslMethods(visitor, generatedType, viewProperties, viewClass);
}
for (StructMethodBinding methodBinding : bindings.getMethodBindings()) {
WeaklyTypeReferencingMethod<?, ?> weakViewMethod = methodBinding.getViewMethod();
Method viewMethod = weakViewMethod.getMethod();
// Don't generate method if it's not part of the view schema
Wrapper<Method> methodKey = SIGNATURE_EQUIVALENCE.wrap(viewMethod);
if (!viewMethods.contains(methodKey)) {
continue;
}
if (methodBinding instanceof DirectMethodBinding) {
// TODO:LPTR What is with the "metaClass" property here?
boolean isGetterMethod = methodBinding.getAccessorType() == GET_GETTER || methodBinding.getAccessorType() == IS_GETTER;
if (isGetterMethod && !Modifier.isFinal(viewMethod.getModifiers()) && !viewMethod.getName().equals("getMetaClass")) {
writeNonAbstractMethodWrapper(visitor, generatedType, viewClass, viewMethod);
}
} else if (methodBinding instanceof BridgeMethodBinding) {
writeBridgeMethod(visitor, generatedType, viewMethod);
} else if (methodBinding instanceof DelegateMethodBinding) {
writeDelegatingMethod(visitor, generatedType, delegateType, viewMethod);
} else if (methodBinding instanceof ManagedPropertyMethodBinding) {
ManagedPropertyMethodBinding propertyBinding = (ManagedPropertyMethodBinding) methodBinding;
ManagedProperty<?> managedProperty = bindings.getManagedProperty(propertyBinding.getPropertyName());
String propertyName = managedProperty.getName();
Class<?> propertyClass = managedProperty.getType().getRawClass();
WeaklyTypeReferencingMethod<?, ?> propertyAccessor = propertyBinding.getViewMethod();
switch(propertyBinding.getAccessorType()) {
case GET_GETTER:
case IS_GETTER:
writeGetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
break;
case SETTER:
writeSetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
break;
default:
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
}
use of org.gradle.model.internal.manage.schema.ModelProperty in project gradle by gradle.
the class ModelRuleExtractor method doExtract.
private <T> CachedRuleSource doExtract(final Class<T> source) {
final ModelType<T> type = ModelType.of(source);
FormattingValidationProblemCollector problems = new FormattingValidationProblemCollector("rule source", type);
DefaultMethodModelRuleExtractionContext context = new DefaultMethodModelRuleExtractionContext(this, problems);
// TODO - exceptions thrown here should point to some extensive documentation on the concept of class rule sources
StructSchema<T> schema = getSchema(source, context);
if (schema == null) {
throw new InvalidModelRuleDeclarationException(problems.format());
}
// sort for determinism
Set<Method> methods = new TreeSet<Method>(Ordering.usingToString());
methods.addAll(Arrays.asList(source.getDeclaredMethods()));
ImmutableList.Builder<ModelProperty<?>> implicitInputs = ImmutableList.builder();
ModelProperty<?> target = null;
for (ModelProperty<?> property : schema.getProperties()) {
if (property.isAnnotationPresent(RuleTarget.class)) {
target = property;
} else if (property.isAnnotationPresent(RuleInput.class) && !(property.getSchema() instanceof ScalarValueSchema)) {
implicitInputs.add(property);
}
for (WeaklyTypeReferencingMethod<?, ?> method : property.getAccessors()) {
methods.remove(method.getMethod());
}
}
ImmutableList.Builder<ExtractedRuleDetails> rules = ImmutableList.builder();
for (Method method : methods) {
MethodRuleDefinition<?, ?> ruleDefinition = DefaultMethodRuleDefinition.create(source, method);
ExtractedModelRule rule = getMethodHandler(ruleDefinition, method, context);
if (rule != null) {
rules.add(new ExtractedRuleDetails(ruleDefinition, rule));
}
}
if (context.hasProblems()) {
throw new InvalidModelRuleDeclarationException(problems.format());
}
StructBindings<T> bindings = structBindingsStore.getBindings(schema.getType());
if (schema.getProperties().isEmpty()) {
return new StatelessRuleSource(rules.build(), Modifier.isAbstract(source.getModifiers()) ? new AbstractRuleSourceFactory<T>(schema, bindings, proxyFactory) : new ConcreteRuleSourceFactory<T>(type));
} else {
return new ParameterizedRuleSource(rules.build(), target, implicitInputs.build(), schema, bindings, proxyFactory);
}
}
use of org.gradle.model.internal.manage.schema.ModelProperty in project gradle by gradle.
the class VariantAspectExtractionStrategy method extract.
@Nullable
@Override
public ModelSchemaAspectExtractionResult extract(ModelSchemaExtractionContext<?> extractionContext, final List<ModelPropertyExtractionResult<?>> propertyResults) {
ImmutableSet.Builder<ModelProperty<?>> dimensionsBuilder = ImmutableSet.builder();
for (ModelPropertyExtractionResult<?> propertyResult : propertyResults) {
ModelProperty<?> property = propertyResult.getProperty();
for (PropertyAccessorExtractionContext accessor : propertyResult.getAccessors()) {
if (accessor.isAnnotationPresent(Variant.class)) {
if (accessor.getAccessorType() == PropertyAccessorType.SETTER) {
throw invalidProperty(extractionContext, property, "@Variant annotation is only allowed on getter methods");
}
Class<?> propertyType = property.getType().getRawClass();
if (!String.class.equals(propertyType) && !Named.class.isAssignableFrom(propertyType)) {
throw invalidProperty(extractionContext, property, String.format("@Variant annotation only allowed for properties of type String and %s, but property has type %s", Named.class.getName(), propertyType.getName()));
}
dimensionsBuilder.add(property);
}
}
}
ImmutableSet<ModelProperty<?>> dimensions = dimensionsBuilder.build();
if (dimensions.isEmpty()) {
return null;
}
return new ModelSchemaAspectExtractionResult(new VariantAspect(dimensions));
}
Aggregations