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;h=d1e2ade645cdc57006866baa787083b21fd84fba;hb=c26409b1caf2f1e560d37c5befd11b442399c3fe;hp=5ea30643a52675c7522122576ea50cc4f3a84924;hpb=969bd23cab98a79ca9101af33334000879fb60c5;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 index 5ea30643a..d1e2ade64 100644 --- 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 @@ -1,171 +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); - } - -} +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); + } + +}