You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2019/05/07 12:28:41 UTC

[cxf] branch 3.2.x-fixes updated: [CXF-8028] Use ClassValue to cache/record the method/field needed for the SAAJ workarounds to avoid expensive reflection on every element.

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

dkulp pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.2.x-fixes by this push:
     new aafb97c  [CXF-8028] Use ClassValue to cache/record the method/field needed for the SAAJ workarounds to avoid expensive reflection on every element.
aafb97c is described below

commit aafb97c17b2446c14a45d74e148fabe06259b1e2
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Mon May 6 14:10:32 2019 -0400

    [CXF-8028] Use ClassValue to cache/record the method/field needed for the SAAJ workarounds to avoid expensive reflection on every element.
---
 .../main/java/org/apache/cxf/helpers/DOMUtils.java | 48 ++++++++++++++--------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/helpers/DOMUtils.java b/core/src/main/java/org/apache/cxf/helpers/DOMUtils.java
index f8e9462..ec2db05 100644
--- a/core/src/main/java/org/apache/cxf/helpers/DOMUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/DOMUtils.java
@@ -22,6 +22,7 @@ package org.apache.cxf.helpers;
 import java.io.IOException;
 import java.io.StringReader;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -54,6 +55,7 @@ import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
 
 /**
@@ -66,8 +68,25 @@ public final class DOMUtils {
     private static final String XMLNAMESPACE = "xmlns";
     private static volatile Document emptyDocument;
 
-
-
+    private static final ClassValue<Method> GET_DOM_ELEMENTS_METHODS = new ClassValue<Method>() {
+        @Override
+        protected Method computeValue(Class<?> type) {
+            try {
+                return ReflectionUtil.getMethod(type, "getDomElement");
+            } catch (NoSuchMethodException e) {
+                //best effort to try, do nothing if NoSuchMethodException
+                return null;
+            }
+        }
+    };
+    private static final ClassValue<Field> GET_DOCUMENT_FRAGMENT_FIELDS = new ClassValue<Field>() {
+        @Override
+        protected Field computeValue(Class<?> type) {
+            return ReflectionUtil.getDeclaredField(type, "documentFragment");
+        }
+        
+    };
+        
     static {
         try {
             Method[] methods = DOMUtils.class.getClassLoader().
@@ -751,13 +770,13 @@ public final class DOMUtils {
     public static Node getDomElement(Node node) {
         if (node != null && isJava9SAAJ()) {
             //java9plus hack
-            try {
-                Method method = node.getClass().getMethod("getDomElement");
-                node = (Node)method.invoke(node);
-            } catch (NoSuchMethodException e) {
-                //best effort to try, do nothing if NoSuchMethodException
-            } catch (Exception e) {
-                throw new RuntimeException(e);
+            Method method = GET_DOM_ELEMENTS_METHODS.get(node.getClass());
+            if (method != null) {
+                try {
+                    return (Node)ReflectionUtil.setAccessible(method).invoke(node);
+                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+                    throw new RuntimeException(e);
+                }
             }
         }
         return node;
@@ -771,14 +790,9 @@ public final class DOMUtils {
     public static DocumentFragment getDomDocumentFragment(DocumentFragment fragment) {
         if (fragment != null && isJava9SAAJ()) {
             //java9 plus hack
-            try {
-                Field f = fragment.getClass().getDeclaredField("documentFragment");
-                f.setAccessible(true);
-                fragment = (DocumentFragment) f.get(fragment);
-            } catch (NoSuchFieldException e) {
-                //best effort to try, do nothing if NoSuchFieldException
-            } catch (Exception e) {
-                throw new RuntimeException(e);
+            Field f = GET_DOCUMENT_FRAGMENT_FIELDS.get(fragment.getClass());
+            if (f != null) {
+                return ReflectionUtil.accessDeclaredField(f, fragment, DocumentFragment.class);
             }
         }
         return fragment;