/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aot.test.generator.compile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;

/**
 * {@link ClassLoader} implementation to support
 * {@link CompileWithTargetClassAccess @CompileWithTargetClassAccess}.
 *
 * @author Phillip Webb
 * @author Sam Brannen
 * @since 6.0
 */
final class CompileWithTargetClassAccessClassLoader extends ClassLoader {

	private static final Set<String> passthroughPackages =
			Set.of("org.junit", "org.testng", "org.hamcrest");

	private final ClassLoader testClassLoader;


	public CompileWithTargetClassAccessClassLoader(ClassLoader testClassLoader) {
		super(testClassLoader.getParent());
		this.testClassLoader = testClassLoader;
	}


	@Override
	public Class<?> loadClass(String name) throws ClassNotFoundException {
		if (passthroughPackages.stream().anyMatch(pkg -> name.startsWith(pkg))) {
			return Class.forName(name, false, this.testClassLoader);
		}
		return super.loadClass(name);
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		String resourceName = name.replace(".", "/") + ".class";
		InputStream stream = this.testClassLoader.getResourceAsStream(resourceName);
		if (stream != null) {
			try (stream) {
				byte[] bytes = stream.readAllBytes();
				return defineClass(name, bytes, 0, bytes.length, null);
			}
			catch (IOException ex) {
			}
		}
		return super.findClass(name);
	}


	// Invoked reflectively by DynamicClassLoader.findDefineClassMethod(ClassLoader)
	Class<?> defineClassWithTargetAccess(String name, byte[] b, int off, int len) {
		return super.defineClass(name, b, off, len);
	}

	@Override
	protected Enumeration<URL> findResources(String name) throws IOException {
		return this.testClassLoader.getResources(name);
	}

	@Override
	protected URL findResource(String name) {
		return this.testClassLoader.getResource(name);
	}

}

@@ -783,30 +783,8 @@
 ion;
-%0Aimport java.util.Set;
 %0A%0A/*
@@ -936,31 +936,8 @@
 ebb%0A
- * @author Sam Brannen%0A
  * @
@@ -1029,119 +1029,8 @@
  %7B%0A%0A
-%09private static final Set%3CString%3E passthroughPackages =%0A%09%09%09Set.of(%22org.junit%22, %22org.testng%22, %22org.hamcrest%22);%0A%0A
 %09pri
@@ -1328,52 +1328,39 @@
 if (
-passthroughPackages.stream().anyMatch(pkg -%3E
+name.startsWith(%22org.junit%22) %7C%7C
  nam
@@ -1376,12 +1376,22 @@
 ith(
-pkg)
+%22org.hamcrest%22
 )) %7B

Revert "Support TestNG with @CompileWithTargetClassAccess"