/*
 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.function.Function;
import jdk.incubator.code.Reflect;
import jdk.incubator.code.Op;
import jdk.incubator.code.bytecode.BytecodeGenerator;
import jdk.incubator.code.dialect.core.CoreOp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/*
 * @test
 * @bug 8392557
 * @modules jdk.incubator.code
 * @library ../lib/
 * @library ../
 * @enablePreview
 * @run junit TestIntersectionTargetLambda
 */
public class TestIntersectionTargetLambda {

    interface Marker {}

    @Reflect
    public static String functionFirst() {
        var f = (Function<String, String> & Marker) x -> x;
        return f.apply("functionFirst");
    }

    @Test
    public void testFunctionFirst() throws Throwable {
        Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("functionFirst");
        CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
        MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
        Assertions.assertEquals(functionFirst(), (String) handle.invokeExact());
    }

    @Reflect
    public static String markerFirst() {
        var f = (Marker & Function<String, String>) x -> x;
        return f.apply("markerFirst");
    }

    @Test
    public void testMarkerFirst() throws Throwable {
        Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("markerFirst");
        CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
        MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
        Assertions.assertEquals(markerFirst(), (String) handle.invokeExact());
    }

    interface Base {
        String a();
        String b();
        String c();
    }

    interface A extends Base {
        default String a() { return "A"; }
    }

    interface B extends Base {
        default String b() { return "B"; }
    }

    @Reflect
    public static String complex() {
        var f = (A & B) () -> { return "C"; };
        return f.c();
    }

    @Test
    public void testComplex() throws Throwable {
        Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("complex");
        CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
        MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
        Assertions.assertEquals(complex(), (String) handle.invokeExact());
    }

    interface X {
        String get();
    }

    interface Y {
        String get();
    }

    interface Z extends X, Y {}

    @Reflect
    public static String overrideEquivalent() {
        Z f = () -> "overrideEquivalent";
        return f.get();
    }

    @Test
    public void testOverrideEquivalent() throws Throwable {
        Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("overrideEquivalent");
        CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
        MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
        Assertions.assertEquals(overrideEquivalent(), (String) handle.invokeExact());
    }
}
