Skip to content

Commit

Permalink
Reactivate PMD and fix issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed May 2, 2024
1 parent fb3dd8e commit 8868bbd
Show file tree
Hide file tree
Showing 55 changed files with 233 additions and 205 deletions.
1 change: 1 addition & 0 deletions org.jgrapes.core/src/org/jgrapes/core/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public Object defaultCriterion() {
* @see Channel#isEligibleFor(Object)
*/
@Override
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public boolean isEligibleFor(Object value) {
return value.equals(Channel.class)
|| value == defaultCriterion();
Expand Down
17 changes: 10 additions & 7 deletions org.jgrapes.core/src/org/jgrapes/core/Components.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
Expand All @@ -48,13 +49,15 @@
*/
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.ClassNamingConventions",
"PMD.ExcessivePublicCount", "PMD.ExcessiveClassLength",
"PMD.ClassWithOnlyPrivateConstructorsShouldBeFinal" })
"PMD.ClassWithOnlyPrivateConstructorsShouldBeFinal",
"PMD.CouplingBetweenObjects" })
public class Components {

private static ExecutorService defaultExecutorService
= Executors.newCachedThreadPool(
new ThreadFactory() {
@SuppressWarnings("PMD.CommentRequired")
@SuppressWarnings({ "PMD.CommentRequired",
"PMD.MissingOverride" })
public Thread newThread(Runnable runnable) {
Thread thread
= Executors.defaultThreadFactory().newThread(runnable);
Expand Down Expand Up @@ -246,7 +249,7 @@ public static String objectName(Object object) {
return "<null>";
}
StringBuilder builder = new StringBuilder();
builder.append(Components.className(object.getClass()))
builder.append(className(object.getClass()))
.append('#')
.append(objectId(object));
return builder.toString();
Expand Down Expand Up @@ -452,13 +455,13 @@ public static void setTimerExecutorService(
*/
private static class Scheduler extends Thread {

private final PriorityQueue<Timer> timers
= new PriorityQueue<>(10,
Comparator.comparing(Timer::scheduledFor));
private final Queue<Timer> timers = new PriorityQueue<>(10,
Comparator.comparing(Timer::scheduledFor));

/**
* Instantiates a new scheduler.
*/
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
public Scheduler() {
setName("Components.Scheduler");
setDaemon(true);
Expand Down Expand Up @@ -520,7 +523,7 @@ public void run() {
}
try {
synchronized (timers) {
if (timers.size() == 0) {
if (timers.isEmpty()) {
timers.wait();
} else {
timers
Expand Down
2 changes: 1 addition & 1 deletion org.jgrapes.core/src/org/jgrapes/core/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public <V> Optional<V> associated(Object by, Class<V> type) {
}
return Optional.ofNullable(contextData.get(by))
.filter(found -> type.isAssignableFrom(found.getClass()))
.map(match -> type.cast(match));
.map(type::cast);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion org.jgrapes.core/src/org/jgrapes/core/Subchannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public <V> Optional<V> associated(Object by, Class<V> type) {
}
return Optional.ofNullable(contextData.get(by))
.filter(found -> type.isAssignableFrom(found.getClass()))
.map(match -> type.cast(match));
.map(type::cast);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion org.jgrapes.core/src/org/jgrapes/core/TypedIdKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static <V> V associate(Associator associator, Serializable id,
@SuppressWarnings({ "unchecked" })
public static <V> V put(Map<? super TypedIdKey<V>, ? super V> map,
Serializable id, V value) {
map.put(new TypedIdKey<V>((Class<V>) value.getClass(), id), value);
map.put(new TypedIdKey<>((Class<V>) value.getClass(), id), value);
return value;
}

Expand Down
14 changes: 6 additions & 8 deletions org.jgrapes.core/src/org/jgrapes/core/annotation/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private static void addInternal(ComponentType component, String method,
}
for (Annotation annotation : m.getDeclaredAnnotations()) {
Class<?> annoType = annotation.annotationType();
if (!(annoType.equals(Handler.class))) {
if (!annoType.equals(Handler.class)) {
continue;
}
HandlerDefinition hda
Expand All @@ -407,8 +407,8 @@ private static void addInternal(ComponentType component, String method,
"No method named \"" + method + "\" with DynamicHandler"
+ " annotation and correct parameter list.");
} catch (SecurityException e) {
throw (RuntimeException) (new IllegalArgumentException()
.initCause(e));
throw (RuntimeException) new IllegalArgumentException()
.initCause(e);
}
}

Expand Down Expand Up @@ -548,7 +548,7 @@ public boolean includes(Eligible event, Eligible[] channels) {
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder(100);
builder.append("Scope [");
if (eventCriteria != null) {
builder.append("handledEvents=")
Expand All @@ -557,12 +557,10 @@ public String toString() {
return Components.className((Class<?>) crit);
}
return crit.toString();
}).collect(Collectors.toSet()));
builder.append(", ");
}).collect(Collectors.toSet())).append(", ");
}
if (channelCriteria != null) {
builder.append("handledChannels=");
builder.append(channelCriteria);
builder.append("handledChannels=").append(channelCriteria);
}
builder.append(']');
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ interface Evaluator {
* @return the scope or {@code null} if a handler for the method
* should not be created
*/
@SuppressWarnings("PMD.LooseCoupling")
HandlerScope scope(ComponentType component, Method method,
ChannelReplacements channelReplacements);

Expand All @@ -94,6 +95,7 @@ HandlerScope scope(ComponentType component, Method method,
* @param method the method
* @return the result
*/
@SuppressWarnings("PMD.UselessParentheses")
static boolean checkMethodSignature(Method method) {
return method.getParameterTypes().length == 0
|| method.getParameterTypes().length == 1
Expand Down Expand Up @@ -121,6 +123,7 @@ class ChannelReplacements // NOPMD (for missing serialVersionUID)
*
* @return the channel replacements
*/
@SuppressWarnings("PMD.LooseCoupling")
public static ChannelReplacements create() {
return new ChannelReplacements();
}
Expand All @@ -132,6 +135,7 @@ public static ChannelReplacements create() {
* @param replacements the replacements
* @return the channel replacements for easy chaining
*/
@SuppressWarnings("PMD.LooseCoupling")
public ChannelReplacements add(
Class<? extends Channel> annotationCriterion,
Channel... replacements) {
Expand Down
2 changes: 1 addition & 1 deletion org.jgrapes.core/src/org/jgrapes/core/events/Attached.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ComponentType parent() {
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder(50);
builder.append(Components.objectName(this))
.append(" [");
if (parent == null) {
Expand Down
2 changes: 1 addition & 1 deletion org.jgrapes.core/src/org/jgrapes/core/events/Detached.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ComponentType parent() {
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder(50);
builder.append(Components.objectName(this))
.append(" [")
.append(parent)
Expand Down
9 changes: 3 additions & 6 deletions org.jgrapes.core/src/org/jgrapes/core/events/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,18 @@ public Throwable throwable() {
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder(50);
builder.append(Components.objectName(this))
.append(" [");
if (channels().length > 0) {
builder.append("channels=");
builder.append(Channel.toString(channels()));
}
if (message != null) {
builder.append(", message=\"");
builder.append(message);
builder.append('"');
builder.append(", message=\"").append(message).append('"');
}
if (event != null) {
builder.append(", caused by: ");
builder.append(event.toString());
builder.append(", caused by: ").append(event.toString());
}
builder.append(']');
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public CallableActionEvent(String name, Callable<V> callable) {
}

@SuppressWarnings("PMD.SignatureDeclareThrowsException")
@Override
/* default */ void execute() throws Exception {
setResult(action.call());
}
Expand All @@ -125,6 +126,7 @@ public RunnableActionEvent(String name, Runnable action) {
}

@SuppressWarnings("PMD.SignatureDeclareThrowsException")
@Override
/* default */ void execute() throws Exception {
action.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public String toString() {
// Avoid problem with concurrency
var bufd = buffered;
if (bufd != null) {
builder.append("buffered=");
builder.append(bufd);
builder.append("buffered=").append(bufd);
}
builder.append(']');
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public <T extends Event<?>> T fire(T event, Channel... channels) {
allowNext.set(null);
if (!allowed // i.e. if not allowed anyway...
&& (allowedSourceRef.get() == null
|| (allowedSourceRef.get())
|| allowedSourceRef.get()
.wrapped() != componentTree.dispatchingPipeline())) {
CoreUtils.fireRestrictionLogger.log(Level.SEVERE,
Components.objectName(componentTree.dispatchingPipeline())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class ComponentProxy extends ComponentVertex {
/** The reference to the actual component. */
private final ComponentType component;
/** The referenced component's channel. */
private Channel componentChannel;
private final Channel componentChannel;

private static Field getManagerField(Class<?> clazz) {
try {
Expand All @@ -56,8 +56,8 @@ private static Field getManagerField(Class<?> clazz) {
}
}
} catch (SecurityException e) {
throw (RuntimeException) (new IllegalArgumentException(
"Cannot access component's manager attribute")).initCause(e);
throw (RuntimeException) new IllegalArgumentException(
"Cannot access component's manager attribute").initCause(e);
}
}

Expand Down Expand Up @@ -104,8 +104,8 @@ private ComponentProxy(
this.componentChannel = componentChannel;
initComponentsHandlers(null);
} catch (SecurityException | IllegalAccessException e) {
throw (RuntimeException) (new IllegalArgumentException(
"Cannot access component's manager attribute")).initCause(e);
throw (RuntimeException) new IllegalArgumentException(
"Cannot access component's manager attribute").initCause(e);
}
}

Expand Down Expand Up @@ -142,8 +142,8 @@ private ComponentProxy(
}
}
} catch (SecurityException | IllegalAccessException e) {
throw (RuntimeException) (new IllegalArgumentException(
"Cannot access component's manager attribute")).initCause(e);
throw (RuntimeException) new IllegalArgumentException(
"Cannot access component's manager attribute").initCause(e);
}
return componentProxy;
}
Expand Down Expand Up @@ -177,6 +177,7 @@ public Object defaultCriterion() {
* @see Channel#isEligibleFor(Object)
*/
@Override
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public boolean isEligibleFor(Object value) {
return value.equals(Channel.class)
|| value == defaultCriterion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
* used.
*/
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.DataflowAnomalyAnalysis",
"PMD.AvoidDuplicateLiterals", "PMD.GodClass" })
"PMD.AvoidDuplicateLiterals", "PMD.GodClass",
"PMD.CouplingBetweenObjects" })
public abstract class ComponentVertex implements Manager, Channel {

/** The component's (optional) name. */
Expand Down Expand Up @@ -255,7 +256,8 @@ private void setTree(ComponentTree tree) {
*/
@Override
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.NcssCount",
"PMD.NPathComplexity", "PMD.CognitiveComplexity" })
"PMD.NPathComplexity", "PMD.CognitiveComplexity",
"PMD.ConfusingArgumentToVarargsMethod" })
public <T extends ComponentType> T attach(T child) {
synchronized (this) {
ComponentVertex childNode = componentVertex(child, null);
Expand Down Expand Up @@ -395,15 +397,16 @@ public ComponentType next() {
*/
private static class TreeIterator implements Iterator<ComponentVertex> {

@SuppressWarnings({ "PMD.LooseCoupling", "PMD.ReplaceVectorWithList" })
private final Stack<CurPos> stack = new Stack<>();
private final ComponentTree tree;

/**
* The current position.
*/
private class CurPos {
public ComponentVertex current;
public Iterator<ComponentVertex> childIter;
public final ComponentVertex current;
public final Iterator<ComponentVertex> childIter;

/**
* Instantiates a new current position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public void add(Object obj) {
if (running == 1) { // NOPMD, no, not using a constant for this.
keepAlive = new Thread("GeneratorRegistry") {
@Override
@SuppressWarnings("PMD.EmptyCatchBlock")
public void run() {
try {
while (true) {
Thread.sleep(Long.MAX_VALUE);
sleep(Long.MAX_VALUE);
}
} catch (InterruptedException e) {
// Okay, then stop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ protected HandlerReference(ComponentType component, Method method,
this.method = MethodHandles.lookup().unreflect(method);
this.method = this.method.bindTo(component);
} catch (IllegalAccessException e) {
throw (RuntimeException) (new IllegalArgumentException("Method "
throw (RuntimeException) new IllegalArgumentException("Method "
+ component.getClass().getName()
+ "." + method.getName()
+ " annotated as handler has wrong signature"
+ " or class is not accessible"))
+ " or class is not accessible")
.initCause(e);
}
}
Expand Down Expand Up @@ -246,18 +246,12 @@ public String toString() {
StringBuilder builder = new StringBuilder(50);
builder.append("Handler [");
if (method != null) {
builder.append("method=");
builder.append(methodToString());
builder.append(", ");
builder.append("method=").append(methodToString()).append(", ");
}
if (filter != null) {
builder.append("filter=");
builder.append(filter);
builder.append(", ");
builder.append("filter=").append(filter).append(", ");
}
builder.append("priority=")
.append(priority)
.append(']');
builder.append("priority=").append(priority).append(']');
return builder.toString();
}

Expand Down
Loading

0 comments on commit 8868bbd

Please sign in to comment.