-
Notifications
You must be signed in to change notification settings - Fork 57
/
AppliedValue.java
171 lines (151 loc) · 7.71 KB
/
AppliedValue.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
package com.redhat.ceylon.compiler.java.runtime.metamodel;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import ceylon.language.metamodel.Model$impl;
import ceylon.language.metamodel.Value$impl;
import ceylon.language.metamodel.AttributeModel$impl;
import com.redhat.ceylon.compiler.java.codegen.Naming;
import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Ignore;
import com.redhat.ceylon.compiler.java.metadata.TypeInfo;
import com.redhat.ceylon.compiler.java.metadata.TypeParameter;
import com.redhat.ceylon.compiler.java.metadata.TypeParameters;
import com.redhat.ceylon.compiler.java.metadata.Variance;
import com.redhat.ceylon.compiler.java.runtime.model.ReifiedType;
import com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor;
import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionClass;
import com.redhat.ceylon.compiler.loader.model.FieldValue;
import com.redhat.ceylon.compiler.loader.model.JavaBeanValue;
import com.redhat.ceylon.compiler.loader.model.LazyValue;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
@Ceylon(major = 5)
@com.redhat.ceylon.compiler.java.metadata.Class
@TypeParameters({
@TypeParameter(value = "Type", variance = Variance.OUT),
})
public class AppliedValue<Type>
implements ceylon.language.metamodel.Value<Type>, ReifiedType {
private ceylon.language.metamodel.Type type;
@Ignore
protected TypeDescriptor $reifiedType;
protected FreeAttribute declaration;
private MethodHandle getter;
public AppliedValue(@Ignore TypeDescriptor $reifiedType, FreeAttribute value, ProducedType valueType, Object instance) {
this.type = Metamodel.getAppliedMetamodel(valueType);
this.$reifiedType = $reifiedType;
this.declaration = value;
initField(instance, valueType);
}
private void initField(Object instance, ProducedType valueType) {
com.redhat.ceylon.compiler.typechecker.model.Declaration decl = declaration.declaration;
if(decl instanceof JavaBeanValue){
java.lang.Class<?> javaClass = Metamodel.getJavaClass((com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface)decl.getContainer());
String getterName = ((JavaBeanValue) decl).getGetterName();
try {
Method m = javaClass.getMethod(getterName);
m.setAccessible(true);
getter = MethodHandles.lookup().unreflect(m);
java.lang.Class<?> getterType = m.getReturnType();
getter = MethodHandleUtil.boxReturnValue(getter, getterType, valueType);
getter = getter.bindTo(instance);
// we need to cast to Object because this is what comes out when calling it in $call
getter = getter.asType(MethodType.methodType(Object.class));
initField(decl, javaClass, getterType, instance, valueType);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
} catch (SecurityException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
}
}else if(decl instanceof LazyValue){
LazyValue lazyDecl = (LazyValue) decl;
java.lang.Class<?> javaClass = ((ReflectionClass)lazyDecl.classMirror).klass;
// FIXME: we should really save the getter name in the LazyDecl
String getterName = Naming.getGetterName(lazyDecl);
try {
Method m = javaClass.getMethod(getterName);
m.setAccessible(true);
getter = MethodHandles.lookup().unreflect(m);
java.lang.Class<?> getterType = m.getReturnType();
getter = MethodHandleUtil.boxReturnValue(getter, getterType, valueType);
// we need to cast to Object because this is what comes out when calling it in $call
getter = getter.asType(MethodType.methodType(Object.class));
initField(decl, javaClass, getterType, null, valueType);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
} catch (SecurityException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to find getter method "+getterName+" for: "+decl, e);
}
}else if(decl instanceof FieldValue){
FieldValue fieldDecl = (FieldValue) decl;
java.lang.Class<?> javaClass = Metamodel.getJavaClass((com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface)decl.getContainer());
String fieldName = fieldDecl.getRealName();
try {
Field f = javaClass.getField(fieldName);
getter = MethodHandles.lookup().unreflectGetter(f);
java.lang.Class<?> getterType = f.getType();
getter = MethodHandleUtil.boxReturnValue(getter, getterType, valueType);
getter = getter.bindTo(instance);
// we need to cast to Object because this is what comes out when calling it in $call
getter = getter.asType(MethodType.methodType(Object.class));
initField(decl, javaClass, getterType, instance, valueType);
} catch (NoSuchFieldException e) {
throw new RuntimeException("Failed to find field "+fieldName+" for: "+decl, e);
} catch (SecurityException e) {
throw new RuntimeException("Failed to find field "+fieldName+" for: "+decl, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to find field "+fieldName+" for: "+decl, e);
}
}else
throw new RuntimeException("Unsupported attribute type: "+decl);
}
// for AppliedVariable
protected void initField(com.redhat.ceylon.compiler.typechecker.model.Declaration decl, java.lang.Class<?> javaClass,
java.lang.Class<?> getterReturnType, Object instance, ProducedType valueType) {}
@Override
@Ignore
public Value$impl<Type> $ceylon$language$metamodel$Value$impl() {
// TODO Auto-generated method stub
return null;
}
@Override
@Ignore
public AttributeModel$impl<Type> $ceylon$language$metamodel$AttributeModel$impl() {
// TODO Auto-generated method stub
return null;
}
@Override
@Ignore
public Model$impl $ceylon$language$metamodel$Model$impl() {
// TODO Auto-generated method stub
return null;
}
@Override
@TypeInfo("ceylon.language.metamodel.declaration::AttributeDeclaration")
public ceylon.language.metamodel.declaration.AttributeDeclaration getDeclaration() {
return declaration;
}
@Override
public Type get() {
try {
return (Type) getter.invokeExact();
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke getter for "+declaration.getName(), e);
}
}
@Override
@TypeInfo("ceylon.language.metamodel::Type")
public ceylon.language.metamodel.Type getType() {
return type;
}
@Override
public TypeDescriptor $getType() {
return TypeDescriptor.klass(AppliedValue.class, $reifiedType);
}
}