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/27 13:32:51 UTC
[ws-axiom] branch master updated: Add support for generating factory methods
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 bdbcc31 Add support for generating factory methods
bdbcc31 is described below
commit bdbcc31d68ddfabd6ee05966717e45896665b307
Author: Andreas Veithen <an...@gmail.com>
AuthorDate: Sat Nov 27 13:25:13 2021 +0000
Add support for generating factory methods
---
.../axiom/weaver/annotation/FactoryMethod.java | 35 +++++++++++
axiom-weaver/pom.xml | 1 -
.../main/java/org/apache/axiom/weaver/Weaver.java | 2 +
.../weaver/mixin/factory/FactoryMethodBody.java | 44 ++++++++++++++
.../weaver/mixin/factory/FactoryMixinFactory.java | 67 ++++++++++++++++++++++
.../org/apache/axiom/weaver/tree/ChildNode.java | 21 +++++++
.../org/apache/axiom/weaver/tree/Directory.java | 21 +++++++
.../java/org/apache/axiom/weaver/tree/Factory.java | 32 +++++++++++
.../java/org/apache/axiom/weaver/tree/Leaf.java | 21 +++++++
.../org/apache/axiom/weaver/tree/ParentNode.java | 21 +++++++
.../java/org/apache/axiom/weaver/tree/Root.java | 21 +++++++
.../org/apache/axiom/weaver/tree/TreeTest.java | 42 ++++++++++++++
12 files changed, 327 insertions(+), 1 deletion(-)
diff --git a/axiom-weaver-annotations/src/main/java/org/apache/axiom/weaver/annotation/FactoryMethod.java b/axiom-weaver-annotations/src/main/java/org/apache/axiom/weaver/annotation/FactoryMethod.java
new file mode 100644
index 0000000..3618583
--- /dev/null
+++ b/axiom-weaver-annotations/src/main/java/org/apache/axiom/weaver/annotation/FactoryMethod.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.annotation;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that the method is a factory method. The return type of the method must be an
+ * interface and the method must not have any arguments. The weaver will generate code that
+ * creates a new instance of the class implementing the interface.
+ */
+@Retention(RUNTIME)
+@Target(METHOD)
+public @interface FactoryMethod {
+}
diff --git a/axiom-weaver/pom.xml b/axiom-weaver/pom.xml
index 0fe69af..0fc5af5 100644
--- a/axiom-weaver/pom.xml
+++ b/axiom-weaver/pom.xml
@@ -61,7 +61,6 @@
<groupId>${project.groupId}</groupId>
<artifactId>axiom-weaver-annotations</artifactId>
<version>${project.version}</version>
- <scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
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 f9e293b..b7e8daf 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
@@ -29,6 +29,7 @@ import org.apache.axiom.weaver.mixin.ClassDefinition;
import org.apache.axiom.weaver.mixin.Mixin;
import org.apache.axiom.weaver.mixin.WeavingContext;
import org.apache.axiom.weaver.mixin.clazz.MixinFactory;
+import org.apache.axiom.weaver.mixin.factory.FactoryMixinFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -66,6 +67,7 @@ public final class Weaver {
private InterfaceNode addInterface(Class<?> iface) {
InterfaceNode interfaceNode = interfaceNodes.get(iface);
if (interfaceNode == null) {
+ FactoryMixinFactory.createFactoryMixin(iface).ifPresent(this::addMixin);
Set<InterfaceNode> parentInterfaces = new HashSet<>();
Set<ImplementationNode> parentImplementations = new HashSet<>();
for (Class<?> superClass : iface.getInterfaces()) {
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMethodBody.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMethodBody.java
new file mode 100644
index 0000000..c8dc778
--- /dev/null
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMethodBody.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.mixin.factory;
+
+import org.apache.axiom.weaver.mixin.MethodBody;
+import org.apache.axiom.weaver.mixin.TargetContext;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+final class FactoryMethodBody extends MethodBody {
+ private final Class<?> iface;
+
+ FactoryMethodBody(Class<?> iface) {
+ this.iface = iface;
+ }
+
+ @Override
+ public void apply(TargetContext context, MethodVisitor mv) {
+ String implementationClassName =
+ context.getWeavingContext().getImplementationClassName(iface);
+ mv.visitTypeInsn(Opcodes.NEW, implementationClassName);
+ mv.visitInsn(Opcodes.DUP);
+ mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationClassName, "<init>", "()V", false);
+ mv.visitInsn(Opcodes.ARETURN);
+ mv.visitMaxs(2, 1);
+ mv.visitEnd();
+ }
+}
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMixinFactory.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMixinFactory.java
new file mode 100644
index 0000000..7407b06
--- /dev/null
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/mixin/factory/FactoryMixinFactory.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.mixin.factory;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.axiom.weaver.annotation.FactoryMethod;
+import org.apache.axiom.weaver.mixin.Mixin;
+import org.apache.axiom.weaver.mixin.MixinMethod;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
+
+public final class FactoryMixinFactory {
+ private FactoryMixinFactory() {}
+
+ public static Optional<Mixin> createFactoryMixin(Class<?> iface) {
+ List<MixinMethod> mixinMethods = new ArrayList<>();
+ for (Method method : iface.getDeclaredMethods()) {
+ if (method.getAnnotation(FactoryMethod.class) == null) {
+ continue;
+ }
+ // TODO: check that the method has the expected signature
+ Class<?> returnType = method.getReturnType();
+ mixinMethods.add(
+ new MixinMethod(
+ Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
+ method.getName(),
+ "()" + Type.getDescriptor(returnType),
+ null,
+ null,
+ new FactoryMethodBody(returnType)));
+ }
+ if (mixinMethods.isEmpty()) {
+ return Optional.empty();
+ }
+ return Optional.of(
+ new Mixin(
+ Opcodes.V1_8,
+ iface.getSimpleName() + "FactoryMixin",
+ iface,
+ Collections.emptyList(),
+ null,
+ null,
+ mixinMethods,
+ Collections.emptyList()));
+ }
+}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ChildNode.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ChildNode.java
new file mode 100644
index 0000000..38c0198
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ChildNode.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+public interface ChildNode {}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Directory.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Directory.java
new file mode 100644
index 0000000..0fe06a7
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Directory.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+public interface Directory extends ParentNode, ChildNode {}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Factory.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Factory.java
new file mode 100644
index 0000000..00827d9
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Factory.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+import org.apache.axiom.weaver.annotation.FactoryMethod;
+
+public interface Factory {
+ @FactoryMethod
+ Root createRoot();
+
+ @FactoryMethod
+ Directory createDirectory();
+
+ @FactoryMethod
+ Leaf createLeaf();
+}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Leaf.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Leaf.java
new file mode 100644
index 0000000..1797886
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Leaf.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+public interface Leaf extends ChildNode {}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ParentNode.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ParentNode.java
new file mode 100644
index 0000000..6879dc0
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/ParentNode.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+public interface ParentNode {}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Root.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Root.java
new file mode 100644
index 0000000..f56c41d
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/Root.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+public interface Root extends ParentNode {}
diff --git a/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/TreeTest.java b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/TreeTest.java
new file mode 100644
index 0000000..57e82f6
--- /dev/null
+++ b/axiom-weaver/src/test/java/org/apache/axiom/weaver/tree/TreeTest.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.axiom.weaver.tree;
+
+import org.apache.axiom.weaver.SimpleImplementationClassNameMapper;
+import org.apache.axiom.weaver.Weaver;
+import org.junit.Test;
+
+public class TreeTest {
+ @Test
+ public void testInnerclass() throws Exception {
+ ClassLoader cl = TreeTest.class.getClassLoader();
+ Weaver weaver = new Weaver(new SimpleImplementationClassNameMapper("impl"));
+ weaver.addInterfaceToImplement(Root.class);
+ weaver.addInterfaceToImplement(Directory.class);
+ weaver.addInterfaceToImplement(Leaf.class);
+ weaver.addInterfaceToImplement(Factory.class);
+ Factory factory =
+ weaver.toClassLoader(cl)
+ .loadClass("impl.FactoryImpl")
+ .asSubclass(Factory.class)
+ .getConstructor()
+ .newInstance();
+ factory.createRoot();
+ }
+}