From f2f20c30d3b862d1b3305e429a2e5ed23e3617da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Fuch=C3=9F?= Date: Thu, 5 Dec 2024 16:35:43 +0100 Subject: [PATCH] Make abstractconfigurable ready to be used in serializable classes --- .../configuration/AbstractConfigurable.java | 32 ++++++------------- .../mcse/ardoco/core/pipeline/Pipeline.java | 6 ++-- .../mcse/ardoco/core/execution/ArDoCo.java | 4 +-- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/configuration/AbstractConfigurable.java b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/configuration/AbstractConfigurable.java index 292e42080..877d2f287 100644 --- a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/configuration/AbstractConfigurable.java +++ b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/configuration/AbstractConfigurable.java @@ -1,10 +1,6 @@ /* Licensed under MIT 2022-2024. */ package edu.kit.kastel.mcse.ardoco.core.configuration; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serial; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; @@ -14,7 +10,6 @@ import java.util.SortedMap; import java.util.TreeMap; -import org.apache.commons.lang3.reflect.FieldUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,12 +17,13 @@ @Deterministic public abstract class AbstractConfigurable implements IConfigurable { - protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - public static final String CLASS_ATTRIBUTE_CONNECTOR = "::"; public static final String KEY_VALUE_CONNECTOR = "="; public static final String LIST_SEPARATOR = ","; + @SuppressWarnings("java:S2065") // The logger is used in the subclasses that are serializable + private transient Logger logger; + private SortedMap lastAppliedConfiguration = new TreeMap<>(); @Override @@ -50,7 +46,7 @@ private void applyConfiguration(SortedMap additionalConfiguratio } if (currentClassInHierarchy.getAnnotation(NoConfiguration.class) != null) { - this.logger.debug("Skipping configuration for class {}", currentClassInHierarchy.getSimpleName()); + this.getLogger().debug("Skipping configuration for class {}", currentClassInHierarchy.getSimpleName()); return; } @@ -101,7 +97,7 @@ private void setValue(Field field, String value) { field.setAccessible(true); field.set(this, parsedValue); } catch (Exception e) { - this.logger.error(e.getMessage(), e); + this.getLogger().error(e.getMessage(), e); } } @@ -131,20 +127,10 @@ private Object parse(Field field, Class fieldsClass, String value) { throw new IllegalArgumentException("Could not find a parse method for fields of type: " + fieldsClass); } - @Serial - private void writeObject(ObjectOutputStream objectOutputStream) throws IOException { - objectOutputStream.defaultWriteObject(); - } - - @Serial - private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - objectInputStream.defaultReadObject(); - try { - var loggerField = Arrays.stream(FieldUtils.getAllFields(this.getClass())).filter(f -> f.getName().equals("logger")).findFirst().orElseThrow(); - loggerField.setAccessible(true); - loggerField.set(this, LoggerFactory.getLogger(this.getClass())); - } catch (IllegalAccessException e) { - throw new IllegalAccessError(e.getMessage()); + protected final Logger getLogger() { + if (this.logger == null) { + this.logger = LoggerFactory.getLogger(this.getClass()); } + return this.logger; } } diff --git a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/pipeline/Pipeline.java b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/pipeline/Pipeline.java index 1f0b9092b..a68924cf8 100644 --- a/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/pipeline/Pipeline.java +++ b/framework/common/src/main/java/edu/kit/kastel/mcse/ardoco/core/pipeline/Pipeline.java @@ -74,12 +74,12 @@ public boolean wasExecuted() { public void process() { this.preparePipelineSteps(); for (var pipelineStep : this.pipelineSteps) { - this.logger.info("Starting {} - {}", this.getId(), pipelineStep.getId()); + this.getLogger().info("Starting {} - {}", this.getId(), pipelineStep.getId()); var start = Instant.now(); pipelineStep.run(); - if (this.logger.isInfoEnabled()) { + if (this.getLogger().isInfoEnabled()) { var end = Instant.now(); var duration = Duration.between(start, end); long minutesPart = duration.toMinutes(); @@ -92,7 +92,7 @@ public void process() { durationString = String.format("%01d.%03d s", secondsPart, millisPart); } - this.logger.info("Finished {} - {} in {}", this.getId(), pipelineStep.getId(), durationString); + this.getLogger().info("Finished {} - {} in {}", this.getId(), pipelineStep.getId(), durationString); } } } diff --git a/pipeline-core/src/main/java/edu/kit/kastel/mcse/ardoco/core/execution/ArDoCo.java b/pipeline-core/src/main/java/edu/kit/kastel/mcse/ardoco/core/execution/ArDoCo.java index 13d552ee9..ef5e985b0 100644 --- a/pipeline-core/src/main/java/edu/kit/kastel/mcse/ardoco/core/execution/ArDoCo.java +++ b/pipeline-core/src/main/java/edu/kit/kastel/mcse/ardoco/core/execution/ArDoCo.java @@ -68,7 +68,7 @@ public ArDoCoResult runAndSave(File outputDir) { classLogger.info("Starting {}", this.projectName); if (!this.hasPipelineSteps()) { - this.logger.error("Pipeline has not been defined and initialized beforehand. Aborting!"); + this.getLogger().error("Pipeline has not been defined and initialized beforehand. Aborting!"); return null; } @@ -79,7 +79,7 @@ public ArDoCoResult runAndSave(File outputDir) { ArDoCoResult arDoCoResult = new ArDoCoResult(this.getDataRepository()); saveOutput(this.projectName, outputDir, arDoCoResult); - if (this.logger.isInfoEnabled()) { + if (this.getLogger().isInfoEnabled()) { var duration = Duration.between(startTime, endTime); long minutesPart = duration.toMinutes(); int secondsPart = duration.toSecondsPart();