Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3160 Enable PMD for eo-runime #3171

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

c71n93
Copy link
Member

@c71n93 c71n93 commented May 6, 2024

Closes #3160


PR-Codex overview

This PR focuses on code cleanup and improvements across multiple classes, including the addition of @SuppressWarnings annotations and constructor parameter updates.

Detailed summary

  • Added @SuppressWarnings annotations to classes for code quality improvements.
  • Updated constructor parameters in various classes.
  • Modified exception handling in SafeFunc and ExAbstract.
  • Refactored PhPackage class.
  • Updated byte array initialization in UniverseDefaultTest.

The following files were skipped due to too many changes: eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java, eo-runtime/src/main/java/org/eolang/PhDefault.java, eo-runtime/src/main/java/org/eolang/BytesOf.java, eo-runtime/src/main/java/org/eolang/PhTraced.java, eo-runtime/src/main/java/org/eolang/Data.java, eo-runtime/src/test/java/org/eolang/DataizedTest.java, eo-runtime/src/test/java/org/eolang/SnippetTestCase.java, eo-runtime/src/test/java/org/eolang/PhDefaultTest.java

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

@c71n93
Copy link
Member Author

c71n93 commented May 6, 2024

@maxonfjvipon could you check this one, please?

@c71n93
Copy link
Member Author

c71n93 commented May 6, 2024

@maxonfjvipon oops, it looks like something went wrong with PMD check on windows. Looks like internal PMD error. Have you ever come across something like this?

2024-05-06T18:35:20.8319792Z [INFO] PMD: D:\a\eo\eo\eo-runtime\src\main\java\org\eolang\AtComposite.java[unknown]: PMDException: Error while processing D:\a\eo\eo\eo-runtime\src\main\java\org\eolang\AtComposite.java: net.sourceforge.pmd.PMDException: Error while processing D:\a\eo\eo\eo-runtime\src\main\java\org\eolang\AtComposite.java
2024-05-06T18:35:20.8325885Z 	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:128)
2024-05-06T18:35:20.8327642Z 	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:100)
2024-05-06T18:35:20.8329641Z 	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:62)
2024-05-06T18:35:20.8333094Z 	at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:89)
2024-05-06T18:35:20.8336483Z 	at net.sourceforge.pmd.processor.MonoThreadProcessor.runAnalysis(MonoThreadProcessor.java:32)
2024-05-06T18:35:20.8341693Z 	at net.sourceforge.pmd.processor.AbstractPMDProcessor.processFiles(AbstractPMDProcessor.java:143)
2024-05-06T18:35:20.8344982Z 	at net.sourceforge.pmd.processor.AbstractPMDProcessor.processFiles(AbstractPMDProcessor.java:123)
2024-05-06T18:35:20.8347679Z 	at net.sourceforge.pmd.PMD.processFiles(PMD.java:322)
2024-05-06T18:35:20.8349328Z 	at com.qulice.pmd.SourceValidator.validateOne(SourceValidator.java:130)
2024-05-06T18:35:20.8351728Z 	at com.qulice.pmd.SourceValidator.validate(SourceValidator.java:105)
2024-05-06T18:35:20.8353447Z 	at com.qulice.pmd.PmdValidator.validate(PmdValidator.java:67)
2024-05-06T18:35:20.8355075Z 	at com.qulice.maven.CheckMojo$ValidatorCallable.call(CheckMojo.java:237)
2024-05-06T18:35:20.8357390Z 	at com.qulice.maven.CheckMojo$ValidatorCallable.call(CheckMojo.java:205)
2024-05-06T18:35:20.8359571Z 	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
2024-05-06T18:35:20.8361442Z 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
2024-05-06T18:35:20.8364028Z 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
2024-05-06T18:35:20.8365987Z 	at java.base/java.lang.Thread.run(Thread.java:829)
2024-05-06T18:35:20.8369028Z Caused by: net.sourceforge.pmd.lang.ast.TokenMgrError: Lexical error at line 61, column 22.  Encountered: "\u2020" (8224), after : "" (in lexical state 0)
2024-05-06T18:35:20.8373051Z 	at net.sourceforge.pmd.lang.java.ast.JavaParserTokenManager.getNextToken(JavaParserTokenManager.java:2534)
2024-05-06T18:35:20.8375895Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.jj_consume_token(JavaParser.java:14281)
2024-05-06T18:35:20.8379388Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.PrimarySuffix(JavaParser.java:5270)
2024-05-06T18:35:20.8382917Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.PrimaryExpression(JavaParser.java:4680)
2024-05-06T18:35:20.8385610Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.PostfixExpression(JavaParser.java:4494)
2024-05-06T18:35:20.8388757Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.UnaryExpressionNotPlusMinus(JavaParser.java:4392)
2024-05-06T18:35:20.8391888Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.UnaryExpression(JavaParser.java:4269)
2024-05-06T18:35:20.8394940Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.MultiplicativeExpression(JavaParser.java:4184)
2024-05-06T18:35:20.8397995Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.AdditiveExpression(JavaParser.java:4131)
2024-05-06T18:35:20.8400882Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ShiftExpression(JavaParser.java:4074)
2024-05-06T18:35:20.8403983Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.RelationalExpression(JavaParser.java:4013)
2024-05-06T18:35:20.8406696Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.InstanceOfExpression(JavaParser.java:3941)
2024-05-06T18:35:20.8409813Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.EqualityExpression(JavaParser.java:3686)
2024-05-06T18:35:20.8412868Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.AndExpression(JavaParser.java:3646)
2024-05-06T18:35:20.8415768Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ExclusiveOrExpression(JavaParser.java:3606)
2024-05-06T18:35:20.8418568Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.InclusiveOrExpression(JavaParser.java:3566)
2024-05-06T18:35:20.8421940Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ConditionalAndExpression(JavaParser.java:3526)
2024-05-06T18:35:20.8424760Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ConditionalOrExpression(JavaParser.java:3486)
2024-05-06T18:35:20.8427832Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ConditionalExpression(JavaParser.java:3448)
2024-05-06T18:35:20.8430697Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.Expression(JavaParser.java:3307)
2024-05-06T18:35:20.8435868Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ReturnStatement(JavaParser.java:6990)
2024-05-06T18:35:20.8446583Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.Statement(JavaParser.java:5799)
2024-05-06T18:35:20.8450644Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.BlockStatement(JavaParser.java:5971)
2024-05-06T18:35:20.8453924Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.Block(JavaParser.java:5888)
2024-05-06T18:35:20.8458729Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.MethodDeclaration(JavaParser.java:2201)
2024-05-06T18:35:20.8462293Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceBodyDeclaration(JavaParser.java:1855)
2024-05-06T18:35:20.8465229Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceBody(JavaParser.java:1808)
2024-05-06T18:35:20.8468312Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceDeclaration(JavaParser.java:936)
2024-05-06T18:35:20.8471294Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.TypeDeclaration(JavaParser.java:838)
2024-05-06T18:35:20.8474144Z 	at net.sourceforge.pmd.lang.java.ast.JavaParser.CompilationUnit(JavaParser.java:558)
2024-05-06T18:35:20.8477198Z 	at net.sourceforge.pmd.lang.java.AbstractJavaParser.parse(AbstractJavaParser.java:62)
2024-05-06T18:35:20.8479692Z 	at net.sourceforge.pmd.lang.AbstractParser.doParse(AbstractParser.java:45)
2024-05-06T18:35:20.8484630Z 	at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:136)
2024-05-06T18:35:20.8487198Z 	at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:200)
2024-05-06T18:35:20.8489778Z 	at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:118)
2024-05-06T18:35:20.8491860Z 	... 16 more
2024-05-06T18:35:20.8492229Z  (ProcessingError)

