diff --git a/dumpAst.base/src/main/jastadd/Transform.jadd b/dumpAst.base/src/main/jastadd/Transform.jadd index d1a71ae0cddd2b5ca54b66d2e5afc47d6e5c7023..bafbe3b782ba2c015d8f46138e3e67fb0da4d5d4 100644 --- a/dumpAst.base/src/main/jastadd/Transform.jadd +++ b/dumpAst.base/src/main/jastadd/Transform.jadd @@ -126,188 +126,236 @@ aspect Transform { } for (AnalysedMethod containmentMethod : car.getContainmentMethodList()) { if (containmentMethod.isSingleChildMethod() || containmentMethod.isOptChildMethod()) { - // -- singleChild or optChild -- - Object target; - if (containmentMethod.isOptChildMethod() && !((boolean) containmentMethod.asOptChildMethod().getCheckMethod().invoke(obj))) { - if (getBuildConfig().getExcludeNullNodes()) { - continue; - //target = containmentMethod.getMethod().invoke(obj); - } else { - target = null; - } - } else { - target = containmentMethod.getMethod().invoke(obj); - } - String childName = containmentMethod.getName(); - if (!getBuildConfig().getIncludeChildMethod().shouldInclude(obj, target, childName)) { - continue; - } - DumpNode targetNode = transform(tti, target, options.asNormal(false).allowNullObjectsOnce()); - if (targetNode != null) { - DumpNormalChildNode normalChild = new DumpNormalChildNode(); - normalChild.setName(childName + (containmentMethod.isOptChildMethod() ? "?" : "")); - normalChild.setDumpNode(targetNode); - normalChild.setComputed(false); - node.addDumpChildNode(normalChild); - } + handleContainmentSingleOrOptChild(node, containmentMethod, obj, tti, options); } else if (containmentMethod.isListChildMethod()) { - // -- listChild -- - Iterable<?> targetList = (Iterable<?>) containmentMethod.getMethod().invoke(obj); - DumpListChildNode listChild = new DumpListChildNode(); - listChild.setComputed(false); - String childName = containmentMethod.getName(); - listChild.setName(childName); - int index = -1; - for (Object target : targetList) { - index++; - if (!getBuildConfig().getIncludeChildMethod().shouldInclude(obj, target, childName)) { - continue; - } - DumpNode targetNode = transform(tti, target, options.asNormal(false)); - if (target != null && targetNode != null) { - listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(targetNode).setOriginalIndex(index)); - } - } - if (listChild.getNumInnerDumpNode() == 0) { - listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(transform(tti, EMPTY, - options.asNormal(false).allowNullObjectsOnce()))); - } - node.addDumpChildNode(listChild); + handleContainmentListChild(node, containmentMethod, obj, tti, options); } else { throw new RuntimeException("Unknown containment method type " + containmentMethod); } } for (AnalysedMethod otherMethod : car.getOtherMethodList()) { if (otherMethod.isSingleChildMethod()) { - // -- singleChild -- - String childName = otherMethod.getName(); - boolean computed = otherMethod.asSingleChildMethod().isNTASingleChildMethod(); - boolean shouldInclude = computed ? - getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, childName, true, () -> catchedInvoke(otherMethod.getMethod(), obj)) : - getBuildConfig().getIncludeChildMethod().shouldInclude(obj, otherMethod.getMethod().invoke(obj), childName); - if (!shouldInclude) { - continue; - } - DumpNode targetNode = transform(tti, otherMethod.getMethod().invoke(obj), options.asNormal(false).computed(computed).allowNullObjectsOnce()); - if (targetNode != null) { - DumpNormalChildNode normalChild = new DumpNormalChildNode(); - normalChild.setName(childName); - normalChild.setDumpNode(targetNode); - normalChild.setComputed(computed); - node.addDumpChildNode(normalChild); - } + handleOtherSingleChild(node, otherMethod, obj, tti, options); } else if (otherMethod.isListChildMethod()) { - // -- listChild -- - // it is always a NTAListChildMethod - String childName = otherMethod.getName(); - if (!getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, childName, true, () -> catchedInvoke(otherMethod.getMethod(), obj))) { - continue; - } - Iterable<?> targetList = (Iterable<?>) otherMethod.getMethod().invoke(obj); - DumpListChildNode listChild = new DumpListChildNode(); - boolean computed = otherMethod.asListChildMethod().isNTAListChildMethod(); - listChild.setComputed(computed); - listChild.setName(childName); - for (Object target : targetList) { - DumpNode targetNode = transform(tti, target, options.asNormal(false).computed(computed)); - if (target != null && targetNode != null) { - listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(targetNode)); - } - } - if (listChild.getNumInnerDumpNode() > 0) { - node.addDumpChildNode(listChild); - } + handleOtherListChild(node, otherMethod, obj, tti, options); } else if (otherMethod.isSingleRelationMethod()) { - // -- singleRelation -- - Object target = otherMethod.getMethod().invoke(obj); - if (!getBuildConfig().getIncludeRelationMethod().shouldInclude(obj, target, otherMethod.getName())) { - continue; - } - DumpNode targetNode = transform(tti, target, options.asRelation()); - if (targetNode != null) { - DumpNormalRelation normalRelation = new DumpNormalRelation(); - normalRelation.setName(otherMethod.getName() + - (otherMethod.asSingleRelationMethod().isOptRelationMethod() ? "?" : "")); - normalRelation.setDumpNode(targetNode); - node.addDumpRelation(normalRelation); - } + handleOtherSingleRelation(node, otherMethod, obj, tti, options); } else if (otherMethod.isListRelationMethod()) { - // -- listRelation -- - Iterable<?> targetList = (Iterable<?>) otherMethod.getMethod().invoke(obj); - DumpListRelation listRelation = new DumpListRelation(); - listRelation.setName(otherMethod.getName()); - int index = -1; - for (Object target : targetList) { - index++; - if (!getBuildConfig().getIncludeRelationMethod().shouldInclude(obj, target, otherMethod.getName())) { - continue; - } - DumpNode targetNode = transform(tti, target, options.asRelation()); - if (target != null && targetNode != null) { - listRelation.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(targetNode) - .setOriginalIndex(index)); + handleOtherListRelation(node, otherMethod, obj, tti, options); + } else if (otherMethod.isTokenMethod()) { + handleOtherToken(node, otherMethod, obj, tti, options); + } else { + throw new RuntimeException("Unknown other method type " + otherMethod); + } + } + return node; + } + + private boolean DumpAst.handleContainmentSingleOrOptChild(DumpNode node, AnalysedMethod containmentMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- singleChild or optChild -- + Object target; + if (containmentMethod.isOptChildMethod() && !((boolean) containmentMethod.asOptChildMethod().getCheckMethod().invoke(obj))) { + if (getBuildConfig().getExcludeNullNodes()) { + return false; + } else { + target = null; + } + } else { + target = containmentMethod.getMethod().invoke(obj); + } + String childName = containmentMethod.getName(); + if (!getBuildConfig().getIncludeChildMethod().shouldInclude(obj, target, childName)) { + return false; + } + DumpNode targetNode = transform(tti, target, options.asNormal(false).allowNullObjectsOnce()); + if (targetNode != null) { + DumpNormalChildNode normalChild = new DumpNormalChildNode(); + normalChild.setName(childName + (containmentMethod.isOptChildMethod() ? "?" : "")); + normalChild.setDumpNode(targetNode); + normalChild.setComputed(false); + node.addDumpChildNode(normalChild); + } + return true; + } + + private boolean DumpAst.handleContainmentListChild(DumpNode node, AnalysedMethod containmentMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- listChild -- + Iterable<?> targetList = (Iterable<?>) containmentMethod.getMethod().invoke(obj); + DumpListChildNode listChild = new DumpListChildNode(); + listChild.setComputed(false); + String childName = containmentMethod.getName(); + listChild.setName(childName); + int index = -1; + for (Object target : targetList) { + index++; + if (!getBuildConfig().getIncludeChildMethod().shouldInclude(obj, target, childName)) { + continue; + } + DumpNode targetNode = transform(tti, target, options.asNormal(false)); + if (target != null && targetNode != null) { + listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(targetNode).setOriginalIndex(index)); + } + } + if (listChild.getNumInnerDumpNode() == 0) { + listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(transform(tti, EMPTY, + options.asNormal(false).allowNullObjectsOnce()))); + } + node.addDumpChildNode(listChild); + return true; + } + + private boolean DumpAst.handleOtherSingleChild(DumpNode node, AnalysedMethod otherMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- singleChild -- + String childName = otherMethod.getName(); + boolean computed = otherMethod.asSingleChildMethod().isNTASingleChildMethod(); + boolean shouldInclude = computed ? + getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, childName, true, () -> catchedInvoke(otherMethod.getMethod(), obj)) : + getBuildConfig().getIncludeChildMethod().shouldInclude(obj, otherMethod.getMethod().invoke(obj), childName); + if (!shouldInclude) { + return false; + } + DumpNode targetNode = transform(tti, otherMethod.getMethod().invoke(obj), options.asNormal(false).computed(computed).allowNullObjectsOnce()); + if (targetNode != null) { + DumpNormalChildNode normalChild = new DumpNormalChildNode(); + normalChild.setName(childName); + normalChild.setDumpNode(targetNode); + normalChild.setComputed(computed); + node.addDumpChildNode(normalChild); + } + return true; + } + + private boolean DumpAst.handleOtherListChild(DumpNode node, AnalysedMethod otherMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- listChild -- + // it is always a NTAListChildMethod + String childName = otherMethod.getName(); + if (!getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, childName, true, () -> catchedInvoke(otherMethod.getMethod(), obj))) { + return false; + } + Iterable<?> targetList = (Iterable<?>) otherMethod.getMethod().invoke(obj); + DumpListChildNode listChild = new DumpListChildNode(); + boolean computed = otherMethod.asListChildMethod().isNTAListChildMethod(); + listChild.setComputed(computed); + listChild.setName(childName); + for (Object target : targetList) { + DumpNode targetNode = transform(tti, target, options.asNormal(false).computed(computed)); + if (target != null && targetNode != null) { + listChild.addInnerDumpNode(new InnerDumpNode().setDumpNode(targetNode)); + } + } + if (listChild.getNumInnerDumpNode() > 0) { + node.addDumpChildNode(listChild); + } + return true; + } + + private boolean DumpAst.handleOtherSingleRelation(DumpNode node, AnalysedMethod otherMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- singleRelation -- + Object target = otherMethod.getMethod().invoke(obj); + if (!getBuildConfig().getIncludeRelationMethod().shouldInclude(obj, target, otherMethod.getName())) { + return false; + } + DumpNode targetNode = transform(tti, target, options.asRelation()); + if (targetNode != null) { + DumpNormalRelation normalRelation = new DumpNormalRelation(); + normalRelation.setName(otherMethod.getName() + + (otherMethod.asSingleRelationMethod().isOptRelationMethod() ? "?" : "")); + normalRelation.setDumpNode(targetNode); + node.addDumpRelation(normalRelation); + } + return true; + } + + private boolean DumpAst.handleOtherListRelation(DumpNode node, AnalysedMethod otherMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- listRelation -- + Iterable<?> targetList = (Iterable<?>) otherMethod.getMethod().invoke(obj); + DumpListRelation listRelation = new DumpListRelation(); + listRelation.setName(otherMethod.getName()); + int index = -1; + for (Object target : targetList) { + index++; + if (!getBuildConfig().getIncludeRelationMethod().shouldInclude(obj, target, otherMethod.getName())) { + continue; + } + DumpNode targetNode = transform(tti, target, options.asRelation()); + if (target != null && targetNode != null) { + listRelation.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(targetNode) + .setOriginalIndex(index)); + } + } + if (listRelation.getNumInnerRelationDumpNode() == 0) { + listRelation.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(transform(tti, EMPTY, + options.asNormal(false).allowNullObjectsOnce()))); + } + node.addDumpRelation(listRelation); + return true; + } + + private boolean DumpAst.handleOtherToken(DumpNode node, AnalysedMethod otherMethod, + Object obj, TransformationTransferInformation tti, TransformationOptions options) + throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException { + // -- token -- + TokenMethod tokenMethod = otherMethod.asTokenMethod(); + boolean shouldInclude = tokenMethod.isAttributeMethod() ? + getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, otherMethod.getName(), false, () -> catchedInvoke(otherMethod.getMethod(), obj)) : + getBuildConfig().getIncludeTokenMethod().shouldInclude(obj, otherMethod.getName(), otherMethod.getMethod().invoke(obj)); + if (!shouldInclude) { + return false; + } + Object target = otherMethod.getMethod().invoke(obj); + if (target != null) { + DumpToken token = null; + boolean atLeastOneASTNode = false; + if (Iterable.class.isAssignableFrom(target.getClass())) { + java.util.List<DumpNode> nodes = new java.util.ArrayList<>(); + Iterable iterable = (Iterable) target; + for (Object element : iterable) { + // TODO check if isAstNode for first non-null. if yes, use DumpReferenceListToken, other DumpValueToken + DumpNode nodeForElement = transform(tti, element, options.asRelation()); + nodes.add(nodeForElement); + if (nodeForElement != null && nodeForElement.isAstNode()) { + atLeastOneASTNode = true; } } - if (listRelation.getNumInnerRelationDumpNode() == 0) { - listRelation.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(transform(tti, EMPTY, - options.asNormal(false).allowNullObjectsOnce()))); - } - node.addDumpRelation(listRelation); - } else if (otherMethod.isTokenMethod()) { - // -- token -- - TokenMethod tokenMethod = otherMethod.asTokenMethod(); - boolean shouldInclude = tokenMethod.isAttributeMethod() ? - getBuildConfig().getIncludeAttributeMethod().shouldInclude(obj, otherMethod.getName(), false, () -> catchedInvoke(otherMethod.getMethod(), obj)) : - getBuildConfig().getIncludeTokenMethod().shouldInclude(obj, otherMethod.getName(), otherMethod.getMethod().invoke(obj)); - if (!shouldInclude) { - continue; + if (atLeastOneASTNode) { + DumpReferenceListToken listToken = new DumpReferenceListToken(); + nodes.forEach(element -> { + listToken.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(element)); + }); + token = listToken; } - Object target = otherMethod.getMethod().invoke(obj); - if (target != null) { - DumpToken token = null; - boolean atLeastOneASTNode = false; - if (Iterable.class.isAssignableFrom(target.getClass())) { - java.util.List<DumpNode> nodes = new java.util.ArrayList<>(); - Iterable iterable = (Iterable) target; - for (Object element : iterable) { - // TODO check if isAstNode for first non-null. if yes, use DumpReferenceListToken, other DumpValueToken - DumpNode nodeForElement = transform(tti, element, options.asRelation()); - nodes.add(nodeForElement); - if (nodeForElement != null && nodeForElement.isAstNode()) { - atLeastOneASTNode = true; - } - } - if (atLeastOneASTNode) { - DumpReferenceListToken listToken = new DumpReferenceListToken(); - nodes.forEach(element -> { - listToken.addInnerRelationDumpNode(new InnerRelationDumpNode().setDumpNode(element)); - }); - token = listToken; - } - } - if (!atLeastOneASTNode) { - DumpNode targetNode = transform(tti, target, options.asRelation()); - if (targetNode != null && targetNode.isAstNode()) { - token = new DumpReferenceToken().setValue(targetNode); - } else { - if (getBuildConfig().getIncludeEmptyString() || !target.toString().isEmpty()) { - DumpValueToken valueToken = new DumpValueToken(); - valueToken.setValue(target); - token = valueToken; - } - } - } - if (token != null) { - token.setName(otherMethod.getName()); - token.setComputed(otherMethod.asTokenMethod().isAttributeMethod()); - node.addDumpToken(token); + } + if (!atLeastOneASTNode) { + DumpNode targetNode = transform(tti, target, options.asRelation()); + if (targetNode != null && targetNode.isAstNode()) { + token = new DumpReferenceToken().setValue(targetNode); + } else { + if (getBuildConfig().getIncludeEmptyString() || !target.toString().isEmpty()) { + DumpValueToken valueToken = new DumpValueToken(); + valueToken.setValue(target); + token = valueToken; } } - } else { - throw new RuntimeException("Unknown other method type " + otherMethod); + } + if (token != null) { + token.setName(otherMethod.getName()); + token.setComputed(otherMethod.asTokenMethod().isAttributeMethod()); + node.addDumpToken(token); } } - return node; + return true; } private Object DumpAst.catchedInvoke(java.lang.reflect.Method method, Object obj) {