diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/AnnotatedAbstractModule.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/AnnotatedAbstractModule.java index 00390bd5..fce39d24 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/AnnotatedAbstractModule.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/AnnotatedAbstractModule.java @@ -1,12 +1,16 @@ package cz.cvut.spipes.modules; +import cz.cvut.spipes.engine.ExecutionContext; import cz.cvut.spipes.exception.ModuleConfigurationInconsistentException; import cz.cvut.spipes.modules.handlers.*; +import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.ResourceFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -39,6 +43,9 @@ public abstract class AnnotatedAbstractModule extends AbstractModule { *
  • A {@link Setter} is selected based on the field's type (e.g., {@link FieldSetter} for regular fields, {@link ListSetter} for lists).
  • *
  • The {@link HandlerRegistry} retrieves a matching {@link Handler} for the field's type, * which is responsible for setting the value of the field based on its {@link Parameter#iri()}.
  • + *
  • If the field specifies a custom handler (via {@link Parameter#handler()}), an instance of the custom handler is created using its + * constructor that accepts {@link Resource}, {@link ExecutionContext}, and {@link Setter} as parameters. This custom handler is + * then used to set the field's value.
  • * * *

    This method processes all declared fields in the class and its subclasses, @@ -85,9 +92,21 @@ public void loadConfiguration() { } else { setter = new FieldSetter(f, this); } - HandlerRegistry handlerRegistry = HandlerRegistry.getInstance(); - Handler handler = handlerRegistry.getHandler(f.getType(), resource, executionContext, setter); - handler.setValueByProperty(ResourceFactory.createProperty(p.iri())); + Class handlerClazz = p.handler(); + + if(handlerClazz != DefaultHandler.class){ + try{ + Constructor constructor = (Constructor) handlerClazz.getConstructor(Resource.class, ExecutionContext.class, Setter.class); + Handler typeHandler = constructor.newInstance(resource, executionContext, setter); + typeHandler.setValueByProperty(ResourceFactory.createProperty(p.iri())); + } catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { + throw new IllegalArgumentException("No suitable constructor found for handler " + handlerClazz); + } + }else{ + HandlerRegistry handlerRegistry = HandlerRegistry.getInstance(); + Handler typeHandler = handlerRegistry.getHandler(f.getType(), resource, executionContext, setter); + typeHandler.setValueByProperty(ResourceFactory.createProperty(p.iri())); + } } } loadManualConfiguration(); diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/Parameter.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/Parameter.java index 48d22521..04bb8223 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/Parameter.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/Parameter.java @@ -1,5 +1,7 @@ package cz.cvut.spipes.modules; + +import cz.cvut.spipes.modules.handlers.DefaultHandler; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -10,4 +12,5 @@ public @interface Parameter { String iri(); String comment() default ""; + Class handler() default DefaultHandler.class; } diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/handlers/DefaultHandler.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/handlers/DefaultHandler.java new file mode 100644 index 00000000..41a72c76 --- /dev/null +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/handlers/DefaultHandler.java @@ -0,0 +1,17 @@ +package cz.cvut.spipes.modules.handlers; + +import cz.cvut.spipes.engine.ExecutionContext; +import org.apache.jena.rdf.model.Property; +import org.apache.jena.rdf.model.Resource; + +public class DefaultHandler extends Handler{ + + public DefaultHandler(Resource resource, ExecutionContext executionContext, Setter setter) { + super(resource, executionContext, setter); + } + + @Override + public void setValueByProperty(Property property) { + + } +}