<commit_before>/*
 * Copyright 2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.commons.extension;

import static com.google.common.collect.Lists.newArrayList;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader;
import org.eclipse.osgi.internal.baseadaptor.DevClassPathHelper;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring;

import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.classloaderhandler.OSGiDefaultClassLoaderHandler;
import io.github.lukehutch.fastclasspathscanner.scanner.ClasspathOrder;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanSpec;
import io.github.lukehutch.fastclasspathscanner.utils.LogNode;
import io.github.lukehutch.fastclasspathscanner.utils.ReflectionUtils;

/**
 * @since 7.0
 */
@SuppressWarnings("restriction")
public enum ClassPathScanner {
	
	INSTANCE;

	private static final long SYSTEM_BUNDLE_ID = 0L;
	
	private final ScanResult registry;

	private ClassPathScanner() {
		List<ClassLoader> classLoaders = newArrayList();
		for (BundleDescription bundleDescription : Platform.getPlatformAdmin().getState(false).getBundles()) {
			Bundle bundle = bundleDescription.getBundle();
			if (SYSTEM_BUNDLE_ID  == bundle.getBundleId()) {
				continue;
			}
			ClassLoader classLoader = bundle.adapt(BundleWiring.class).getClassLoader();
			if (classLoader != null) {
				classLoaders.add(classLoader);
			}
		}
		registry = new FastClasspathScanner()
			.setAnnotationVisibility(RetentionPolicy.RUNTIME)
			.overrideClassLoaders(classLoaders.toArray(new ClassLoader[classLoaders.size()]))
			.registerClassLoaderHandler(PDEOSGiDefaultClassLoaderHandler.class)
			.scan();
	}
	
	/**
	 * Returns classes annotated with the given annotation.
	 * 
	 * @param annotation
	 * @return
	 */
	public Collection<Class<?>> getComponentClasses(Class<? extends Annotation> annotation) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfClassesWithAnnotation(annotation);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}

	/**
	 * Returns classes that implement the given interface type.
	 * @param type
	 * @return
	 */
	public Collection<Class<?>> getComponentsClassesByInterface(Class<?> type) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfClassesImplementing(type);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}
	
	/**
	 * Returns classes that extend the given superclass type.
	 * @param type
	 * @return
	 */
	public Collection<Class<?>> getComponentsClassesBySuperclass(Class<?> type) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfSubclassesOf(type);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}
	
	/*Filters and returns Class<?> instances for the given classNames, where the class is annotated with the Component annotation*/
	private List<Class<?>> getComponentClasses(final List<String> classNames) {
		return registry.classNamesToClassRefs(classNames).stream()
				.filter(this::isComponentAnnotationPresent)
				.collect(Collectors.toList());
	}
	
	private boolean isComponentAnnotationPresent(Class<?> type) {
		return type.isAnnotationPresent(Component.class) 
				|| (type.getSuperclass() != null && isComponentAnnotationPresent(type.getSuperclass()));
	}

	/**
	 * Returns instances of classes annotated with the given annotation.
	 * @param annotation
	 * @return
	 */
	public Collection<Object> getComponentsByAnnotation(Class<? extends Annotation> annotation) {
		return getComponentsByAnnotation(annotation, Object.class);
	}
	
	/**
	 * Returns instances of classes annotated with the given annotation and subtype of the given type.
	 * 
	 * @param annotation - the expected annotation
	 * @param expectedType - the expected type
	 * @return
	 */
	public <T> Collection<T> getComponentsByAnnotation(Class<? extends Annotation> annotation, Class<T> expectedType) {<commit_msg>Revert "SO-3109: support classpath scanning of classes where the superclass..."<commit_after>/*
 * Copyright 2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.commons.extension;

import static com.google.common.collect.Lists.newArrayList;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader;
import org.eclipse.osgi.internal.baseadaptor.DevClassPathHelper;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring;

import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.classloaderhandler.OSGiDefaultClassLoaderHandler;
import io.github.lukehutch.fastclasspathscanner.scanner.ClasspathOrder;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanSpec;
import io.github.lukehutch.fastclasspathscanner.utils.LogNode;
import io.github.lukehutch.fastclasspathscanner.utils.ReflectionUtils;

/**
 * @since 7.0
 */
@SuppressWarnings("restriction")
public enum ClassPathScanner {
	
	INSTANCE;

	private static final long SYSTEM_BUNDLE_ID = 0L;
	
	private final ScanResult registry;

	private ClassPathScanner() {
		List<ClassLoader> classLoaders = newArrayList();
		for (BundleDescription bundleDescription : Platform.getPlatformAdmin().getState(false).getBundles()) {
			Bundle bundle = bundleDescription.getBundle();
			if (SYSTEM_BUNDLE_ID  == bundle.getBundleId()) {
				continue;
			}
			ClassLoader classLoader = bundle.adapt(BundleWiring.class).getClassLoader();
			if (classLoader != null) {
				classLoaders.add(classLoader);
			}
		}
		registry = new FastClasspathScanner()
			.setAnnotationVisibility(RetentionPolicy.RUNTIME)
			.overrideClassLoaders(classLoaders.toArray(new ClassLoader[classLoaders.size()]))
			.registerClassLoaderHandler(PDEOSGiDefaultClassLoaderHandler.class)
			.scan();
	}
	
	/**
	 * Returns classes annotated with the given annotation.
	 * 
	 * @param annotation
	 * @return
	 */
	public Collection<Class<?>> getComponentClasses(Class<? extends Annotation> annotation) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfClassesWithAnnotation(annotation);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}

	/**
	 * Returns classes that implement the given interface type.
	 * @param type
	 * @return
	 */
	public Collection<Class<?>> getComponentsClassesByInterface(Class<?> type) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfClassesImplementing(type);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}
	
	/**
	 * Returns classes that extend the given superclass type.
	 * @param type
	 * @return
	 */
	public Collection<Class<?>> getComponentsClassesBySuperclass(Class<?> type) {
		final List<String> namesOfClassesWithAnnotation = registry.getNamesOfSubclassesOf(type);
		return getComponentClasses(namesOfClassesWithAnnotation);
	}
	
	/*Filters and returns Class<?> instances for the given classNames, where the class is annotated with the Component annotation*/
	private List<Class<?>> getComponentClasses(final List<String> classNames) {
		return registry.classNamesToClassRefs(classNames).stream()
				.filter(type -> type.isAnnotationPresent(Component.class))
				.collect(Collectors.toList());
	}
	
	/**
	 * Returns instances of classes annotated with the given annotation.
	 * @param annotation
	 * @return
	 */
	public Collection<Object> getComponentsByAnnotation(Class<? extends Annotation> annotation) {
		return getComponentsByAnnotation(annotation, Object.class);
	}
	
	/**
	 * Returns instances of classes annotated with the given annotation and subtype of the given type.
	 * 
	 * @param annotation - the expected annotation
	 * @param expectedType - the expected type
	 * @return
	 */
	public <T> Collection<T> getComponentsByAnnotation(Class<? extends Annotation> annotation, Class<T> expectedType) {
{'fraction_non_alphanumeric': 0.09621887488472179, 'fraction_numerical': 0.003279024490214161, 'mean_word_length': 5.4, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 51, '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': '539473', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2660, 'n_words': 671}