diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java b/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java
index 643c09629..64133b931 100644
--- a/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java
+++ b/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java
@@ -1,11 +1,11 @@
/**
* Copyright 2016 SPeCS.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. under the License.
@@ -13,24 +13,22 @@
package org.lara.language.specification.dsl;
+import org.lara.language.specification.dsl.types.IType;
+
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
-import org.lara.language.specification.dsl.types.ArrayType;
-import org.lara.language.specification.dsl.types.IType;
-import org.lara.language.specification.dsl.types.PrimitiveClasses;
-
/**
* A basic class that contains a type and a name
- *
- * @author tiago
*
+ * @author tiago
*/
public class Attribute extends BaseNode implements Comparable {
private Declaration declaration;
private List parameters;
+ private List defs;
public Attribute(IType type, String name) {
this(type, name, new ArrayList<>());
@@ -39,8 +37,9 @@ public Attribute(IType type, String name) {
public Attribute(IType type, String name, List parameters) {
declaration = new Declaration(type, name);
this.parameters = parameters;
+ this.defs = new ArrayList<>();
}
-
+
public void addParameter(IType type, String name) {
addParameter(type, name, "");
}
@@ -49,6 +48,14 @@ public void addParameter(IType type, String name, String defaultValue) {
parameters.add(new Parameter(type, name, defaultValue));
}
+ public void addDef(Def def) {
+ defs.add(def);
+ }
+
+ public List getDefs() {
+ return defs;
+ }
+
public IType getType() {
return declaration.getType();
}
diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java b/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java
index 2584ee349..fb172646a 100644
--- a/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java
+++ b/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java
@@ -1,11 +1,11 @@
/**
* Copyright 2016 SPeCS.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. under the License.
@@ -13,12 +13,12 @@
package org.lara.language.specification.dsl.types;
+import org.lara.language.specification.dsl.BaseNode;
+
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
-import org.lara.language.specification.dsl.BaseNode;
-
public class EnumDef extends BaseNode implements IType {
private String name;
diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java b/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java
index 2144341b2..ced1bcf30 100644
--- a/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java
+++ b/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java
@@ -1,11 +1,11 @@
/**
* Copyright 2016 SPeCS.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. under the License.
@@ -77,6 +77,9 @@ public String getType() {
@Override
public String toString() {
- return values.stream().collect(Collectors.joining("| ", "[", "]"));
+ //return values.stream().collect(Collectors.joining("| ", "[", "]"));
+
+ // To keep compatibility with previous code, since it is dependent on this format
+ return values.stream().collect(Collectors.joining(",", "{", "}"));
}
}
diff --git a/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java b/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java
index c5d7041ef..0201e551a 100644
--- a/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java
+++ b/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java
@@ -245,9 +245,23 @@ private static Attribute getAttribute(XmlElement attributeNode, LanguageSpecific
parameterNode.getAttribute("name"), parameterNode.getAttribute("default"));
}
+ var defNodes = attributeNode.getElementsByName("def");
+
+ for (var defNode : defNodes) {
+ // If def does not have a type, use the attribute type
+ newAttribute.addDef(parseDef(defNode, type));
+ }
+
return newAttribute;
}
+ private static Def parseDef(XmlElement defNode, String attributeType) {
+ // Check if it has an optional 'type'
+ var type = defNode.getAttribute("type", attributeType);
+
+ return new Def(type);
+ }
+
private static String getType(XmlElement node) {
// Default type is "void"
return node.getAttribute("type", "void");
@@ -293,7 +307,7 @@ private static List convertSelects(LanguageSpecificationV2 langSpecV2,
String alias = selectNode.getAttribute("alias");
alias = alias.equals(selectClassName) ? "" : alias; // Is this necessary?
Select newSelect = new Select(selectJP, alias);
- newSelect.setToolTip(selectNode.getAttribute("tooltip"));
+ newSelect.setToolTip(selectNode.getAttribute("tooltip", null));
selects.add(newSelect);
}
diff --git a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/BaseGenerator.java b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/BaseGenerator.java
index a96ebc78b..e27129830 100644
--- a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/BaseGenerator.java
+++ b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/BaseGenerator.java
@@ -1,11 +1,11 @@
/**
* Copyright 2015 SPeCS.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. under the License.
@@ -13,16 +13,15 @@
package org.lara.interpreter.weaver.generator.generator;
-import java.io.File;
-import java.util.Arrays;
-
import org.lara.interpreter.weaver.generator.generator.utils.GenConstants;
import org.lara.language.specification.LanguageSpecification;
import org.lara.language.specification.dsl.JoinPointFactory;
import org.lara.language.specification.dsl.LanguageSpecificationV2;
-
import pt.up.fe.specs.util.SpecsCheck;
+import java.io.File;
+import java.util.Arrays;
+
public abstract class BaseGenerator {
private String outPackage;
@@ -54,10 +53,9 @@ public BaseGenerator(BaseGenerator baseGenerator) {
/**
* Generate the code for the given language specification
- *
- * @throws RuntimeException
- * if the language specification was not defined
+ *
* @return true if generation was successful; false otherwise
+ * @throws RuntimeException if the language specification was not defined
*/
public void generate() {
if (languageSpecification == null) {
@@ -69,10 +67,9 @@ public void generate() {
/**
* Print the generated code in files
- *
- * @throws RuntimeException
- * if the language specification was not defined
+ *
* @return true if generation was successful; false otherwise
+ * @throws RuntimeException if the language specification was not defined
*/
public void print() {
if (!generated) {
@@ -104,7 +101,7 @@ private void init() {
/**
* Initialize the generator based on another generator
- *
+ *
* @param baseGenerator
*/
private void init(BaseGenerator baseGenerator) {
@@ -127,7 +124,7 @@ private void init(BaseGenerator baseGenerator) {
/**
* Generate the code for the given language specification
- *
+ *
* @return
*/
protected abstract void generateCode();
@@ -196,7 +193,7 @@ public BaseGenerator nodeType(String className) {
/**
* Get the output package for the weaver and join points
- *
+ *
* @return
*/
public String getOutPackage() {
@@ -205,7 +202,7 @@ public String getOutPackage() {
/**
* Set the output package for the weaver and join points
- *
+ *
* @param outPackage
*/
public void setOutPackage(String outPackage) {
@@ -214,7 +211,7 @@ public void setOutPackage(String outPackage) {
/**
* Get the output dir for the generated files
- *
+ *
* @return
*/
public File getOutDir() {
@@ -223,7 +220,7 @@ public File getOutDir() {
/**
* Set the output dir for the generated files
- *
+ *
* @return
*/
public void setOutDir(File outDir) {
@@ -232,7 +229,7 @@ public void setOutDir(File outDir) {
/**
* Get the name of the weaver
- *
+ *
* @return
*/
public String getWeaverName() {
@@ -241,7 +238,7 @@ public String getWeaverName() {
/**
* Set the name of the weaver
- *
+ *
* @return
*/
public void setWeaverName(String weaverName) {
@@ -250,7 +247,7 @@ public void setWeaverName(String weaverName) {
/**
* See if the getters should be generated as abstract, and thus no field is generated
- *
+ *
* @return
*/
public boolean isAbstractGetters() {
@@ -259,7 +256,7 @@ public boolean isAbstractGetters() {
/**
* Should the generated code have events or not
- *
+ *
* @return
*/
public boolean hasEvents() {
@@ -268,7 +265,7 @@ public boolean hasEvents() {
/**
* Set if the getters should be generated as abstract, and thus no field is generated
- *
+ *
* @return
*/
public void setAbstractGetters(boolean abstractGetters) {
@@ -277,7 +274,7 @@ public void setAbstractGetters(boolean abstractGetters) {
/**
* Get the base AST node.
- *
+ *
* @return
*/
public String getNodeType() {
@@ -286,7 +283,7 @@ public String getNodeType() {
/**
* Get the base AST node name.
- *
+ *
* @return
*/
public String getNodeName() {
@@ -300,7 +297,7 @@ public String getNodeName() {
/**
* Set the base AST node.
- *
+ *
* @param nodeType
*/
public void setNodeType(String nodeType) {
@@ -309,7 +306,7 @@ public void setNodeType(String nodeType) {
/**
* Set the generic type of the join points
- *
+ *
* @param nodeType
*/
public void setNodeType(Class> nodeType) {
@@ -318,7 +315,7 @@ public void setNodeType(Class> nodeType) {
/**
* Should the generator show a graph in the end of the generation
- *
+ *
* @return
*/
public boolean isShowGraph() {
@@ -327,7 +324,6 @@ public boolean isShowGraph() {
/**
* Set if the generator should show a graph in the end of the generation
- *
*/
public void setShowGraph(boolean showGraph) {
this.showGraph = showGraph;
@@ -335,7 +331,7 @@ public void setShowGraph(boolean showGraph) {
/**
* The language specification for this generation
- *
+ *
* @return
*/
public LanguageSpecificationV2 getLanguageSpecificationV2() {
@@ -348,7 +344,7 @@ public LanguageSpecification getLanguageSpecification() {
/**
* Set the language specification for this generation
- *
+ *
* @param languageSpecification
*/
private void setLanguageSpecification(LanguageSpecificationV2 languageSpecification) {
@@ -362,21 +358,19 @@ public void setLanguageSpecificationOld(LanguageSpecification languageSpecificat
/**
* Create a language specification using the models defined in the given directory
- *
- * @param langSpecDirStr
- * the input folder
+ *
+ * @param langSpecDirStr the input folder
*/
public void setLanguageSpecification(File langSpecDir) {
languageSpecificationOld = LanguageSpecification.newInstance(langSpecDir, true);
- languageSpecification = JoinPointFactory.fromOld(languageSpecificationOld);
- // languageSpecification = LanguageSpecificationV2.newInstance(langSpecDir, true);
+ //languageSpecification = JoinPointFactory.fromOld(languageSpecificationOld);
+ languageSpecification = LanguageSpecificationV2.newInstance(langSpecDir);
}
/**
* Create a language specification using the models defined in the given directory name
- *
- * @param langSpecDirStr
- * name of the input folder
+ *
+ * @param langSpecDirStr name of the input folder
*/
public void setLanguageSpecification(String langSpecDirStr) {
final File langSpecDir = new File(langSpecDirStr);
@@ -431,8 +425,7 @@ public boolean isTemplatedGenerator() {
}
/**
- * @param concreteClassesPrefix
- * the concreteClassesPrefix to set
+ * @param concreteClassesPrefix the concreteClassesPrefix to set
*/
public void setConcreteClassesPrefix(String concreteClassesPrefix) {
this.concreteClassesPrefix = concreteClassesPrefix;
diff --git a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AbstractJoinPointClassGenerator.java b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AbstractJoinPointClassGenerator.java
index e3508743f..2c9d83479 100644
--- a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AbstractJoinPointClassGenerator.java
+++ b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AbstractJoinPointClassGenerator.java
@@ -13,7 +13,6 @@
package org.lara.interpreter.weaver.generator.generator.java.helpers;
-import org.lara.interpreter.exception.SelectException;
import org.lara.interpreter.weaver.generator.generator.java.JavaAbstractsGenerator;
import org.lara.interpreter.weaver.generator.generator.java.utils.GeneratorUtils;
import org.lara.interpreter.weaver.generator.generator.utils.GenConstants;
@@ -23,9 +22,7 @@
import org.lara.language.specification.artifactsmodel.schema.Artifact;
import org.lara.language.specification.artifactsmodel.schema.Attribute;
import org.lara.language.specification.dsl.JoinPointClass;
-import org.lara.language.specification.joinpointmodel.JoinPointModel;
import org.lara.language.specification.joinpointmodel.schema.JoinPointType;
-import org.lara.language.specification.joinpointmodel.schema.Select;
import org.specs.generators.java.classtypes.JavaClass;
import org.specs.generators.java.enums.Annotation;
import org.specs.generators.java.enums.JDocTag;
@@ -39,7 +36,6 @@
import org.specs.generators.java.types.JavaTypeFactory;
import org.specs.generators.java.utils.Utils;
-import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -48,12 +44,10 @@
*/
public class AbstractJoinPointClassGenerator extends GeneratorHelper {
- private final JoinPointType joinPoint;
private final JoinPointClass joinPointV2;
protected AbstractJoinPointClassGenerator(JavaAbstractsGenerator javaGenerator, JoinPointType joinPoint) {
super(javaGenerator);
- this.joinPoint = joinPoint;
this.joinPointV2 = javaGenerator.getLanguageSpecificationV2().getJoinPoint(joinPoint.getClazz());
}
@@ -185,11 +179,7 @@ private void addFieldsAndConstructors(JavaClass javaC) {
*/
private void addSelects(JavaClass javaC) {
- // if (!joinPoint.getSelect().isEmpty())
- // javaC.addImport("java.util.List");
-
- for (final Select sel : joinPoint.getSelect()) {
-
+ for (var sel : joinPointV2.getSelectsSelf()) {
addSelect(sel, javaC);
}
@@ -223,16 +213,13 @@ private void addActions(JavaClass javaC) {
* @param type
* @param javaC
*/
- private void addSelect(Select sel, JavaClass javaC) {
+ private void addSelect(org.lara.language.specification.dsl.Select sel, JavaClass javaC) {
final String joinPointPackage = javaGenerator.getJoinPointClassPackage();
- // final Method selectMethod = GeneratorUtils.generateSelectMethod(sel, joinPointPackage, true);
final Method selectMethod = GeneratorUtils.generateSelectMethodGeneric(sel, joinPointPackage);
javaC.add(selectMethod);
javaC.addImport(SelectOp.class);
-
- // addSelectWithTryCatch(selectName, javaC, selectMethod);
}
/**
@@ -242,6 +229,7 @@ private void addSelect(Select sel, JavaClass javaC) {
* @param javaC
* @param selectMethod
*/
+ /*
void addSelectWithTryCatch(String selectName, JavaClass javaC, final Method selectMethod) {
// Add the method used to encapsulate the output with an Optional, or
// encapsulate a thrown exception
@@ -263,6 +251,7 @@ void addSelectWithTryCatch(String selectName, JavaClass javaC, final Method sele
javaC.addImport(Collections.class);
javaC.addImport(SelectException.class);
}
+ */
/**
* Add code that calls to the super methods
@@ -273,31 +262,32 @@ void addSelectWithTryCatch(String selectName, JavaClass javaC, final Method sele
*/
private String addSuperMethods(JavaClass javaC) {
- final JoinPointType superType = JoinPointModel.toJoinPointType(joinPoint.getExtends());
- final JoinPointType superSuperType = JoinPointModel.toJoinPointType(superType.getExtends());
- final String superClassName = GenConstants.abstractPrefix() + Utils.firstCharToUpper(superType.getClazz());
+ var superType = joinPointV2.getExtendExplicit().orElseThrow(() -> new RuntimeException("Expected join point to explicitly extend another join point"));
+
+ final String superClassName = GenConstants.abstractPrefix() + Utils.firstCharToUpper(superType.getName());
final String fieldName = GenConstants.abstractPrefix().toLowerCase()
- + Utils.firstCharToUpper(superType.getClazz());
+ + Utils.firstCharToUpper(superType.getName());
final JavaType joinPointType = new JavaType(superClassName, javaGenerator.getJoinPointClassPackage());
javaC.add(new Field(joinPointType, fieldName, Privacy.PROTECTED));
final Constructor constructor = new Constructor(javaC);
constructor.addArgument(joinPointType, fieldName);
- if (!superType.equals(superSuperType)) {
+ if (superType.getExtendExplicit().isPresent()) {
constructor.appendCode("super(" + fieldName + ");" + ln());
}
constructor.appendCode("this." + fieldName + " = " + fieldName + ";");
// System.out.println("addSuperMethods: " + joinPoint.getClazz());
- GeneratorUtils.addSuperMethods(javaC, fieldName, javaGenerator, joinPoint);
+ GeneratorUtils.addSuperMethods(javaC, fieldName, javaGenerator, joinPointV2);
// Add global methods for global attributes
- var globalAttributes = javaGenerator.getLanguageSpecification().getArtifacts().getGlobalAttributes().values();
+ var globalAttributes = javaGenerator.getLanguageSpecificationV2().getGlobal().getAttributesSelf();
+
GeneratorUtils.addSuperGetters(javaC, fieldName, javaGenerator, globalAttributes);
// GeneratorUtils.addSuperMethods(javaC, fieldName, javaGenerator, superType.getClazz());
// addSuperGetters(javaC, fieldName, javaGenerator, parent);
// System.out.println("ABS JP: " + superType.getClazz());
// Update also here for actionImpl
- GeneratorUtils.addSuperActions(javaGenerator, javaC, superType.getClazz(), fieldName);
+ GeneratorUtils.addSuperActions(javaGenerator, javaC, superType.getName(), fieldName);
// GeneratorUtils.addSuperToString(javaC, fieldName); // Do not add toString(), JoinPoint already implements it,
// and this one has bugs (e.g., param shows 'decl')
// addSuperWeaverEngineSetter(javaC, fieldName);
diff --git a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AttributesEnumGenerator.java b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AttributesEnumGenerator.java
index 6dcb8c548..ae184662b 100644
--- a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AttributesEnumGenerator.java
+++ b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/helpers/AttributesEnumGenerator.java
@@ -13,10 +13,8 @@
package org.lara.interpreter.weaver.generator.generator.java.helpers;
-import org.lara.language.specification.LanguageSpecification;
import org.lara.language.specification.artifactsmodel.schema.Attribute;
import org.lara.language.specification.dsl.JoinPointClass;
-import org.lara.language.specification.joinpointmodel.schema.JoinPointType;
import org.specs.generators.java.classtypes.JavaClass;
import org.specs.generators.java.classtypes.JavaEnum;
import org.specs.generators.java.enums.Modifier;
@@ -35,7 +33,7 @@
import java.util.stream.Collectors;
public class AttributesEnumGenerator {
-
+
private JoinPointClass joinPointV2;
private JavaClass javaC;
@@ -50,28 +48,16 @@ public static void generate(JavaClass javaC, JoinPointClass joinPointV2) {
}
public void generate() {
- String clazz = joinPointV2.getName();
-/*
- List attributes = langSpec.getArtifacts()
- .getAttributesRecursively(clazz).stream().sorted((attribute, t1) -> attribute.getName().compareTo(t1.getName())).toList();
-
- System.out.println(clazz);
- System.out.println("OLD: " + langSpec.getArtifacts()
- .getAttributesRecursively(clazz).stream().map(Attribute::getName).sorted().toList());
- System.out.println("NEW: " + joinPointV2.getAttributes().stream().map(org.lara.language.specification.dsl.Attribute::getName).sorted().toList());
-*/
- // TODO: Sorting so that refactor output can be compared with previous output, can be removed after
- var attributes = joinPointV2.getAttributes().stream().sorted((attribute, t1) -> attribute.getName().compareTo(t1.getName())).toList();
+ var attributes = joinPointV2.getAttributes();
if (attributes.isEmpty()) {
return;
}
- String enumName = StringUtils.firstCharToUpper(clazz) + "Attributes";
+ String enumName = StringUtils.firstCharToUpper(joinPointV2.getName()) + "Attributes";
JavaEnum anEnum = new JavaEnum(enumName, javaC.getClassPackage());
anEnum.setPrivacy(Privacy.PROTECTED);
- //addAttributes(attributes, anEnum);
addAttributesV2(attributes, anEnum);
addNameField(anEnum);
diff --git a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/utils/GeneratorUtils.java b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/utils/GeneratorUtils.java
index a2b08d28a..df064d73e 100644
--- a/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/utils/GeneratorUtils.java
+++ b/WeaverGenerator/src/org/lara/interpreter/weaver/generator/generator/java/utils/GeneratorUtils.java
@@ -23,7 +23,6 @@
import org.lara.interpreter.weaver.interf.events.Stage;
import org.lara.language.specification.actionsmodel.schema.Action;
import org.lara.language.specification.actionsmodel.schema.Parameter;
-import org.lara.language.specification.artifactsmodel.schema.Artifact;
import org.lara.language.specification.artifactsmodel.schema.Attribute;
import org.lara.language.specification.artifactsmodel.schema.DefArgType;
import org.lara.language.specification.dsl.JoinPointClass;
@@ -46,10 +45,7 @@
import tdrc.utils.Pair;
import tdrc.utils.StringUtils;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Optional;
+import java.util.*;
import java.util.function.Function;
public class GeneratorUtils {
@@ -194,25 +190,17 @@ public static void createListOfAvailableActions(JavaClass javaC, JoinPointClass
* @param current
*/
public static void addSuperMethods(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- JoinPointType current) {
- // public static void addSuperMethods(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- // JoinPointType current, boolean calledOnBaseJp) {
- final String parentType = JoinPointModelConstructor.getJoinPointClass(current.getExtends());
- if (parentType == null || parentType.equals(current.getClazz())) {
+ JoinPointClass current) {
+
+ var parentV2 = current.getExtendExplicit().orElse(null);
+ if (parentV2 == null) {
return;
}
- // if (parentType == null || calledOnBaseJp) {
- // return;
- // }
- //
- // var newCalledOnBaseJp = parentType.equals(current.getClazz());
-
- final JoinPointType parent = generator.getLanguageSpecification().getJpModel().getJoinPoint(parentType);
- addSuperGetters(javaC, fieldName, generator, parent);
- addSuperSelect(javaC, fieldName, generator, parent);
- // addSuperMethods(javaC, fieldName, generator, parent, newCalledOnBaseJp);
- addSuperMethods(javaC, fieldName, generator, parent);
- addSuperDefs(javaC, fieldName, generator, parent);
+
+ addSuperGetters(javaC, fieldName, generator, parentV2);
+ addSuperSelect(javaC, fieldName, generator, parentV2);
+ addSuperMethods(javaC, fieldName, generator, parentV2);
+ addSuperDefs(javaC, fieldName, generator, parentV2);
}
/**
@@ -221,8 +209,8 @@ public static void addSuperMethods(JavaClass javaC, String fieldName, JavaAbstra
* @param parent
*/
public static void addSuperSelect(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- JoinPointType parent) {
- for (final Select sel : parent.getSelect()) {
+ JoinPointClass parent) {
+ for (var sel : parent.getSelectsSelf()) {
final Method selectMethod = generateSelectMethod(sel, generator.getJoinPointClassPackage(), false);
selectMethod.add(Annotation.OVERRIDE);
@@ -244,35 +232,33 @@ public static void addSuperToString(JavaClass javaC, String fieldName) {
javaC.add(toStringMethod);
}
- public static void addSuperGetters(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- JoinPointType parent) {
-
- final Artifact artifact = generator.getLanguageSpecification().getArtifacts().getArtifact(parent.getClazz());
- if (artifact == null) {
- return;
- }
+ public static void addSuperGetters(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
+ JoinPointClass parent) {
- addSuperGetters(javaC, fieldName, generator, artifact.getAttribute());
+ addSuperGetters(javaC, fieldName, generator, parent.getAttributesSelf());
}
public static void addSuperGetters(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- Collection attributes) {
+ List attributes) {
+ // TODO: Remove sort
+ Collections.sort(attributes, (attribute, t1) -> attribute.getName().compareTo(t1.getName()));
// System.out.println("JP: " + javaC.getName());
- for (final Attribute attribute : attributes) {
+ //for (final Attribute attribute : attributes) {
+ for (var attributeV2 : attributes) {
+
// System.out.println("ATTR:" + attribute.getName());
- String attrClassStr = attribute.getType().trim();
+ String attrClassStr = attributeV2.getReturnType().trim();
if (attrClassStr.startsWith("{")) { // then it is an enumerator
- // attrClassStr = extractEnumName(attribute.getName());
attrClassStr = String.class.getSimpleName();
}
// if (ObjectOfPrimitives.contains(attrClassStr))
// attrClassStr = ObjectOfPrimitives.getPrimitive(attrClassStr);
- String name = attribute.getName();
+ String name = attributeV2.getName();
// JavaType type = ConvertUtils.getConvertedType(attrClassStr, generator);
JavaType type = ConvertUtils.getAttributeConvertedType(attrClassStr, generator);
@@ -284,7 +270,8 @@ public static void addSuperGetters(JavaClass javaC, String fieldName, JavaAbstra
if (generator.hasImplMode() && !type.isArray()) {
name += GenConstants.getImplementationSufix();
}
- final Method getter = createSuperGetter(sanitizedName, name, type, fieldName, attribute.getParameter(),
+
+ final Method getter = createSuperGetter(sanitizedName, name, type, fieldName, attributeV2.getParameters(),
generator);
getter.add(Annotation.OVERRIDE);
javaC.add(getter);
@@ -423,16 +410,17 @@ private static Method createGetter(String attr, String originalName, JavaType ge
* @return
*/
private static Method createSuperGetter(String attr, String originalName, JavaType getAttrType, String superField,
- List list,
+ List listV2,
JavaAbstractsGenerator generator) {
- if (list != null && !list.isEmpty()) {
+ if (!listV2.isEmpty()) {
final Method getAttribute = new Method(getAttrType, originalName);
// getAttribute.addModifier(Modifier.ABSTRACT);
getAttribute.appendComment("Get value on attribute " + attr);
getAttribute.addJavaDocTag(JDocTag.RETURN, "the attribute's value");
getAttribute.appendCode("return this." + superField + "." + originalName + "(");
- for (org.lara.language.specification.artifactsmodel.schema.Parameter parameter : list) {
+
+ for (var parameter : listV2) {
JavaType type = ConvertUtils.getConvertedType(parameter.getType(), generator);
getAttribute.addArgument(type, parameter.getName());
@@ -734,6 +722,7 @@ public static Method generateActionImplMethod(Method original, Action action,
* @param selectName
* @param type
* @return
+ * @deprecated
*/
public static Method generateSelectMethod(Select sel, String _package, boolean isAbstract) {
final String selectName = sel.getAlias();
@@ -756,12 +745,39 @@ public static Method generateSelectMethod(Select sel, String _package, boolean i
return selectMethod;
}
+ /**
+ * @param selectName
+ * @param type
+ * @return
+ */
+ public static Method generateSelectMethod(org.lara.language.specification.dsl.Select sel, String _package, boolean isAbstract) {
+ final String selectName = sel.getSelectName();
+ final String type = sel.getClazz().getName();
+ //final String type = JoinPointModelConstructor.getJoinPointClass(sel);
+ final String firstCharToUpper = Utils.firstCharToUpper(selectName);
+ final String methodName = "select" + firstCharToUpper;
+ final JavaType baseType = generateJoinPointBaseType(_package, type);
+ final JavaGenericType genType = JavaTypeFactory.getWildExtendsType(baseType);
+ final JavaType listType = JavaTypeFactory.getListJavaType(genType);
+ // Method selectMethod = new Method("List extends A" +
+ // typeFirstCharToUpper + ">", methodName);
+ final Method selectMethod = new Method(listType, methodName);
+ selectMethod.addJavaDocTag(JDocTag.RETURN);
+ if (isAbstract) {
+ selectMethod.add(Modifier.ABSTRACT);
+ }
+ String comment = sel.getToolTip().orElse("Method used by the lara interpreter to select " + selectName + "s");
+ selectMethod.appendComment(comment);
+ return selectMethod;
+ }
+
/**
* Generic implementation of the select method which uses the select() function in the global join point class.
*
* @param selectName
* @param type
* @return
+ * @deprecated
*/
public static Method generateSelectMethodGeneric(Select sel, String _package) {
final String selectName = sel.getAlias();
@@ -785,6 +801,33 @@ public static Method generateSelectMethodGeneric(Select sel, String _package) {
return selectMethod;
}
+ /**
+ * Generic implementation of the select method which uses the select() function in the global join point class.
+ *
+ * @param selectName
+ * @param type
+ * @return
+ */
+ public static Method generateSelectMethodGeneric(org.lara.language.specification.dsl.Select sel, String _package) {
+ final String selectName = sel.getSelectName();
+ final String type = sel.getClazz().getName();
+ final String firstCharToUpper = Utils.firstCharToUpper(selectName);
+ final String methodName = "select" + firstCharToUpper;
+ final JavaType baseType = generateJoinPointBaseType(_package, type);
+ final JavaGenericType genType = JavaTypeFactory.getWildExtendsType(baseType);
+ final JavaType listType = JavaTypeFactory.getListJavaType(genType);
+ // Method selectMethod = new Method("List extends A" +
+ // typeFirstCharToUpper + ">", methodName);
+ final Method selectMethod = new Method(listType, methodName);
+ selectMethod.addJavaDocTag(JDocTag.RETURN);
+ String comment = sel.getToolTip().orElse("Default implementation of the method used by the lara interpreter to select " + selectName + "s");
+ selectMethod.appendComment(comment);
+
+ selectMethod.appendCode("return select(" + baseType + ".class, SelectOp.DESCENDANTS);");
+
+ return selectMethod;
+ }
+
/**
* @param _package
* @param typeFirstCharToUpper
@@ -1134,6 +1177,14 @@ public static void generateDefMethods(Attribute attribute, JavaType returnType,
}
+ /**
+ * @param attribute
+ * @param returnType
+ * @param javaC
+ * @param javaGenerator
+ * @param codeProvider
+ * @deprecated
+ */
public static void generateDefForType(Attribute attribute, JavaType returnType, JavaClass javaC,
JavaAbstractsGenerator javaGenerator, Function codeProvider) {
List defs = attribute.getDef();
@@ -1163,31 +1214,60 @@ public static void generateDefForType(Attribute attribute, JavaType returnType,
}
}
+ public static void generateDefForType(org.lara.language.specification.dsl.Attribute attribute, JavaType returnType, JavaClass javaC,
+ JavaAbstractsGenerator javaGenerator, Function codeProvider) {
+ var defs = attribute.getDefs();
+ if (defs.isEmpty()) {
+ return;
+ }
+ List processedTypes = SpecsCollections.newArrayList();
+ for (var defType : defs) {
+ String type = defType.getType();
+ if (processedTypes.contains(type)) {
+ continue;
+ }
+ JavaType defJavaType;
+ if (type == null) {
+ defJavaType = returnType.clone();
+ } else {
+ defJavaType = ConvertUtils.getAttributeConvertedType(type, javaGenerator);
+ }
+ Method defAttrImpl = new Method(JavaTypeFactory.getVoidType(),
+ GenConstants.getDefAttributeImplName(attribute.getName()));
+ defAttrImpl.setPrivacy(Privacy.PUBLIC);
+ defAttrImpl.addArgument(defJavaType, "value");
+ defAttrImpl.appendCode(codeProvider.apply(defJavaType.getName()));
+
+ processedTypes.add(type);
+ javaC.add(defAttrImpl);
+ }
+ }
+
public static void addSuperDefs(JavaClass javaC, String fieldName, JavaAbstractsGenerator generator,
- JoinPointType parent) {
+ JoinPointClass parent) {
- final Artifact artifact = generator.getLanguageSpecification().getArtifacts().getArtifact(parent.getClazz());
- if (artifact != null) {
- for (final Attribute attribute : artifact.getAttribute()) {
- List defs = attribute.getDef();
- if (defs.isEmpty()) {
- continue;
- }
- String attrClassStr = attribute.getType().trim();
+ //for (var attribute : parent.getAttributesSelf().stream().sorted((attribute, t1) -> attribute.getName().compareTo(t1.getName())).toList()) {
+ for (var attribute : parent.getAttributesSelf()) {
- if (attrClassStr.startsWith("{")) { // then it is an enumerator
- // attrClassStr = extractEnumName(attribute.getName());
- attrClassStr = String.class.getSimpleName();
- }
- String defMethodName = GenConstants.getDefAttributeImplName(attribute);
- JavaType type = ConvertUtils.getAttributeConvertedType(attrClassStr, generator);
+ var defs = attribute.getDefs();
+ if (defs.isEmpty()) {
+ continue;
+ }
+ String attrClassStr = attribute.getReturnType().trim();
- Function codeProvider = t -> "this." + fieldName + "." + defMethodName + "(value);";
- generateDefForType(attribute, type, javaC, generator, codeProvider);
+ if (attrClassStr.startsWith("{")) { // then it is an enumerator
+ attrClassStr = String.class.getSimpleName();
}
+ String defMethodName = GenConstants.getDefAttributeImplName(attribute.getName());
+ JavaType type = ConvertUtils.getAttributeConvertedType(attrClassStr, generator);
+
+ Function codeProvider = t -> "this." + fieldName + "." + defMethodName + "(value);";
+ generateDefForType(attribute, type, javaC, generator, codeProvider);
}
+
}
+
public static void createDefImpl(JavaClass javaC, boolean isFinal,
List attributes, JavaAbstractsGenerator javaGenerator) {
Method defMethod = new Method(JavaTypeFactory.getVoidType(), GenConstants.withImpl("def"));