@maxonfjvipon
Copy link
Member

@c71n93 no, I use macos)

@c71n93
Copy link
Member Author

c71n93 commented May 6, 2024

@maxonfjvipon me too. But unfortunately we have windows CI :(

@maxonfjvipon
Copy link
Member

@c71n93 did you try to find the solution on stackoverflow?

@c71n93
Copy link
Member Author

c71n93 commented May 6, 2024

@maxonfjvipon not yet. But according to #3160 (comment) there was some problems with PMD and eo-runtime earlier. I assume that this (#3171 (comment)) is the problem.

@c71n93 c71n93 force-pushed the 3160-enable-pmd-in-eo-runtime branch from 7644b64 to ff1c2ed Compare May 13, 2024 08:21
@c71n93
Copy link
Member Author

c71n93 commented May 13, 2024

@maxonfjvipon could you check this one, please?

Copy link
Member

@maxonfjvipon maxonfjvipon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 that's awesome! Thanks

@c71n93
Copy link
Member Author

c71n93 commented May 13, 2024

@yegor256 could you check this one, please?

@c71n93
Copy link
Member Author

c71n93 commented May 14, 2024

@yegor256 could you merge this one, please?

@@ -35,6 +35,7 @@
* @since 0.22
*/
@Versionized
@SuppressWarnings({"PMD.TooManyMethods", "PMD.ConstructorShouldDoInitialization"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 I believe, you can easily fix this error (that PMD complains about), instead of suppressing it

public final class PhTraced implements Phi {

/**
* Name of property that responsible for keeping max depth.
*/
@SuppressWarnings("PMD.LongVariable")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 why can't you just make the name shorter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 Because this variable means "maximum cage recursion depth". As for me the shortest name for this variable is MAX_CAGE_RECURSION. However max length of variable is two words. Any two-word options don't seem informative enough to me (MAX_RECURSION, MAX_CAGE, CAGE_RECURSION).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 how about RECURSION_THRESHOLD? The name of the class already provides a lot of semantic: PhTraced.RECURSION_THRESHOLD -- sounds good.

@@ -58,6 +60,7 @@ public final class PhTraced implements Phi {
/**
* Locator of encaged object.
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
private final Integer locator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 same here, just rename the variable

@@ -50,6 +50,7 @@ public final class PhWrite extends PhDefault implements Atom {
* @param attr Attribute name
* @param ret Return value function
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 maybe we can rewrite the code instead of suppressing the warning?

// @checkstyle IllegalCatchCheck (3 line)
} catch (final RuntimeException ex) {
throw ex;
} catch (final Throwable ex) {
} catch (final Exception ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 this seems to be a pretty dangerous change. Throwable is of a higher level than Exception - we must catch it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 I made it because of PMD.AvoidCatchingThrowable violation. It seemed reasonable to me. As far as I understand, it is pointless to catch Error, since it indicates irrecoverable situation. According to https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/lang/Error.html "An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 we catch RuntimeException two lines above, so here we can catch either Exception either Throwable - it would be the same.

@@ -44,6 +44,7 @@ public final class VerboseBytesAsString implements Supplier<String> {
* Ctor.
* @param data Data.
*/
@SuppressWarnings("PMD.ArrayIsStoredDirectly")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 this is a reasonable complaint of PMD, I suggest we fix this

@@ -34,6 +34,7 @@
*
* @since 0.16
*/
@SuppressWarnings("PMD.JUnit5TestShouldBePackagePrivate")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 why not just fixing this error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 Temporarily this class should be public because it stores

public static final String TO_ADD_MESSAGE = "TO ADD ASSERTION MESSAGE";

There is todo about it: https://github.com/c71n93/eo/blob/88b3b4bf5e7cf54efd3a116aac7a41cc35ce54ef/eo-runtime/src/test/java/org/eolang/AtCompositeTest.java#L42. This decision was made here #3113 (review)

@@ -95,6 +96,7 @@ private static class Rnd extends PhDefault {
/**
* Ctor.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 this is also easily fixable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 As I understand it is common practice to use PhDefault::add in constructor of classes that extends PhDefault and created for tests. I don't think that we need to fix it. @maxonfjvipon what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 it's not a brightest design so we should fix it anyway. But it's not a part of this task. It should be done in other PR

@yegor256
Copy link
Member

@c71n93 most of the suppressions that you introduced are related to easily fixable code mistakes. I suggest we try to fix them instead of suppressing.

public T get() {
try {
return this.origin.call();
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new ExInterrupted();
} catch (final ExAbstract ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 there was a reason why I added this ExAbstract catch block but I forgot to document it. There are a lot of places in the code where we throw some exceptions that extends ExAbstract. But today ExAbstract extends RuntimeException, tomorrow it may be just Exception. So the main purpose of this block is to explicitly show that this method catches ExAbstract exception which is important for such objects like PhSafe or AtSafe. Because they convert it to EOerror.ExError exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxonfjvipon do we need this class? It never used now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 we don't need it

Copy link
Member Author

@c71n93 c71n93 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 @maxonfjvipon maybe we can move this class to eo-runtime/src/test/java/EOorg/EOeolang/? (it uses only there)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 I believe it should stay in org.eolang since it's kind of part of common runtime object which are used for building the concrete ones

@c71n93
Copy link
Member Author

c71n93 commented May 16, 2024

@maxonfjvipon @yegor256 I resolved all I could.

@c71n93
Copy link
Member Author

c71n93 commented May 21, 2024

@yegor256 @maxonfjvipon could you check this one again, please?

@c71n93
Copy link
Member Author

c71n93 commented May 22, 2024

@yegor256 @maxonfjvipon could you check this one, please? I think it would be nice to enable pmd for eo-runtime as early as possible.

@@ -58,7 +60,7 @@ public final class PhTraced implements Phi {
/**
* Locator of encaged object.
*/
private final Integer locator;
private final Integer locatr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 what was wrong with locator name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxonfjvipon it repeats the method name (prohibited by PMD)

Copy link
Member

@maxonfjvipon maxonfjvipon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

@c71n93
Copy link
Member Author

c71n93 commented May 23, 2024

@yegor256 could you check this one, please?

@yegor256
Copy link
Member

@c71n93 one small comment above and we are good to go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PMD check disabled for entire eo-runtime
3 participants