com.softicar.platform.common.code.reference.point.SourceCodeReferencePointsLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
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();
}
}