Note that there are some explanatory texts on larger screens.

plurals
  1. POReflection much slower than normal
    primarykey
    data
    text
    <p>I am currently working on a Java project in which I am trying to learn the ins and outs. In previous projects, I have used Java reflection in order to create a toString() by calling each getter in an object and parsing it to display the value. This method has been a helpful, clean, and dynamic way to display the data.</p> <p>Below is a heavily simplified version of my code:</p> <pre><code>private static String objectToString(Object o) { LOGGER.debug("entering ObjectStringUtils::objectToString()"); .... Class&lt;?&gt; oClass = o.getClass(); String className = oClass.getName(); Method[] methods = oClass.getMethods(); for (Method m : methods) { if ([method is a getter]) { String methodName; Object value; try { methodName= m.getName(); LOGGER.debug("Invoking " + className + "::" + methodName); Object value = m.invoke(o); LOGGER.debug("Invoked " + className + "::" + methodName); } catch (Exception e) { e.printStackTrace(); value = null; } LOGGER.debug(methodName+ " -&gt; " + value); } } } </code></pre> <p>This produces logger output which looks like this:</p> <pre><code>14:47:49,478 [] DEBUG ObjectStringUtils:? - Invoking org.hibernate.impl.SessionImpl::isOpen 14:47:49,613 [] DEBUG ObjectStringUtils:? - Invoked org.hibernate.impl.SessionImpl::isOpen 14:47:49,613 [] DEBUG ObjectStringUtils:? - isOpen -&gt; true </code></pre> <p>Notice that it took Java <strong>139 milliseconds</strong> to call the function. <strong>It takes this long to perform the reflection in any method in any class, even if the method is only a standard getter which performs no logic other than to return a value.</strong> This means that it takes far too long to perform the operation when there are multiple nested values involved. When I used reflection previously on WebSphere 7, it took a tiny fraction of this long to perform the operation.</p> <p>So my question is: <strong>why</strong> is it taking so long to process? I understand that reflection is slower, but it shouldn't be on the magnitude of 140 milliseconds to call a getter. Is this an artifact of the way it takes WebLogic a long time to call a function, or the fact that line numbers appear to be stripped from the .class files? So far, I don't have any idea.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload