This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-jspf.git
commit 7834279ff5d73f3b8c481b5f751a72201f9aab62 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 17:29:36 2026 +0100 [PERF] WiringServiceTable: resolve the setter of an enabling interface once wire runs for every term of every parsed record and called getDeclaredMethods on each enabling interface the term implements, which copies the Method array and each Method every time: about 13% of the parse of a short record and 5% of a check. An interface never changes, so its single setter is kept in a ClassValue: no need to follow put and remove of the services, which are still read from the table on each call. --- .../james/jspf/wiring/WiringServiceTable.java | 22 ++++- .../james/jspf/wiring/WiringServiceTableTest.java | 108 +++++++++++++++++++++ 2 files changed, 127 insertions(+), 3 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceTable.java b/resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceTable.java index afbf081..d5be3ab 100644 --- a/resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceTable.java +++ b/resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceTable.java @@ -33,6 +33,22 @@ public class WiringServiceTable extends Hashtable<Class<?>,Object> implements Wi private static final long serialVersionUID = -9151935136150279119L; + /** + * The setter of each enabling interface, its only declared method, or null when it has + * none or several. An interface never changes: no need to follow put and remove. + * getDeclaredMethods copies the Method array and each Method on every call. + */ + private static final ClassValue<Method> SETTERS = new ClassValue<Method>() { + @Override + protected Method computeValue(Class<?> enablingClass) { + Method[] m = enablingClass.getDeclaredMethods(); + if (m != null && m.length == 1 && m[0] != null) { + return m[0]; + } + return null; + } + }; + /** * @see org.apache.james.jspf.wiring.WiringService#wire(java.lang.Object) */ @@ -41,10 +57,10 @@ public class WiringServiceTable extends Hashtable<Class<?>,Object> implements Wi while (i.hasNext()) { Class<?> enablingClass = i.next(); if (enablingClass.isInstance(component)) { - Method[] m = enablingClass.getDeclaredMethods(); - if (m!=null && m.length == 1 && m[0] != null) { + Method setter = SETTERS.get(enablingClass); + if (setter != null) { try { - m[0].invoke(component, new Object[] {get(enablingClass)}); + setter.invoke(component, get(enablingClass)); } catch (IllegalArgumentException e) { throw new WiringServiceException("Illegal argument invoking enabled service: "+enablingClass.toString(), e); } catch (InvocationTargetException e) { diff --git a/resolver/src/test/java/org/apache/james/jspf/wiring/WiringServiceTableTest.java b/resolver/src/test/java/org/apache/james/jspf/wiring/WiringServiceTableTest.java new file mode 100644 index 0000000..e805426 --- /dev/null +++ b/resolver/src/test/java/org/apache/james/jspf/wiring/WiringServiceTableTest.java @@ -0,0 +1,108 @@ +/**************************************************************** + * 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.james.jspf.wiring; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; + +public class WiringServiceTableTest { + + public interface NameEnabled { + void enableName(String name); + } + + public interface TwoMethodsEnabled { + void first(String value); + + void second(String value); + } + + public static class Component implements NameEnabled, TwoMethodsEnabled { + String name; + String other; + + public void enableName(String name) { + if ("boom".equals(name)) { + throw new IllegalStateException("boom"); + } + this.name = name; + } + + public void first(String value) { + other = value; + } + + public void second(String value) { + other = value; + } + } + + @Test + public void shouldCallTheSetterOfTheEnablingInterface() throws Exception { + WiringServiceTable table = new WiringServiceTable(); + table.put(NameEnabled.class, "a"); + Component component = new Component(); + + table.wire(component); + + assertEquals("a", component.name); + } + + @Test + public void shouldWireTheServiceRegisteredLast() throws Exception { + WiringServiceTable table = new WiringServiceTable(); + table.put(NameEnabled.class, "a"); + table.wire(new Component()); + table.put(NameEnabled.class, "b"); + Component component = new Component(); + + table.wire(component); + + assertEquals("b", component.name); + } + + @Test + public void shouldIgnoreAnInterfaceWithSeveralMethods() throws Exception { + WiringServiceTable table = new WiringServiceTable(); + table.put(TwoMethodsEnabled.class, "a"); + Component component = new Component(); + + table.wire(component); + + assertNull(component.other); + } + + @Test + public void shouldReportAFailingSetter() { + WiringServiceTable table = new WiringServiceTable(); + table.put(NameEnabled.class, "boom"); + + try { + table.wire(new Component()); + fail("expected a WiringServiceException"); + } catch (WiringServiceException e) { + assertTrue(e.getMessage().contains("NameEnabled")); + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
