X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Ftypes%2FAbstractRuntimeJavaReferenceValidator.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Ftypes%2FAbstractRuntimeJavaReferenceValidator.java;h=5ea30643a52675c7522122576ea50cc4f3a84924;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/AbstractRuntimeJavaReferenceValidator.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/AbstractRuntimeJavaReferenceValidator.java new file mode 100644 index 000000000..5ea30643a --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/AbstractRuntimeJavaReferenceValidator.java @@ -0,0 +1,171 @@ +package org.simantics.scl.compiler.internal.codegen.types; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; + +import org.cojen.classfile.TypeDesc; +import org.simantics.scl.compiler.constants.generic.ClassRef; +import org.simantics.scl.runtime.function.Function; + +public abstract class AbstractRuntimeJavaReferenceValidator +implements JavaReferenceValidator, Method, Field, Constructor> { + + @Override + public abstract Class findClass(TypeDesc name); + + @Override + public boolean isInterface(Class clazz) { + return clazz.isInterface(); + } + + @Override + public boolean isPublic(Class clazz) { + return Modifier.isPublic(clazz.getModifiers()); + } + + @Override + public Method[] findCompatibleMethods(Class clazz, boolean isStatic, String name, TypeDesc[] parameterTypes, TypeDesc returnType) { + Class[] parameterClasses = new Class[parameterTypes.length]; + for(int i=0;i returnClass = findClass(returnType); + + ArrayList methods = new ArrayList(2); + + methodLoop: + for(Method method : clazz.getMethods()) { + if(!method.getName().equals(name)) + continue; + if(Modifier.isStatic(method.getModifiers()) != isStatic) + continue; + + Class[] parameters = method.getParameterTypes(); + if(parameters.length != parameterClasses.length) + continue; + for(int i=0;i[] parameters = method.getParameterTypes(); + TypeDesc[] result = new TypeDesc[parameters.length]; + for(int i=0;i[] findCompatibleConstructors(Class clazz, + TypeDesc[] types) { + Class[] classes = new Class[types.length]; + for(int i=0;i> constructors = new ArrayList>(2); + + methodLoop: + for(Constructor constructor : clazz.getConstructors()) { + Class[] parameters = constructor.getParameterTypes(); + int arity = parameters.length; + if(arity > maxArity) + continue; + for(int i=0;i constructor) { + Class[] parameters = constructor.getParameterTypes(); + TypeDesc[] result = new TypeDesc[parameters.length]; + for(int i=0;i clazz, String name) { + try { + return clazz.getField(name); + } catch(NoSuchFieldException e) { + return null; + } + } + + @Override + public boolean isStaticField(Field field) { + return Modifier.isStatic(field.getModifiers()); + } + + @Override + public TypeDesc getFieldType(Field field) { + return TypeDesc.forClass(field.getType()); + } + + @Override + public boolean isAssignableFrom(TypeDesc to, TypeDesc from) { + if(to == from) + return true; + Class toClass = findClass(to); + Class fromClass = findClass(from); + if(toClass == null || fromClass == null) + // Note: I added this branch when I noticed that type can be + // one from the module under compilation. + // Note2: A problem with this seems to be that also + // some other type descs go to null, such as double[][] + return false; + else + return toClass.isAssignableFrom(fromClass); + } + + @Override + public Method[] chooseBest(Method[] methods) { + ArrayList newResult = new ArrayList(); + for(Method method : methods) + if(!method.isSynthetic()) + newResult.add(method); + return newResult.toArray(new Method[newResult.size()]); + } + + @Override + public ClassRef getClassRef(String className) { + Class clazz = findClass(TypeDesc.forClass(className)); + if(clazz == null) + return null; + return new ClassRef(clazz); + } + +}