You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2021/11/26 21:45:13 UTC

[ws-axiom] branch master updated: Simplify the way implementation class names are generated

This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new 6157ac7  Simplify the way implementation class names are generated
6157ac7 is described below

commit 6157ac74095af30a96343b5230de24bf192140d0
Author: Andreas Veithen <an...@gmail.com>
AuthorDate: Fri Nov 26 21:45:04 2021 +0000

    Simplify the way implementation class names are generated
---
 .../apache/axiom/weaver/ImplementationNode.java    | 23 ++++++++++++++--------
 .../main/java/org/apache/axiom/weaver/Weaver.java  | 13 +++++++-----
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationNode.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationNode.java
index 0c19131..7891e80 100644
--- a/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationNode.java
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationNode.java
@@ -23,6 +23,7 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.function.Supplier;
 
 import org.apache.axiom.weaver.mixin.ClassDefinition;
 import org.apache.axiom.weaver.mixin.Mixin;
@@ -56,14 +57,16 @@ final class ImplementationNode {
     private final References<ImplementationNode> ancestorsOrSelf = ANCESTOR_OR_SELF.newReferenceHolder(this);
     private final References<ImplementationNode> descendantsOrSelf = ANCESTOR_OR_SELF.getConverse().newReferenceHolder(this);
     private final References<MixinNode> transitiveMixins = TRANSITIVE_MIXIN.newReferenceHolder(this);
+    private final Supplier<String> className;
     private boolean requireImplementation;
 
-    ImplementationNode(int id, Set<ImplementationNode> parents, InterfaceNode iface, Set<MixinNode> mixins) {
+    ImplementationNode(int id, Set<ImplementationNode> parents, InterfaceNode iface, Set<MixinNode> mixins, Supplier<String> className) {
         this.id = id;
         this.primaryInterface = iface;
         ifaces.add(iface);
         this.mixins.addAll(mixins);
         this.parents.addAll(parents);
+        this.className = className;
     }
 
     InterfaceNode getPrimaryInterface() {
@@ -74,6 +77,10 @@ final class ImplementationNode {
         requireImplementation = true;
     }
 
+    String getClassName() {
+        return className.get();
+    }
+
     Set<ImplementationNode> getRequiredDescendantsOrSelf() {
         Set<ImplementationNode> result = new LinkedIdentityHashSet<>();
         for (ImplementationNode node : descendantsOrSelf.asSet()) {
@@ -115,14 +122,14 @@ final class ImplementationNode {
         return interfaces;
     }
 
-    void dump(StringBuilder builder, ImplementationClassNameMapper implementationClassNameMapper) {
+    void dump(StringBuilder builder, boolean showImplName) {
         builder.append("  n");
         builder.append(id);
         builder.append(" [label=<");
-        if (implementationClassNameMapper != null) {
-            String implementationClassName = implementationClassNameMapper.getImplementationClassName(primaryInterface.getInterface());
+        if (showImplName) {
+            String implementationClassName = getClassName();
             builder.append("<b>");
-            builder.append(implementationClassName.substring(implementationClassName.lastIndexOf('.')+1));
+            builder.append(implementationClassName.substring(implementationClassName.lastIndexOf('/')+1));
             builder.append("</b><br/>");
         }
         for (Class<?> iface : getInterfaces()) {
@@ -295,9 +302,9 @@ final class ImplementationNode {
         return builder.toString();
     }
 
-    List<ClassDefinition> toClassDefinitions(ImplementationClassNameMapper implementationClassNameMapper) {
+    List<ClassDefinition> toClassDefinitions() {
         List<ClassDefinition> classDefinitions = new ArrayList<>();
-        String className = implementationClassNameMapper.getImplementationClassName(primaryInterface.getInterface()).replace('.', '/');
+        String className = getClassName();
         int version = 0;
         List<Mixin> mixins = new ArrayList<>();
         for (MixinNode mixinNode : this.mixins) {
@@ -328,7 +335,7 @@ final class ImplementationNode {
                 version,
                 access,
                 className,
-                parents.isEmpty() ? null : implementationClassNameMapper.getImplementationClassName(parents.iterator().next().primaryInterface.getInterface()).replace('.', '/'),
+                parents.isEmpty() ? null : parents.iterator().next().getClassName(),
                 ifaceNames.toArray(new String[ifaceNames.size()]),
                 mixins.toArray(new Mixin[mixins.size()])));
         return classDefinitions;
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/Weaver.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/Weaver.java
index 476ad90..f07b7cd 100644
--- a/axiom-weaver/src/main/java/org/apache/axiom/weaver/Weaver.java
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/Weaver.java
@@ -83,7 +83,11 @@ public final class Weaver {
                     mixinNodes.add(new MixinNode(mixin, interfaceNode));
                 }
             }
-            ImplementationNode implementationNode = new ImplementationNode(nextId++, parentImplementations, interfaceNode, mixinNodes);
+            ImplementationNode implementationNode = new ImplementationNode(
+                    nextId++, parentImplementations, interfaceNode, mixinNodes,
+                    // The class name is evaluated lazily so that mapper only needs to produce
+                    // values for classes that are actually generated.
+                    () -> implementationClassNameMapper.getImplementationClassName(iface).replace('.', '/'));
             implementationNodes.put(iface, implementationNode);
             nodes.add(implementationNode);
         }
@@ -123,7 +127,7 @@ public final class Weaver {
             StringBuilder builder = new StringBuilder(prefix);
             builder.append("digraph G {\n  rankdir = BT;\n  node [shape=box];\n");
             for (ImplementationNode node : nodes) {
-                node.dump(builder, showImplName ? implementationClassNameMapper : null);
+                node.dump(builder, showImplName);
             }
             builder.append("}");
             log.debug(builder.toString());
@@ -168,15 +172,14 @@ public final class Weaver {
             if (implementationNodes.size() == 1) {
                 implementationClassNames.put(
                         interfaceNode.getInterface(),
-                        implementationClassNameMapper.getImplementationClassName(
-                                implementationNodes.iterator().next().getPrimaryInterface().getInterface()).replace('.', '/'));
+                        implementationNodes.iterator().next().getClassName());
             }
         });
         log.debug(implementationClassNames);
 
         List<ClassDefinition> result = new ArrayList<>();
         for (ImplementationNode node : nodes) {
-            result.addAll(node.toClassDefinitions(implementationClassNameMapper));
+            result.addAll(node.toClassDefinitions());
         }
         return result.toArray(new ClassDefinition[result.size()]);
     }