From e87f6c0af49047ffe9d58d7c224cbaf341b20f65 Mon Sep 17 00:00:00 2001 From: rschoene <rene.schoene@tu-dresden.de> Date: Tue, 6 Sep 2022 13:25:48 +0200 Subject: [PATCH] restructure aspect files --- dumpAst/src/main/jastadd/ClassAnalysis.jrag | 179 +++++++++ dumpAst/src/main/jastadd/Frontend.jrag | 56 +++ ...{GenerationMustache.jrag => Mustache.jrag} | 2 +- dumpAst/src/main/jastadd/TemplateContext.jrag | 113 ++++++ .../{GenerationToYaml.jrag => ToYaml.jrag} | 6 +- ...{GenerationBackend.jadd => Transform.jadd} | 349 +----------------- .../{GenerationCommon.jrag => Util.jrag} | 10 +- 7 files changed, 363 insertions(+), 352 deletions(-) create mode 100644 dumpAst/src/main/jastadd/ClassAnalysis.jrag create mode 100644 dumpAst/src/main/jastadd/Frontend.jrag rename dumpAst/src/main/jastadd/{GenerationMustache.jrag => Mustache.jrag} (98%) create mode 100644 dumpAst/src/main/jastadd/TemplateContext.jrag rename dumpAst/src/main/jastadd/{GenerationToYaml.jrag => ToYaml.jrag} (97%) rename dumpAst/src/main/jastadd/{GenerationBackend.jadd => Transform.jadd} (51%) rename dumpAst/src/main/jastadd/{GenerationCommon.jrag => Util.jrag} (78%) diff --git a/dumpAst/src/main/jastadd/ClassAnalysis.jrag b/dumpAst/src/main/jastadd/ClassAnalysis.jrag new file mode 100644 index 0000000..80176f2 --- /dev/null +++ b/dumpAst/src/main/jastadd/ClassAnalysis.jrag @@ -0,0 +1,179 @@ +aspect ClassAnalysis { + + syn nta ClassAnalysisResult DumpAst.analyzeClass(java.lang.Class<?> clazz) { + ClassAnalysisResult result = new ClassAnalysisResult(); + String clazzName = clazz.getSimpleName(); + java.util.List<String> targetOrder = targetOrder(clazz); + methodLoop: for (java.lang.reflect.Method method : clazz.getMethods()) { + for (java.lang.annotation.Annotation annotation : method.getAnnotations()) { + String canonicalName = annotation.annotationType().getCanonicalName(); + if (canonicalName.startsWith(astNodeAnnotationPrefix())) { + String simpleName = annotation.annotationType().getSimpleName(); + String contextNameToAdd = null; + AnalysedMethod containmentMethodToAdd = null; + switch (simpleName) { + case "Child": + contextNameToAdd = invokeName(annotation); + NormalSingleChildMethod singleChildMethod = new NormalSingleChildMethod(); + singleChildMethod.setMethod(method); + singleChildMethod.setName(contextNameToAdd); + containmentMethodToAdd = singleChildMethod; + break; + case "OptChild": + contextNameToAdd = invokeName(annotation); + try { + // the annotated method is "get???Opt", but we want "get???" and "has???" + java.lang.reflect.Method realGetter = clazz.getMethod("get" + contextNameToAdd); + java.lang.reflect.Method checkMethod = clazz.getMethod("has" + contextNameToAdd); + NormalOptChildMethod normalOptChildMethod = new NormalOptChildMethod(); + normalOptChildMethod.setMethod(realGetter); + normalOptChildMethod.setCheckMethod(checkMethod); + normalOptChildMethod.setName(contextNameToAdd); + containmentMethodToAdd = normalOptChildMethod; + } catch (NoSuchMethodException e) { + System.err.println("Could not find getter for Opt-child " + contextNameToAdd + " in " + clazzName); + throw new RuntimeException(e); + } + break; + case "ListChild": + String listChildName = invokeName(annotation); + contextNameToAdd = listChildName; + NormalListChildMethod normalListChildMethod = new NormalListChildMethod(); + normalListChildMethod.setMethod(method); + normalListChildMethod.setName(listChildName); + containmentMethodToAdd = normalListChildMethod; + break; + case "Token": + // heuristic for relations + String tokenName = invokeName(annotation); + if (tokenName.startsWith("_impl_")) { + String relationName = titleCase(tokenName.substring(6)); + try { + java.lang.reflect.Method relationMethod = clazz.getMethod("get" + relationName); + // normal get + token-name -> singleRelation + SingleRelationMethod singleRelationMethod = new SingleRelationMethod(); + singleRelationMethod.setMethod(relationMethod); + singleRelationMethod.setName(relationName); + result.addOtherMethod(singleRelationMethod); + continue; + } catch (NoSuchMethodException e) { + // ignore, but we know this is probably not a single relation + } + // try list-relation next + try { + java.lang.reflect.Method relationMethod = clazz.getMethod("get" + relationName + "List"); + // normal get + token-name + "List" -> listRelation + ListRelationMethod listRelationMethod = new ListRelationMethod(); + listRelationMethod.setMethod(relationMethod); + listRelationMethod.setName(relationName); + result.addOtherMethod(listRelationMethod); + continue; + } catch (NoSuchMethodException e) { + // ignore, but we know this is probably not a relation at all + } + } + IntrinsicTokenMethod tokenMethod = new IntrinsicTokenMethod(); + tokenMethod.setMethod(method); + tokenMethod.setName(tokenName); + result.addOtherMethod(tokenMethod); + break; + case "Attribute": + if (method.getParameterCount() > 0) { + // ignore parametrized attributes + continue; + } + String attributeName = method.getName(); + boolean isNTA = (boolean) invokeMethod("isNTA", annotation); + if (isNTA) { + // remove leading "get" + if (attributeName.startsWith("get")) { + attributeName = attributeName.substring(3); + } + // remove trailing "List" + if (attributeName.endsWith("List")) { + attributeName = attributeName.substring(0, attributeName.length() - 4); + } + if (Iterable.class.isAssignableFrom(method.getReturnType())) { + NTAListChildMethod ntaListChildMethod = new NTAListChildMethod(); + ntaListChildMethod.setMethod(method); + ntaListChildMethod.setName(attributeName); + result.addOtherMethod(ntaListChildMethod); + } else { + NTASingleChildMethod ntaSingleChildMethod = new NTASingleChildMethod(); + ntaSingleChildMethod.setMethod(method); + ntaSingleChildMethod.setName(attributeName); + result.addOtherMethod(ntaSingleChildMethod); + } + } else { + // normal attribute + AttributeMethod attributeMethod = new AttributeMethod(); + attributeMethod.setMethod(method); + attributeMethod.setName(attributeName); + result.addOtherMethod(attributeMethod); + } + break; + } + if (containmentMethodToAdd != null) { + int indexOfContextInTarget = targetOrder.indexOf(contextNameToAdd); + if (indexOfContextInTarget == 0) { + result.getContainmentMethodList().insertChild(containmentMethodToAdd, 0); + continue methodLoop; + } + if (indexOfContextInTarget == targetOrder.size() - 1) { + result.addContainmentMethod(containmentMethodToAdd); + continue methodLoop; + } + + for (int i = 0, size = result.getNumContainmentMethod(); i < size; i++) { + String currentContextName = result.getContainmentMethod(i).getName(); + int indexOfCurrentInTarget = targetOrder.indexOf(currentContextName); + if (indexOfCurrentInTarget > indexOfContextInTarget) { + result.getContainmentMethodList().insertChild(containmentMethodToAdd, i); + continue methodLoop; + } + } + result.addContainmentMethod(containmentMethodToAdd); + } + } + } + } + return result; + } + + syn java.util.List<String> DumpAst.targetOrder(Class<?> clazz) { + for (java.lang.reflect.Constructor<?> method : clazz.getConstructors()) { + for (java.lang.annotation.Annotation annotation : method.getAnnotations()) { + String canonicalName = annotation.annotationType().getCanonicalName(); + if (canonicalName.startsWith(astNodeAnnotationPrefix())) { + String simpleName = annotation.annotationType().getSimpleName(); + if (simpleName.equals("Constructor")) { + return java.util.Arrays.asList((String[]) invokeMethod("name", annotation)); + } + } + } + } + // there is no constructor with an annotation, iff the nonterminal has no children + return null; + } + + private static String DumpAst.invokeName(java.lang.annotation.Annotation annotation) { + return (String) invokeMethod("name", annotation); + } + + private static Object DumpAst.invokeMethod(String name, java.lang.annotation.Annotation annotation) { + try { + return annotation.annotationType().getMethod(name).invoke(annotation); + } catch (java.lang.reflect.InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + // --- astNodeAnnotationPrefix --- + syn String DumpAst.astNodeAnnotationPrefix() = getPackageName() + ".ASTNodeAnnotation"; + inh String DumpNode.astNodeAnnotationPrefix(); + eq DumpAst.getDumpNode().astNodeAnnotationPrefix() = astNodeAnnotationPrefix(); +} diff --git a/dumpAst/src/main/jastadd/Frontend.jrag b/dumpAst/src/main/jastadd/Frontend.jrag new file mode 100644 index 0000000..f5bf7c2 --- /dev/null +++ b/dumpAst/src/main/jastadd/Frontend.jrag @@ -0,0 +1,56 @@ +aspect Frontend { + + // --- match{In,Ex}cludePatternCollection --- + syn PatternCollection BuildConfig.matchIncludePatternCollection(String typeName) { + for (TypePatternCollectionMapping mapping : getIncludeTypePatternList()) { + if (matches(mapping.typePattern(), typeName)) { + return mapping.getPatternCollection(); + } + } + return null; + } + syn PatternCollection BuildConfig.matchExcludePatternCollection(String typeName) { + for (TypePatternCollectionMapping mapping : getExcludeTypePatternList()) { + if (matches(mapping.typePattern(), typeName)) { + return mapping.getPatternCollection(); + } + } + return null; + } + + static StyleInformation StyleInformation.createDefault() { + StyleInformation result = new StyleInformation(); + result.setNameMethod(n -> n == null ? "null" : n.getClass().getSimpleName() + "@" + Integer.toHexString(n.hashCode())); + result.setBackgroundColorMethod(n -> ""); + result.setTextColorMethod(n -> ""); + result.setStereotypeMethod(n -> ""); + result.setComputedColor("blue"); + return result; + } + + @FunctionalInterface + public interface StyleMethod<ASTNODE> { + String get(ASTNODE node); + } + + @FunctionalInterface + public interface IncludeRelationMethod<ASTNODE> { + boolean shouldInclude(ASTNODE sourceNode, ASTNODE targetNode, String roleName); + } + + @FunctionalInterface + public interface IncludeChildMethod<ASTNODE> { + boolean shouldInclude(ASTNODE parentNode, ASTNODE childNode, String contextName); + } + + @FunctionalInterface + public interface IncludeAttributeMethod<ASTNODE> { + boolean shouldInclude(ASTNODE node, String attributeName, boolean isNTA, java.util.function.Supplier<Object> value); + } + + @FunctionalInterface + public interface IncludeTokenMethod<ASTNODE> { + boolean shouldInclude(ASTNODE node, String tokenName, Object value); + } + +} diff --git a/dumpAst/src/main/jastadd/GenerationMustache.jrag b/dumpAst/src/main/jastadd/Mustache.jrag similarity index 98% rename from dumpAst/src/main/jastadd/GenerationMustache.jrag rename to dumpAst/src/main/jastadd/Mustache.jrag index cf9f534..40be7d9 100644 --- a/dumpAst/src/main/jastadd/GenerationMustache.jrag +++ b/dumpAst/src/main/jastadd/Mustache.jrag @@ -1,4 +1,4 @@ -aspect GenerationMustache { +aspect Mustache { syn String DumpAst.toPlantUml() { StringBuilder sb = new StringBuilder(); com.github.mustachejava.reflect.ReflectionObjectHandler roh = new com.github.mustachejava.reflect.ReflectionObjectHandler() { diff --git a/dumpAst/src/main/jastadd/TemplateContext.jrag b/dumpAst/src/main/jastadd/TemplateContext.jrag new file mode 100644 index 0000000..e8d5549 --- /dev/null +++ b/dumpAst/src/main/jastadd/TemplateContext.jrag @@ -0,0 +1,113 @@ +aspect TemplateContext { + + // --- debug --- (mustache has printConfig as context) + syn boolean PrintConfig.debug() = buildConfig().getDebug(); + + // --- isNull --- + syn boolean DumpNode.isNull() { + return getObject() == null; + } + + // --- isAstNode --- + syn boolean DumpNode.isAstNode() { + if (getObject() == null) { + return false; + } + Class<?> clazz = getObject().getClass(); + for (java.lang.reflect.Method method : clazz.getMethods()) { + if ("init$Children".equals(method.getName()) && method.getParameterCount() == 0) { + return true; + } + } + return false; + } + + // --- NTA: InvisiblePath --- + syn InvisiblePath DumpNode.getInvisiblePath() { + InvisiblePath result = new InvisiblePath(); + for (DumpNode successor : reachableThroughInvisible()) { + result.addInnerRelationDumpNode(new InnerRelationDumpNode(successor)); + } + return result; + } + + // --- reachableThroughInvisible --- + syn java.util.List<DumpNode> DumpNode.reachableThroughInvisible() { + java.util.List<DumpNode> result = new java.util.ArrayList<>(); + for (DumpChildNode childNode : getDumpChildNodeList()) { + for (DumpNode inner : childNode.innerNodes(false)) { + if (inner != null && inner.getInvisible()) { + result.addAll(inner.reachableThroughInvisible()); + } else if (this.getInvisible()) { + result.add(inner); + } + } + } + return result; + } + + // --- labelAndTextColor --- + syn String DumpNode.labelAndTextColor() { + if (getTextColor().isEmpty()) { + return getLabel(); + } else { + return "<color:" + getTextColor() + ">" + getLabel() + "</color>"; + } + } + + // --- stereotypeList --- + syn String DumpNode.stereotypeList() { + StringBuilder sb = new StringBuilder(getManualStereotypes()); + if (!automaticStereotypes().isEmpty()) { + // add automatic stereotypes, if there are any + if (!getManualStereotypes().isEmpty()) { + // add a comma between manual and automatic, if both are present + sb.append(","); + } + sb.append(automaticStereotypes()); + } + String manualAndAutomaticStereotypes = sb.toString(); + if (manualAndAutomaticStereotypes.isEmpty()) { + return ""; + } + sb = new StringBuilder(); + for (String stereotype : manualAndAutomaticStereotypes.split(",")) { + sb.append(" <<").append(stereotype).append(">>"); + } + return sb.toString(); + } + // --- manualAndAutomaticStereotypes --- + syn String DumpNode.automaticStereotypes() = getComputed() ? "NTA" : ""; + + // --- myChildren --- + syn java.util.List<DumpNode> DumpNode.myChildren() { + java.util.List<DumpNode> result = new java.util.ArrayList<>(); + for (DumpChildNode childNode : getDumpChildNodeList()) { + for (DumpNode inner : childNode.innerNodes(true)) { + result.add(inner); + } + } + return result; + } + + // --- successor --- + syn DumpNode DumpNode.successor() { + if (container() == null) { + // not contained + return null; + } + java.util.List<DumpNode> siblingsAndMe = container().myChildren(); + int indexOfMe = siblingsAndMe.indexOf(this); + if (indexOfMe == siblingsAndMe.size() - 1) { + // last child + return null; + } + return siblingsAndMe.get(indexOfMe + 1); + } + + // --- hasSuccessor --- + syn boolean DumpNode.hasSuccessor() = successor() != null; + + syn String DumpAst.computedColor() = getBuildConfig().getStyleInformation().getComputedColor(); + +} diff --git a/dumpAst/src/main/jastadd/GenerationToYaml.jrag b/dumpAst/src/main/jastadd/ToYaml.jrag similarity index 97% rename from dumpAst/src/main/jastadd/GenerationToYaml.jrag rename to dumpAst/src/main/jastadd/ToYaml.jrag index 5f3d3b2..0df35a8 100644 --- a/dumpAst/src/main/jastadd/GenerationToYaml.jrag +++ b/dumpAst/src/main/jastadd/ToYaml.jrag @@ -1,4 +1,4 @@ -aspect GenerationToYaml { +aspect ToYaml { syn String DumpAst.printYaml(boolean prependCreationComment) { Document doc = new Document(); doc.setRootElement(this.toYaml(false)); @@ -240,7 +240,9 @@ aspect GenerationToYaml { if (value.isEmpty()) { return StringElement.of(value); } - return refined(value); + return containsAny(value, ",#[{\"\n") ? + StringElement.of(value.replace("\n", "\\n").replace("\"", "\\\"")) : + ValueElement.of(value); } } diff --git a/dumpAst/src/main/jastadd/GenerationBackend.jadd b/dumpAst/src/main/jastadd/Transform.jadd similarity index 51% rename from dumpAst/src/main/jastadd/GenerationBackend.jadd rename to dumpAst/src/main/jastadd/Transform.jadd index bef0917..747a59b 100644 --- a/dumpAst/src/main/jastadd/GenerationBackend.jadd +++ b/dumpAst/src/main/jastadd/Transform.jadd @@ -1,4 +1,4 @@ -aspect GenerationBackend { +aspect Transform { class DumpAst { enum Source { ROOT, NORMAL, RELATION @@ -316,169 +316,6 @@ aspect GenerationBackend { node.setManualStereotypes(getBuildConfig().getStyleInformation().getStereotypeMethod().get(obj)); } - syn nta ClassAnalysisResult DumpAst.analyzeClass(java.lang.Class<?> clazz) { - ClassAnalysisResult result = new ClassAnalysisResult(); - String clazzName = clazz.getSimpleName(); - java.util.List<String> targetOrder = targetOrder(clazz); - methodLoop: for (java.lang.reflect.Method method : clazz.getMethods()) { - for (java.lang.annotation.Annotation annotation : method.getAnnotations()) { - String canonicalName = annotation.annotationType().getCanonicalName(); - if (canonicalName.startsWith(astNodeAnnotationPrefix())) { - String simpleName = annotation.annotationType().getSimpleName(); - String contextNameToAdd = null; - AnalysedMethod containmentMethodToAdd = null; - switch (simpleName) { - case "Child": - contextNameToAdd = invokeName(annotation); - NormalSingleChildMethod singleChildMethod = new NormalSingleChildMethod(); - singleChildMethod.setMethod(method); - singleChildMethod.setName(contextNameToAdd); - containmentMethodToAdd = singleChildMethod; - break; - case "OptChild": - contextNameToAdd = invokeName(annotation); - try { - // the annotated method is "get???Opt", but we want "get???" and "has???" - java.lang.reflect.Method realGetter = clazz.getMethod("get" + contextNameToAdd); - java.lang.reflect.Method checkMethod = clazz.getMethod("has" + contextNameToAdd); - NormalOptChildMethod normalOptChildMethod = new NormalOptChildMethod(); - normalOptChildMethod.setMethod(realGetter); - normalOptChildMethod.setCheckMethod(checkMethod); - normalOptChildMethod.setName(contextNameToAdd); - containmentMethodToAdd = normalOptChildMethod; - } catch (NoSuchMethodException e) { - System.err.println("Could not find getter for Opt-child " + contextNameToAdd + " in " + clazzName); - throw new RuntimeException(e); - } - break; - case "ListChild": - String listChildName = invokeName(annotation); - contextNameToAdd = listChildName; - NormalListChildMethod normalListChildMethod = new NormalListChildMethod(); - normalListChildMethod.setMethod(method); - normalListChildMethod.setName(listChildName); - containmentMethodToAdd = normalListChildMethod; - break; - case "Token": - // heuristic for relations - String tokenName = invokeName(annotation); - if (tokenName.startsWith("_impl_")) { - String relationName = titleCase(tokenName.substring(6)); - try { - java.lang.reflect.Method relationMethod = clazz.getMethod("get" + relationName); - // normal get + token-name -> singleRelation - SingleRelationMethod singleRelationMethod = new SingleRelationMethod(); - singleRelationMethod.setMethod(relationMethod); - singleRelationMethod.setName(relationName); - result.addOtherMethod(singleRelationMethod); - continue; - } catch (NoSuchMethodException e) { - // ignore, but we know this is probably not a single relation - } - // try list-relation next - try { - java.lang.reflect.Method relationMethod = clazz.getMethod("get" + relationName + "List"); - // normal get + token-name + "List" -> listRelation - ListRelationMethod listRelationMethod = new ListRelationMethod(); - listRelationMethod.setMethod(relationMethod); - listRelationMethod.setName(relationName); - result.addOtherMethod(listRelationMethod); - continue; - } catch (NoSuchMethodException e) { - // ignore, but we know this is probably not a relation at all - } - } - IntrinsicTokenMethod tokenMethod = new IntrinsicTokenMethod(); - tokenMethod.setMethod(method); - tokenMethod.setName(tokenName); - result.addOtherMethod(tokenMethod); - break; - case "Attribute": - if (method.getParameterCount() > 0) { - // ignore parametrized attributes - continue; - } - String attributeName = method.getName(); - boolean isNTA = (boolean) invokeMethod("isNTA", annotation); - if (isNTA) { - // remove leading "get" - if (attributeName.startsWith("get")) { - attributeName = attributeName.substring(3); - } - // remove trailing "List" - if (attributeName.endsWith("List")) { - attributeName = attributeName.substring(0, attributeName.length() - 4); - } - if (Iterable.class.isAssignableFrom(method.getReturnType())) { - NTAListChildMethod ntaListChildMethod = new NTAListChildMethod(); - ntaListChildMethod.setMethod(method); - ntaListChildMethod.setName(attributeName); - result.addOtherMethod(ntaListChildMethod); - } else { - NTASingleChildMethod ntaSingleChildMethod = new NTASingleChildMethod(); - ntaSingleChildMethod.setMethod(method); - ntaSingleChildMethod.setName(attributeName); - result.addOtherMethod(ntaSingleChildMethod); - } - } else { - // normal attribute - AttributeMethod attributeMethod = new AttributeMethod(); - attributeMethod.setMethod(method); - attributeMethod.setName(attributeName); - result.addOtherMethod(attributeMethod); - } - break; - } - if (containmentMethodToAdd != null) { - int indexOfContextInTarget = targetOrder.indexOf(contextNameToAdd); - if (indexOfContextInTarget == 0) { - result.getContainmentMethodList().insertChild(containmentMethodToAdd, 0); - continue methodLoop; - } - if (indexOfContextInTarget == targetOrder.size() - 1) { - result.addContainmentMethod(containmentMethodToAdd); - continue methodLoop; - } - - for (int i = 0, size = result.getNumContainmentMethod(); i < size; i++) { - String currentContextName = result.getContainmentMethod(i).getName(); - int indexOfCurrentInTarget = targetOrder.indexOf(currentContextName); - if (indexOfCurrentInTarget > indexOfContextInTarget) { - result.getContainmentMethodList().insertChild(containmentMethodToAdd, i); - continue methodLoop; - } - } - result.addContainmentMethod(containmentMethodToAdd); - } - } - } - } - return result; - } - - syn java.util.List<String> DumpAst.targetOrder(Class<?> clazz) { - for (java.lang.reflect.Constructor<?> method : clazz.getConstructors()) { - for (java.lang.annotation.Annotation annotation : method.getAnnotations()) { - String canonicalName = annotation.annotationType().getCanonicalName(); - if (canonicalName.startsWith(astNodeAnnotationPrefix())) { - String simpleName = annotation.annotationType().getSimpleName(); - if (simpleName.equals("Constructor")) { - return java.util.Arrays.asList((String[]) invokeMethod("name", annotation)); - } - } - } - } - // there is no constructor with an annotation, iff the nonterminal has no children - return null; - } - - private String DumpAst.titleCase(String s) { - if (s.isEmpty()) { - return s; - } - return Character.toUpperCase(s.charAt(0)) + s.substring(1); - } - // TODO: add new attributes for: {token,child,relation,attribute,nta}Enabled(String parentType, String name). 1) just move implementation into this attribute. 2) add include/exclude on type-level to it. // --- isTypeEnabled --- @@ -486,24 +323,6 @@ aspect GenerationBackend { return !matches(getBuildConfig().typeIgnorePattern(), typeName); } - // --- match{In,Ex}cludePatternCollection --- - syn PatternCollection BuildConfig.matchIncludePatternCollection(String typeName) { - for (TypePatternCollectionMapping mapping : getIncludeTypePatternList()) { - if (matches(mapping.typePattern(), typeName)) { - return mapping.getPatternCollection(); - } - } - return null; - } - syn PatternCollection BuildConfig.matchExcludePatternCollection(String typeName) { - for (TypePatternCollectionMapping mapping : getExcludeTypePatternList()) { - if (matches(mapping.typePattern(), typeName)) { - return mapping.getPatternCollection(); - } - } - return null; - } - // --- {typeIgnore,child,token,relation,attribute,nta}Pattern --- syn java.util.regex.Pattern BuildConfig.typeIgnorePattern() = java.util.regex.Pattern.compile(getTypeIgnorePattern()); syn java.util.regex.Pattern PatternCollection.childPattern() = java.util.regex.Pattern.compile(getChildPattern()); @@ -518,175 +337,9 @@ aspect GenerationBackend { return p.matcher(input).matches(); } - // --- debug --- (mustache has printConfig as context) - syn boolean PrintConfig.debug() = buildConfig().getDebug(); - - private static String DumpAst.invokeName(java.lang.annotation.Annotation annotation) { - return (String) invokeMethod("name", annotation); - } - private static Object DumpAst.invokeMethod(String name, java.lang.annotation.Annotation annotation) { - try { - return annotation.annotationType().getMethod(name).invoke(annotation); - } catch (java.lang.reflect.InvocationTargetException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - // --- isNull --- - syn boolean DumpNode.isNull() { - return getObject() == null; - } - - // --- isAstNode --- - syn boolean DumpNode.isAstNode() { - if (getObject() == null) { - return false; - } - Class<?> clazz = getObject().getClass(); - for (java.lang.reflect.Method method : clazz.getMethods()) { - if ("init$Children".equals(method.getName()) && method.getParameterCount() == 0) { - return true; - } - } - return false; - } - - // --- astNodeAnnotationPrefix --- - syn String DumpAst.astNodeAnnotationPrefix() = getPackageName() + ".ASTNodeAnnotation"; - inh String DumpNode.astNodeAnnotationPrefix(); - eq DumpAst.getDumpNode().astNodeAnnotationPrefix() = astNodeAnnotationPrefix(); - - // --- NTA: InvisiblePath --- - syn InvisiblePath DumpNode.getInvisiblePath() { - InvisiblePath result = new InvisiblePath(); - for (DumpNode successor : reachableThroughInvisible()) { - result.addInnerRelationDumpNode(new InnerRelationDumpNode(successor)); - } - return result; - } - - // --- reachableThroughInvisible --- - syn java.util.List<DumpNode> DumpNode.reachableThroughInvisible() { - java.util.List<DumpNode> result = new java.util.ArrayList<>(); - for (DumpChildNode childNode : getDumpChildNodeList()) { - for (DumpNode inner : childNode.innerNodes(false)) { - if (inner != null && inner.getInvisible()) { - result.addAll(inner.reachableThroughInvisible()); - } else if (this.getInvisible()) { - result.add(inner); - } - } - } - return result; - } - - // --- labelAndTextColor --- - syn String DumpNode.labelAndTextColor() { - if (getTextColor().isEmpty()) { - return getLabel(); - } else { - return "<color:" + getTextColor() + ">" + getLabel() + "</color>"; - } - } - - // --- stereotypeList --- - syn String DumpNode.stereotypeList() { - StringBuilder sb = new StringBuilder(getManualStereotypes()); - if (!automaticStereotypes().isEmpty()) { - // add automatic stereotypes, if there are any - if (!getManualStereotypes().isEmpty()) { - // add a comma between manual and automatic, if both are present - sb.append(","); - } - sb.append(automaticStereotypes()); - } - String manualAndAutomaticStereotypes = sb.toString(); - if (manualAndAutomaticStereotypes.isEmpty()) { - return ""; - } - sb = new StringBuilder(); - for (String stereotype : manualAndAutomaticStereotypes.split(",")) { - sb.append(" <<").append(stereotype).append(">>"); - } - return sb.toString(); - } - // --- manualAndAutomaticStereotypes --- - syn String DumpNode.automaticStereotypes() = getComputed() ? "NTA" : ""; - - // --- myChildren --- - syn java.util.List<DumpNode> DumpNode.myChildren() { - java.util.List<DumpNode> result = new java.util.ArrayList<>(); - for (DumpChildNode childNode : getDumpChildNodeList()) { - for (DumpNode inner : childNode.innerNodes(true)) { - result.add(inner); - } - } - return result; - } - - // --- successor --- - syn DumpNode DumpNode.successor() { - if (container() == null) { - // not contained - return null; - } - java.util.List<DumpNode> siblingsAndMe = container().myChildren(); - int indexOfMe = siblingsAndMe.indexOf(this); - if (indexOfMe == siblingsAndMe.size() - 1) { - // last child - return null; - } - return siblingsAndMe.get(indexOfMe + 1); - } - - // --- hasSuccessor --- - syn boolean DumpNode.hasSuccessor() = successor() != null; - class TransformationTransferInformation { java.util.Map<Object, DumpNode> transformed = new java.util.HashMap<>(); java.util.Map<DumpNode, Boolean> relationTargetsUnprocessed = new java.util.HashMap<>(); int nodeCounter = 0; } - - - static StyleInformation StyleInformation.createDefault() { - StyleInformation result = new StyleInformation(); - result.setNameMethod(n -> n == null ? "null" : n.getClass().getSimpleName() + "@" + Integer.toHexString(n.hashCode())); - result.setBackgroundColorMethod(n -> ""); - result.setTextColorMethod(n -> ""); - result.setStereotypeMethod(n -> ""); - result.setComputedColor("blue"); - return result; - } - - syn String DumpAst.computedColor() = getBuildConfig().getStyleInformation().getComputedColor(); - - @FunctionalInterface - public interface StyleMethod<ASTNODE> { - String get(ASTNODE node); - } - - @FunctionalInterface - public interface IncludeRelationMethod<ASTNODE> { - boolean shouldInclude(ASTNODE sourceNode, ASTNODE targetNode, String roleName); - } - - @FunctionalInterface - public interface IncludeChildMethod<ASTNODE> { - boolean shouldInclude(ASTNODE parentNode, ASTNODE childNode, String contextName); - } - - @FunctionalInterface - public interface IncludeAttributeMethod<ASTNODE> { - boolean shouldInclude(ASTNODE node, String attributeName, boolean isNTA, java.util.function.Supplier<Object> value); - } - - @FunctionalInterface - public interface IncludeTokenMethod<ASTNODE> { - boolean shouldInclude(ASTNODE node, String tokenName, Object value); - } } diff --git a/dumpAst/src/main/jastadd/GenerationCommon.jrag b/dumpAst/src/main/jastadd/Util.jrag similarity index 78% rename from dumpAst/src/main/jastadd/GenerationCommon.jrag rename to dumpAst/src/main/jastadd/Util.jrag index a05412d..32c0ab0 100644 --- a/dumpAst/src/main/jastadd/GenerationCommon.jrag +++ b/dumpAst/src/main/jastadd/Util.jrag @@ -1,4 +1,4 @@ -aspect GenerationCommon { +aspect Util { // --- find{In,Ex}cludePatternCollection --- syn PatternCollection BuildConfig.findIncludePatternCollection(String typeRegex) { for (TypePatternCollectionMapping mapping : getIncludeTypePatternList()) { @@ -8,6 +8,7 @@ aspect GenerationCommon { } return null; } + syn PatternCollection BuildConfig.findExcludePatternCollection(String typeRegex) { for (TypePatternCollectionMapping mapping : getExcludeTypePatternList()) { if (mapping.getTypeRegex().equals(typeRegex)) { @@ -16,4 +17,11 @@ aspect GenerationCommon { } return null; } + + private String DumpAst.titleCase(String s) { + if (s.isEmpty()) { + return s; + } + return Character.toUpperCase(s.charAt(0)) + s.substring(1); + } } -- GitLab