Skip to content

Commit

Permalink
[#286] Extend @Parameter annotation by field handler
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Dec 16, 2024
1 parent 313ec43 commit 73cd6b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,6 +43,9 @@ public abstract class AnnotatedAbstractModule extends AbstractModule {
* <li>A {@link Setter} is selected based on the field's type (e.g., {@link FieldSetter} for regular fields, {@link ListSetter} for lists).</li>
* <li>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()}.</li>
* <li>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.</li>
* </ul>
*
* <p>This method processes all declared fields in the class and its subclasses,
Expand Down Expand Up @@ -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<? extends Handler> constructor = (Constructor<? extends Handler>) 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,4 +12,5 @@
public @interface Parameter {
String iri();
String comment() default "";
Class<?> handler() default DefaultHandler.class;
}
Original file line number Diff line number Diff line change
@@ -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<Object>{

public DefaultHandler(Resource resource, ExecutionContext executionContext, Setter<? super Object> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {

}
}

0 comments on commit 73cd6b5

Please sign in to comment.