-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultProperty.java
185 lines (158 loc) · 6.03 KB
/
DefaultProperty.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.provider;
import com.google.common.base.Preconditions;
import org.gradle.api.Transformer;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.internal.Cast;
import javax.annotation.Nullable;
/**
* The implementation for general-purpose (atomic, non-composite) properties, where
* the value is supplied by some provider.
*
* @param <T> the type of the property value
*/
public class DefaultProperty<T> extends AbstractProperty<T, ProviderInternal<? extends T>> implements Property<T> {
private final Class<T> type;
private final ValueSanitizer<T> sanitizer;
private final static ProviderInternal<?> NOT_DEFINED = Providers.notDefined();
public DefaultProperty(PropertyHost propertyHost, Class<T> type) {
super(propertyHost);
this.type = type;
this.sanitizer = ValueSanitizers.forType(type);
init(Providers.notDefined());
}
@Override
public Object unpackState() {
return getProvider();
}
@Override
public Class<?> publicType() {
return Property.class;
}
@Override
public int getFactoryId() {
return ManagedFactories.PropertyManagedFactory.FACTORY_ID;
}
@Override
public Class<T> getType() {
return type;
}
@Override
public void setFromAnyValue(Object object) {
if (object instanceof Provider) {
set(Cast.<Provider<T>>uncheckedNonnullCast(object));
} else {
set(Cast.<T>uncheckedNonnullCast(object));
}
}
@Override
public void set(@Nullable T value) {
if (value == null) {
discardValue();
} else {
setSupplier(Providers.fixedValue(getValidationDisplayName(), value, type, sanitizer));
}
}
@Override
public Property<T> value(@Nullable T value) {
set(value);
return this;
}
@Override
public Property<T> value(Provider<? extends T> provider) {
set(provider);
return this;
}
public ProviderInternal<? extends T> getProvider() {
// TODO(mlopatkin) while calling getProvider is not going to cause StackOverflowError by itself, the returned provider is typically used in some recursive call.
// Without the safety net of the EvaluationContext, it can cause hard-to-debug exceptions.
try (EvaluationContext.ScopeContext context = openScope()) {
return getSupplier(context);
}
}
public DefaultProperty<T> provider(Provider<? extends T> provider) {
set(provider);
return this;
}
@Override
public void set(Provider<? extends T> provider) {
Preconditions.checkArgument(provider != null, "Cannot set the value of a property using a null provider.");
ProviderInternal<? extends T> p = Providers.internal(provider);
setSupplier(p.asSupplier(getValidationDisplayName(), type, sanitizer));
}
@Override
public Property<T> convention(@Nullable T value) {
if (value == null) {
setConvention(Providers.notDefined());
} else {
setConvention(Providers.fixedValue(getValidationDisplayName(), value, type, sanitizer));
}
return this;
}
@Override
public Property<T> convention(Provider<? extends T> provider) {
Preconditions.checkArgument(provider != null, "Cannot set the convention of a property using a null provider.");
setConvention(Providers.internal(provider).asSupplier(getValidationDisplayName(), type, sanitizer));
return this;
}
@Override
public Property<T> unset() {
super.unset();
return this;
}
@Override
public Property<T> unsetConvention() {
discardConvention();
return this;
}
@Override
protected ExecutionTimeValue<? extends T> calculateOwnExecutionTimeValue(EvaluationContext.ScopeContext context, ProviderInternal<? extends T> value) {
// Discard this property from a provider chain, as it does not contribute anything to the calculation.
return value.calculateExecutionTimeValue();
}
@Override
protected Value<? extends T> calculateValueFrom(EvaluationContext.ScopeContext context, ProviderInternal<? extends T> value, ValueConsumer consumer) {
return value.calculateValue(consumer);
}
@Override
protected ProviderInternal<? extends T> finalValue(EvaluationContext.ScopeContext context, ProviderInternal<? extends T> value, ValueConsumer consumer) {
return value.withFinalValue(consumer);
}
@Override
protected ProviderInternal<? extends T> getDefaultConvention() {
return Cast.uncheckedCast(NOT_DEFINED);
}
@Override
protected boolean isDefaultConvention() {
return getConventionSupplier() == NOT_DEFINED;
}
@Override
protected String describeContents() {
// NOTE: Do not realize the value of the Provider in toString(). The debugger will try to call this method and make debugging really frustrating.
return String.format("property(%s, %s)", type.getName(), describeValue());
}
@Override
public void replace(Transformer<? extends @org.jetbrains.annotations.Nullable Provider<? extends T>, ? super Provider<T>> transformation) {
Provider<? extends T> newValue = transformation.transform(shallowCopy());
if (newValue != null) {
set(newValue);
} else {
set((T) null);
}
}
}