Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Reflection constructors orders
    primarykey
    data
    text
    <p>Does the order of class constructs return different results in reflection?</p> <p>I have a problem using reflection. Here is an example:</p> <pre><code>public class A{ public A() { do something here } public A(int i) { do something here } } </code></pre> <p>Then, in another class, I use reflection to get the constructors with getConstructors() method. But here is the problem. It returns only the constructor with parameters.</p> <p>So I modified the code and changed the order of constructos as:</p> <pre><code>public class A{ public A(int i) { do something here } public A() { do something here } } </code></pre> <p>Then, it returns two constructors correctly. </p> <p>Another interesting point is OpenJDK works fine with two examples, but Sun JDK does not. And I have many classes like this, SunJDK works fine in some of them, and wrong in others. Very strange.</p> <p>Any reason?</p> <p>Edit: It is a big project, here is some code for the reflection</p> <pre><code>Constructor&lt;?&gt;[] constructors = handlerClass.getDeclaredConstructors(); for (Constructor&lt;?&gt; constructor : constructors) { Class&lt;?&gt;[] parameterTypes = constructor.getParameterTypes(); </code></pre> <p>System.out.println("- " + constructor.toString()); </p> <pre><code> if (nonConnection == true &amp;&amp; parameterTypes.length == 0) { return constructor; } else if (parameterTypes.length &gt; 1) { continue; } else if (parameterTypes.length == 1) { Class&lt;?&gt; parameterType = parameterTypes[0]; if (TableManipulator.class.isAssignableFrom(parameterType)) { return constructor; } } } </code></pre> <p>handlerClass is the class to get constructors from.</p> <p>ps. I had a look at the bytecode, the bytecodes are the same except the order of the constructors.</p> <p>edit: I added a System.out to display the constructors after getParameterTypes(). It shows only one constructor.... with the old ordering, but two constructors with the new ordering.</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