<commit_before>/*
 * Copyright 2002-2007 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
 * 
 *      http://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.instrument.classloading.weblogic;

import java.lang.instrument.ClassFileTransformer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.util.Assert;

/**
 * Reflective wrapper around a WebLogic classloader. Used to encapsulate the
 * classloader specific methods (discovered and called through reflection) from
 * the loadtime weaver.
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 2.5
 */
class WebLogicClassLoader {

	private static final String GENERIC_CLASS_LOADER_NAME = "weblogic.utils.classloaders.GenericClassLoader";

	private static final String CLASS_PRE_PROCESSOR_NAME = "weblogic.utils.classloaders.ClassPreProcessor";


	private final ClassLoader internalClassLoader;

	private final Class wlPreProcessorClass;

	private final Method addPreProcessorMethod;

	private final Method getClassFinderMethod;

	private final Method getParentMethod;

	private final Constructor wlGenericClassLoaderConstructor;


	public WebLogicClassLoader(ClassLoader classLoader) {
		Class wlGenericClassLoaderClass = null;
		try {
			wlGenericClassLoaderClass = classLoader.loadClass(GENERIC_CLASS_LOADER_NAME);
			this.wlPreProcessorClass = classLoader.loadClass(CLASS_PRE_PROCESSOR_NAME);
			this.addPreProcessorMethod = classLoader.getClass().getMethod(
					"addInstanceClassPreProcessor", this.wlPreProcessorClass);
			this.getClassFinderMethod = classLoader.getClass().getMethod("getClassFinder");
			this.getParentMethod = classLoader.getClass().getMethod("getParent");
			this.wlGenericClassLoaderConstructor = wlGenericClassLoaderClass.getConstructor(
					this.getClassFinderMethod.getReturnType(), ClassLoader.class);
		}
		catch (Exception ex) {
			throw new IllegalStateException(
					"Could not initialize WebLogic ClassLoader because WebLogic API classes are not available", ex);
		}
		Assert.isInstanceOf(wlGenericClassLoaderClass, classLoader,
				"ClassLoader must be instance of [" + wlGenericClassLoaderClass.getName() + "]");
		this.internalClassLoader = classLoader;
	}


	public void addTransformer(ClassFileTransformer transformer) {
		Assert.notNull(transformer, "ClassFileTransformer must not be null");
		try {
			InvocationHandler adapter = new WebLogicClassPreProcessorAdapter(transformer, this.internalClassLoader);
			Object adapterInstance = Proxy.newProxyInstance(this.wlPreProcessorClass.getClassLoader(),
					new Class[] {this.wlPreProcessorClass}, adapter);<commit_msg>polishing<commit_after>/*
 * Copyright 2002-2007 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
 * 
 *      http://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.instrument.classloading.weblogic;

import java.lang.instrument.ClassFileTransformer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.util.Assert;

/**
 * Reflective wrapper around a WebLogic 10 class loader. Used to
 * encapsulate the classloader-specific methods (discovered and
 * called through reflection) from the load-time weaver.
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 2.5
 */
class WebLogicClassLoader {

	private static final String GENERIC_CLASS_LOADER_NAME = "weblogic.utils.classloaders.GenericClassLoader";

	private static final String CLASS_PRE_PROCESSOR_NAME = "weblogic.utils.classloaders.ClassPreProcessor";


	private final ClassLoader internalClassLoader;

	private final Class wlPreProcessorClass;

	private final Method addPreProcessorMethod;

	private final Method getClassFinderMethod;

	private final Method getParentMethod;

	private final Constructor wlGenericClassLoaderConstructor;


	public WebLogicClassLoader(ClassLoader classLoader) {
		Class wlGenericClassLoaderClass = null;
		try {
			wlGenericClassLoaderClass = classLoader.loadClass(GENERIC_CLASS_LOADER_NAME);
			this.wlPreProcessorClass = classLoader.loadClass(CLASS_PRE_PROCESSOR_NAME);
			this.addPreProcessorMethod = classLoader.getClass().getMethod(
					"addInstanceClassPreProcessor", this.wlPreProcessorClass);
			this.getClassFinderMethod = classLoader.getClass().getMethod("getClassFinder");
			this.getParentMethod = classLoader.getClass().getMethod("getParent");
			this.wlGenericClassLoaderConstructor = wlGenericClassLoaderClass.getConstructor(
					this.getClassFinderMethod.getReturnType(), ClassLoader.class);
		}
		catch (Exception ex) {
			throw new IllegalStateException(
					"Could not initialize WebLogic ClassLoader because WebLogic 10 API classes are not available", ex);
		}
		Assert.isInstanceOf(wlGenericClassLoaderClass, classLoader,
				"ClassLoader must be instance of [" + wlGenericClassLoaderClass.getName() + "]");
		this.internalClassLoader = classLoader;
	}


	public void addTransformer(ClassFileTransformer transformer) {
		Assert.notNull(transformer, "ClassFileTransformer must not be null");
		try {
			InvocationHandler adapter = new WebLogicClassPreProcessorAdapter(transformer, this.internalClassLoader);
			Object adapterInstance = Proxy.newProxyInstance(this.wlPreProcessorClass.getClassLoader(),
					new Class[] {this.wlPreProcessorClass}, adapter);
{'fraction_non_alphanumeric': 0.07817790089258234, 'fraction_numerical': 0.0049245921822099106, 'mean_word_length': 5.505505505505505, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093737', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1717, 'n_words': 487}