All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.code.reference.point.SourceCodeReferencePointsLoader Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.code.reference.point;

import com.softicar.platform.common.code.classpath.metadata.ClasspathFilesMetadata;
import com.softicar.platform.common.core.exception.ExceptionsCollector;
import com.softicar.platform.common.core.utils.ReflectionUtils;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

/**
 * Searches, loads and instantiates all classes with an
 * {@link SourceCodeReferencePointUuid} annotation.
 *
 * @author Oliver Richers
 */
class SourceCodeReferencePointsLoader {

	private final Map referencePoints;
	private final ExceptionsCollector exceptionsCollector;
	private boolean loaded;

	public SourceCodeReferencePointsLoader() {

		this.referencePoints = new TreeMap<>();
		this.exceptionsCollector = new ExceptionsCollector();
		this.loaded = false;
	}

	public Map loadAll() {

		if (!loaded) {
			ClasspathFilesMetadata//
				.getInstance()
				.getDirectlyAnnotatedClasses(SourceCodeReferencePointUuid.class)
				.stream()
				.filter(ReflectionUtils::isPublic)
				.forEach(this::addReferencePoint);
			this.loaded = true;
		}

		exceptionsCollector.throwIfNotEmpty();
		return referencePoints;
	}

	private void addReferencePoint(Class referencePointClass) {

		try {
			referencePoints.put(getUuid(referencePointClass), createInstance(referencePointClass));
		} catch (Exception exception) {
			exceptionsCollector.add(new SourceCodeReferencePointLoadingException(referencePointClass, exception));
		}
	}

	private UUID getUuid(Class referencePointClass) {

		return SourceCodeReferencePoints.getUuidOrThrow(referencePointClass);
	}

	private ISourceCodeReferencePoint createInstance(Class referencePointClass) throws ReflectiveOperationException {

		return (ISourceCodeReferencePoint) referencePointClass.getConstructor().newInstance();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy