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 2022/10/19 22:57:18 UTC
[ws-axiom] branch master updated: Inline initializer 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 34ef9ecf4 Inline initializer methods
34ef9ecf4 is described below
commit 34ef9ecf4a36b57de70364e9f08e607961ad6ea3
Author: Andreas Veithen <an...@gmail.com>
AuthorDate: Wed Oct 19 23:57:11 2022 +0100
Inline initializer methods
---
.../weaver/ImplementationClassDefinition.java | 51 ++++-----------
.../org/apache/axiom/weaver/MethodInliner.java | 72 ++++++++++++++++++++++
.../main/java/org/apache/axiom/weaver/Named.java | 37 -----------
.../apache/axiom/weaver/UniqueNameGenerator.java | 31 ----------
4 files changed, 85 insertions(+), 106 deletions(-)
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationClassDefinition.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationClassDefinition.java
index 663947b63..f95dd82a8 100644
--- a/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationClassDefinition.java
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/ImplementationClassDefinition.java
@@ -47,8 +47,8 @@ final class ImplementationClassDefinition extends ClassDefinition {
private final boolean singleton;
private final Mixin[] mixins;
private final MixinMethod[] methods;
- private final List<Named<InitializerMethod>> initializerMethods = new ArrayList<>();
- private final List<Named<StaticInitializerMethod>> staticInitializerMethods = new ArrayList<>();
+ private final List<InitializerMethod> initializerMethods = new ArrayList<>();
+ private final List<StaticInitializerMethod> staticInitializerMethods = new ArrayList<>();
ImplementationClassDefinition(
TargetContext targetContext,
@@ -67,7 +67,6 @@ final class ImplementationClassDefinition extends ClassDefinition {
this.singleton = singleton;
this.mixins = mixins;
Map<String, MixinMethod> methodMap = new LinkedHashMap<>();
- UniqueNameGenerator methodNameGenerator = new UniqueNameGenerator();
for (Mixin mixin : mixins) {
for (MixinMethod method : mixin.getMethods()) {
String signature = method.getSignature();
@@ -110,18 +109,11 @@ final class ImplementationClassDefinition extends ClassDefinition {
}
InitializerMethod initializerMethod = mixin.getInitializerMethod();
if (initializerMethod != null) {
- initializerMethods.add(
- new Named<>(
- initializerMethod,
- methodNameGenerator.generateUniqueName("init$" + mixin.getName())));
+ initializerMethods.add(initializerMethod);
}
StaticInitializerMethod staticInitializerMethod = mixin.getStaticInitializerMethod();
if (staticInitializerMethod != null) {
- staticInitializerMethods.add(
- new Named<>(
- staticInitializerMethod,
- methodNameGenerator.generateUniqueName(
- "clinit$" + mixin.getName())));
+ staticInitializerMethods.add(staticInitializerMethod);
}
}
methods = methodMap.values().toArray(new MixinMethod[methodMap.size()]);
@@ -138,12 +130,13 @@ final class ImplementationClassDefinition extends ClassDefinition {
mv.visitCode();
mv.visitIntInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "()V", false);
- for (Named<InitializerMethod> method : initializerMethods) {
- mv.visitIntInsn(Opcodes.ALOAD, 0);
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, method.getName(), "()V", false);
+ MethodInliner inliner =
+ new MethodInliner(mv, 1, 1, new Object[] {targetContext.getTargetClassName()});
+ for (InitializerMethod method : initializerMethods) {
+ method.getBody().apply(targetContext, inliner);
}
mv.visitInsn(Opcodes.RETURN);
- mv.visitMaxs(1, 1);
+ inliner.emitMaxs();
mv.visitEnd();
}
@@ -155,8 +148,9 @@ final class ImplementationClassDefinition extends ClassDefinition {
cv.visitMethod(
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
- for (Named<StaticInitializerMethod> method : staticInitializerMethods) {
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, method.getName(), "()V", false);
+ MethodInliner inliner = new MethodInliner(mv, singleton ? 2 : 0, 0, new Object[0]);
+ for (StaticInitializerMethod method : staticInitializerMethods) {
+ method.getBody().apply(targetContext, inliner);
}
if (singleton) {
mv.visitTypeInsn(Opcodes.NEW, targetContext.getTargetClassName());
@@ -174,7 +168,7 @@ final class ImplementationClassDefinition extends ClassDefinition {
"L" + targetContext.getTargetClassName() + ";");
}
mv.visitInsn(Opcodes.RETURN);
- mv.visitMaxs(singleton ? 2 : 0, 0);
+ inliner.emitMaxs();
mv.visitEnd();
}
@@ -194,25 +188,6 @@ final class ImplementationClassDefinition extends ClassDefinition {
for (Mixin mixin : mixins) {
mixin.apply(className, cv);
}
- for (Named<InitializerMethod> method : initializerMethods) {
- MethodVisitor mv =
- cv.visitMethod(Opcodes.ACC_PRIVATE, method.getName(), "()V", null, null);
- if (mv != null) {
- method.get().getBody().apply(targetContext, mv);
- }
- }
- for (Named<StaticInitializerMethod> method : staticInitializerMethods) {
- MethodVisitor mv =
- cv.visitMethod(
- Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
- method.getName(),
- "()V",
- null,
- null);
- if (mv != null) {
- method.get().getBody().apply(targetContext, mv);
- }
- }
for (MixinMethod method : methods) {
method.apply(targetContext, cv);
}
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/MethodInliner.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/MethodInliner.java
new file mode 100644
index 000000000..a178c9829
--- /dev/null
+++ b/axiom-weaver/src/main/java/org/apache/axiom/weaver/MethodInliner.java
@@ -0,0 +1,72 @@
+/*
+ * 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;
+
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+final class MethodInliner extends MethodVisitor {
+ private Label endLabel;
+ private int maxStack;
+ private int maxLocals;
+ private Object[] locals;
+
+ MethodInliner(MethodVisitor methodVisitor, int maxStack, int maxLocals, Object[] locals) {
+ super(Opcodes.ASM9, methodVisitor);
+ this.maxStack = maxStack;
+ this.maxLocals = maxLocals;
+ this.locals = locals;
+ }
+
+ @Override
+ public void visitCode() {
+ endLabel = new Label();
+ }
+
+ @Override
+ public void visitInsn(int opcode) {
+ if (opcode == Opcodes.RETURN) {
+ super.visitJumpInsn(Opcodes.GOTO, endLabel);
+ } else {
+ super.visitInsn(opcode);
+ }
+ }
+
+ @Override
+ public void visitMaxs(int maxStack, int maxLocals) {
+ if (maxStack > this.maxStack) {
+ this.maxStack = maxStack;
+ }
+ if (maxLocals > this.maxLocals) {
+ this.maxLocals = maxLocals;
+ }
+ }
+
+ @Override
+ public void visitEnd() {
+ super.visitLabel(endLabel);
+ super.visitFrame(Opcodes.F_NEW, locals.length, locals, 0, null);
+ endLabel = null;
+ }
+
+ public void emitMaxs() {
+ super.visitMaxs(maxStack, maxLocals);
+ }
+}
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/Named.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/Named.java
deleted file mode 100644
index 73dd58665..000000000
--- a/axiom-weaver/src/main/java/org/apache/axiom/weaver/Named.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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;
-
-final class Named<T> {
- private final T object;
- private final String name;
-
- Named(T object, String name) {
- this.object = object;
- this.name = name;
- }
-
- T get() {
- return object;
- }
-
- String getName() {
- return name;
- }
-}
diff --git a/axiom-weaver/src/main/java/org/apache/axiom/weaver/UniqueNameGenerator.java b/axiom-weaver/src/main/java/org/apache/axiom/weaver/UniqueNameGenerator.java
deleted file mode 100644
index 4a74c0061..000000000
--- a/axiom-weaver/src/main/java/org/apache/axiom/weaver/UniqueNameGenerator.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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;
-
-import java.util.HashMap;
-import java.util.Map;
-
-final class UniqueNameGenerator {
- private Map<String, Integer> counts = new HashMap<>();
-
- String generateUniqueName(String baseName) {
- int count = counts.compute(baseName, (k, v) -> v == null ? 1 : v + 1);
- return count == 1 ? baseName : baseName + "$" + count;
- }
-}