blob: 2c2f061ad44174e1ed62681dae2b316f1489d9af (
plain) (
blame)
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
|
package org.eclipse.xtend.backend.expr;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
import org.eclipse.xtend.backend.common.ExpressionBase;
import org.eclipse.xtend.backend.common.SourcePos;
import org.eclipse.xtend.backend.types.builtin.CollectionType;
/**
* This class deals with the case where the middle end can not decide statically whether
* a property is to be resolved on a single object or on a collection
*
* @author Arno Haase (http://www.haase-consulting.com)
*/
public final class PropertyOnWhateverExpression extends ExpressionBase {
private final ExpressionBase _inner;
private final String _propertyName;
public PropertyOnWhateverExpression (ExpressionBase inner, String propertyName, SourcePos sourcePos) {
super (sourcePos);
_inner = inner;
_propertyName = propertyName;
}
@Override
public Object evaluateInternal(ExecutionContext ctx) {
final Object o = _inner.evaluate(ctx);
if (o == null) {
ctx.logNullDeRef (getPos());
return null;
}
final BackendType t = ctx.getTypesystem().findType (o);
if (CollectionType.INSTANCE.isAssignableFrom(t)) {
if (isProperty (t, _propertyName))
return t.getProperty (ctx, o, _propertyName);
final Collection<Object> result = (o instanceof List) ? new ArrayList<Object> () : new HashSet<Object> ();
for (Object obj: (Collection<?>) o)
result.add (ctx.getTypesystem().findType(obj).getProperty(ctx, obj, _propertyName));
return result;
}
else
return t.getProperty (ctx, o, _propertyName);
}
private boolean isProperty (BackendType t, String propName) {
return t.getProperties().containsKey (propName);
}
}
|