1 package org.simantics.scl.compiler.constants;
3 import java.lang.reflect.Field;
5 import org.cojen.classfile.TypeDesc;
6 import org.objectweb.asm.Label;
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
8 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
11 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
12 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
13 import org.simantics.scl.compiler.internal.codegen.utils.TransientClassBuilder;
14 import org.simantics.scl.compiler.runtime.MutableClassLoader;
15 import org.simantics.scl.compiler.types.Type;
17 public class JavaStaticField extends Constant {
27 public JavaStaticField(String className, String fieldName, Type effect, TypeDesc fieldType, Type type, int constructorTag) {
29 this.className = className;
30 this.fieldName = fieldName;
31 this.fieldType = fieldType;
33 this.constructorTag = constructorTag;
36 public JavaStaticField(String className, String fieldName, Type type, int constructorTag) {
37 this(className, fieldName, null, null, type, constructorTag);
41 public void push(MethodBuilder mb) {
42 if(fieldType == null) {
43 JavaTypeTranslator tt = mb.getJavaTypeTranslator();
44 fieldType = tt.toTypeDesc(getType());
47 mb.loadStaticField(className, fieldName, fieldType);
50 public String getClassName() {
54 public String getFieldName() {
59 public Object realizeValue(TransientClassBuilder classBuilder) {
60 MutableClassLoader classLoader = classBuilder.classLoader;
62 Class<?> clazz = classLoader.loadClass(className.replace('/', '.'));
63 Field field = clazz.getDeclaredField(fieldName);
64 return field.get(null);
65 } catch (IllegalArgumentException e) {
66 throw new InternalCompilerError(e);
67 } catch (IllegalAccessException e) {
68 throw new InternalCompilerError(e);
69 } catch (ClassNotFoundException e) {
70 throw new InternalCompilerError(e);
71 } catch (SecurityException e) {
72 throw new InternalCompilerError(e);
73 } catch (NoSuchFieldException e) {
74 throw new InternalCompilerError(e);
79 public void deconstruct(MethodBuilder mb, IVal parameter, Cont success,
82 mb.push(parameter, getType());
83 mb.invokeVirtual(TypeDesc.OBJECT, "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[1]);
84 mb.ifZeroComparisonBranch(failure, "==");
88 public int constructorTag() {
89 return constructorTag;
93 public String toString() {
94 return "JavaStaticField(" + className + "." + fieldName +")";