mardi 15 octobre 2019

getDeclaredMethods() + creating array = extra method?

I am working with reflection and I noticed unexpected methods. I completely narrowed it down to just these few lines of code which reproduces the problem:

import java.lang.reflect.Method;
import java.util.Arrays;

class Scratch {
    void someMethod() {
        // Yea yea, useless code but it's just for demonstration!
        Arrays.stream(new Integer[] { 0 }).toArray(Integer[]::new);
    }

    public static void main(String[] args) {
        for (Method method: Scratch.class.getDeclaredMethods()) {
            System.out.println(method);   
        }
    }
}

I am asking the JVM to give me all the declared methods. I expect to see 2 methods: main and someMethod(). But I see another one:

private static java.lang.Integer[] Scratch.lambda$someMethod$0(int)

If I remove the one and only line inside someMethod, I only get 2 methods as a result of calling getDeclaredMethods, as I would expect.

What is this lamba method? I can not call it directly on Scratch, the static method isn't available (Integer[] result = Scratch.lambda$someMethod$0(0);). Is it some private method? The docs on getDeclaredMethods() does not say anything about this behavior.

I am compiling with Java 8.





2 commentaires:

  1. Hello Admin,

    I am operating with reflection and that i noticed surprising strategies. I utterly narrowed it right down to simply these few lines of code that reproduces the matter

    Regards,
    Thanks

    Mansi Sharma

    RépondreSupprimer
  2. Hello Admin,

    I am running with mirrored image and that i observed surprising techniques. I thoroughly narrowed it proper down to genuinely these few traces of code that reproduces the problem

    Regards,
    komal

    Thanks

    RépondreSupprimer