From 437c402d1c9ccfb78d72967b5db06193b7206002 Mon Sep 17 00:00:00 2001 From: Michael Hoffer Date: Tue, 12 Nov 2019 23:37:14 +0100 Subject: [PATCH] addresses issue #31 (WIP) --- .../eu/mihosoft/vmf/core/DelegationInfo.java | 8 ++- .../java/eu/mihosoft/vmf/core/TypeUtil.java | 59 ++++++++++++++----- .../vmf/gradle/plugin/VMFPlugin.groovy | 7 ++- .../miniclang/vmfmodel/MiniClangModel.java | 3 +- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/eu/mihosoft/vmf/core/DelegationInfo.java b/core/src/main/java/eu/mihosoft/vmf/core/DelegationInfo.java index 537dbf9e..19b382bd 100644 --- a/core/src/main/java/eu/mihosoft/vmf/core/DelegationInfo.java +++ b/core/src/main/java/eu/mihosoft/vmf/core/DelegationInfo.java @@ -84,8 +84,11 @@ public static DelegationInfo newInstance(Model model, Method m) { List paramTypes = new ArrayList<>(m.getParameters().length); List paramNames = new ArrayList<>(m.getParameters().length); + boolean delegated = !m.getName().startsWith("get") && !m.getName().startsWith("is"); + boolean varargs = m.isVarArgs(); + for(Parameter p : m.getParameters()) { - paramTypes.add(TypeUtil.getTypeAsString(model,p.getType())); + paramTypes.add(TypeUtil.getTypeAsString(model,p.getType(),delegated,varargs)); paramNames.add(p.getName()); } @@ -117,11 +120,10 @@ public static DelegationInfo newInstance(Model model, Class clazz) { List paramTypes = new ArrayList<>(); List paramNames = new ArrayList<>(); - return newInstance( delegation.className(), "on"+clazz.getSimpleName()+"Instantiated", - TypeUtil.getTypeAsString(model,clazz), + TypeUtil.getTypeAsString(model,clazz,false,false), paramTypes, paramNames, true, "" /*constructors are not documented because there's no public API for them*/); } diff --git a/core/src/main/java/eu/mihosoft/vmf/core/TypeUtil.java b/core/src/main/java/eu/mihosoft/vmf/core/TypeUtil.java index 0de7d86f..eae2164e 100644 --- a/core/src/main/java/eu/mihosoft/vmf/core/TypeUtil.java +++ b/core/src/main/java/eu/mihosoft/vmf/core/TypeUtil.java @@ -26,8 +26,10 @@ import java.io.File; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; +import java.lang.reflect.TypeVariable; import java.util.Collection; import java.util.List; +import java.util.StringJoiner; /** * Created by miho on 21.03.2017. @@ -49,7 +51,7 @@ private TypeUtil() { * @param cls class object to convert * @return the specified type as string */ - public static String getTypeAsString(Model model, Class cls) { + public static String getTypeAsString(Model model, Class cls, boolean delegated, boolean vararg) { String packageName; String typeName; @@ -58,14 +60,32 @@ public static String getTypeAsString(Model model, Class cls) { packageName = ""; } else if (Collection.class.isAssignableFrom(cls)) { - throw new RuntimeException( - "Collections can only be specified with array syntax, i.e., '[]'"); + if(delegated) { + throw new RuntimeException( + "Collections can currently not be used as params in delegated methods. This will be fixed."); + } else { + throw new RuntimeException( + "Collections can only be specified with array syntax, i.e., '[]'"); + } } else if (cls.isArray()) { - Class containedClazz = cls.getComponentType(); - typeName = "VList<" + ModelType.primitiveToBoxedType( - model.convertModelTypeToDestination(containedClazz)) + ">"; + if(delegated) { + Class containedClazz = cls.getComponentType(); + typeName = containedClazz.getSimpleName(); + packageName = model. + convertModelPackageToDestination(TypeUtil.getPackageName(containedClazz)); + if(vararg) { + typeName +="..."; + } else { + typeName +="[]"; + } + packageName = ""; + } else { + Class containedClazz = cls.getComponentType(); + typeName = "VList<" + ModelType.primitiveToBoxedType( + model.convertModelTypeToDestination(containedClazz)) + ">"; - packageName = "eu.mihosoft.vcollections"; + packageName = "eu.mihosoft.vcollections"; + } } else { typeName = cls.getSimpleName(); @@ -137,18 +157,27 @@ public static String getReturnTypeAsString(Model model, Method m) { packageName = "eu.mihosoft.vcollections"; } else if (propClass.isArray()) { - Class containedClazz = propClass.getComponentType(); - typeName = "VList<" + ModelType.primitiveToBoxedType( - model. - convertModelTypeToDestination(containedClazz)) + ">"; - // System.out.println("TYPENAME: " + typeName); + if(m.getName().startsWith("get") || m.getName().startsWith("is")) { + Class containedClazz = propClass.getComponentType(); + typeName = "VList<" + ModelType.primitiveToBoxedType( + model. + convertModelTypeToDestination(containedClazz)) + ">"; + // System.out.println("TYPENAME: " + typeName); - packageName = "eu.mihosoft.vcollections"; + packageName = "eu.mihosoft.vcollections"; + genericPackageName = model. + convertModelPackageToDestination(TypeUtil.getPackageName(containedClazz)); + + genericTypeName = containedClazz.getSimpleName(); + } else { + Class containedClazz = propClass.getComponentType(); + typeName = + model.convertModelTypeToDestination(containedClazz) + "[]"; + packageName = ""; genericPackageName = model. convertModelPackageToDestination(TypeUtil.getPackageName(containedClazz)); - - genericTypeName = containedClazz.getSimpleName(); + } } else if (m.getGenericReturnType() != null) { ParameterizedType retType = null; diff --git a/gradle-plugin/src/main/groovy/eu/mihosoft/vmf/gradle/plugin/VMFPlugin.groovy b/gradle-plugin/src/main/groovy/eu/mihosoft/vmf/gradle/plugin/VMFPlugin.groovy index 6417ed62..d1bcf24b 100644 --- a/gradle-plugin/src/main/groovy/eu/mihosoft/vmf/gradle/plugin/VMFPlugin.groovy +++ b/gradle-plugin/src/main/groovy/eu/mihosoft/vmf/gradle/plugin/VMFPlugin.groovy @@ -195,11 +195,14 @@ class VMFPlugin implements Plugin { // potentially other dependencies needed to compile the model definitions String configName = sourceSet.getTaskName("vmfCompile", ""); - Dependency dep = project.dependencies.create( + Dependency dep1 = project.dependencies.create( "eu.mihosoft.vmf:vmf:$VMF_VERSION") + Dependency dep2 = project.dependencies.create( + "eu.mihosoft.vmf:vmf-runtime:$VMF_VERSION") def conf = project.configurations.maybeCreate(configName) conf.defaultDependencies { deps -> - deps.add(dep) + deps.add(dep1) + deps.add(dep2) } // 1) Add a new 'vmf' virtual directory mapping diff --git a/test-suite/src/test/vmf/eu/mihosoft/vmftest/complex/vmf_text/generated/miniclang/vmfmodel/MiniClangModel.java b/test-suite/src/test/vmf/eu/mihosoft/vmftest/complex/vmf_text/generated/miniclang/vmfmodel/MiniClangModel.java index e02477a5..a8ec012e 100644 --- a/test-suite/src/test/vmf/eu/mihosoft/vmftest/complex/vmf_text/generated/miniclang/vmfmodel/MiniClangModel.java +++ b/test-suite/src/test/vmf/eu/mihosoft/vmftest/complex/vmf_text/generated/miniclang/vmfmodel/MiniClangModel.java @@ -905,7 +905,8 @@ interface IntLiteral extends CodeElement { @InterfaceOnly interface ControlFlowChildNode { @DelegateTo(className="eu.mihosoft.vmftest.complex.vmf_text.generated.miniclang.ControlFlowChildNodeDelegate") - ControlFlowScope[] parentScopes(); + //ControlFlowScope[] parentScopes(); + eu.mihosoft.vcollections.VList parentScopes(); } @InterfaceOnly