Skip to content

Commit

Permalink
Make sure that ChannelReplacements work for dynamic RequestHandlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed May 4, 2024
1 parent 00eedd3 commit 0c425cb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
12 changes: 6 additions & 6 deletions org.jgrapes.core/src/org/jgrapes/core/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public abstract class Component extends ComponentVertex
* itself.
*/
public Component() {
super();
super(null);
componentChannel = this;
initComponentsHandlers(null);
initComponentsHandlers();
}

/**
Expand All @@ -70,13 +70,13 @@ public Component() {
* {@link Manager#fire(Event, Channel...)} sends the event to
*/
public Component(Channel componentChannel) {
super();
super(null);
if (componentChannel == SELF) {
this.componentChannel = this;
} else {
this.componentChannel = componentChannel;
}
initComponentsHandlers(null);
initComponentsHandlers();
}

/**
Expand All @@ -91,13 +91,13 @@ public Component(Channel componentChannel) {
*/
public Component(
Channel componentChannel, ChannelReplacements channelReplacements) {
super();
super(channelReplacements);
if (componentChannel == SELF) {
this.componentChannel = this;
} else {
this.componentChannel = componentChannel;
}
initComponentsHandlers(channelReplacements);
initComponentsHandlers();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private static Channel getComponentChannel(Field field) {
*/
private ComponentProxy(
Field field, ComponentType component, Channel componentChannel) {
super(null);
this.component = component;
try {
field.set(component, this);
Expand All @@ -102,7 +103,7 @@ private ComponentProxy(
componentChannel = this;
}
this.componentChannel = componentChannel;
initComponentsHandlers(null);
initComponentsHandlers();
} catch (SecurityException | IllegalAccessException e) {
throw (RuntimeException) new IllegalArgumentException(
"Cannot access component's manager attribute").initCause(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,27 @@ public abstract class ComponentVertex implements Manager, Channel {
private final List<ComponentVertex> children = new ArrayList<>();
/** The handlers provided by this component. */
private List<HandlerReference> handlers;
/** The channel replacements. */
private final ChannelReplacements channelReplacements;

/**
/**
*
* Initialize the ComponentVertex. By default it forms a stand-alone
* tree, i.e. the root is set to the component itself.
*
* @param channelReplacements the channel replacements
*/
protected ComponentVertex(ChannelReplacements channelReplacements) {
this.channelReplacements = channelReplacements;
}

/**
* Return the channel replacements passed to the constructor.
*
* @return the channel replacements
*/
protected ComponentVertex() {
// Nothing to do, but appropriate for abstract class.
public ChannelReplacements channelReplacements() {
return channelReplacements;
}

/**
Expand All @@ -80,8 +94,7 @@ protected ComponentVertex() {
* correct value.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
protected void initComponentsHandlers(
ChannelReplacements channelReplacements) {
protected void initComponentsHandlers() {
handlers = new ArrayList<>();
// Have a look at all methods.
for (Method m : component().getClass().getMethods()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jgrapes.core.annotation.Handler.NoEvent;
import org.jgrapes.core.annotation.HandlerDefinition;
import org.jgrapes.core.annotation.HandlerDefinition.ChannelReplacements;
import org.jgrapes.core.internal.ComponentVertex;
import org.jgrapes.core.internal.EventBase;
import org.jgrapes.http.ResourcePattern;
import org.jgrapes.http.events.Request;
Expand Down Expand Up @@ -167,7 +168,8 @@ public static void add(ComponentType component, String method,
add(component, method, pattern, Integer.valueOf(priority));
}

@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
@SuppressWarnings({ "PMD.AvoidBranchingStatementAsLastInLoop",
"PMD.CognitiveComplexity" })
private static void add(ComponentType component, String method,
String pattern, Integer priority) {
try {
Expand All @@ -187,7 +189,10 @@ private static void add(ComponentType component, String method,
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
Scope scope = new Scope(component, m,
(RequestHandler) annotation,
Collections.emptyMap(), pattern);
component instanceof ComponentVertex vertex
? vertex.channelReplacements()
: Collections.emptyMap(),
pattern);
Components.manager(component)
.addHandler(m, scope, priority == null
? ((RequestHandler) annotation).priority()
Expand Down

0 comments on commit 0c425cb

Please sign in to comment.