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.equals(from) || to.equals(TypeDesc.OBJECT)) 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); } }