alibaba / p3c

Alibaba Java Coding Guidelines pmd implements and IDE plugin
https://github.com/alibaba/p3c/wiki
Apache License 2.0
30.34k stars 8.06k forks source link

How to add a new rule #986

Open Rapter1990 opened 11 months ago

Rapter1990 commented 11 months ago

I tried to use your plugin in my Intellij IDEA 2023.2.1 Ultimate Edition and it feels very comfortable to analyze the code and suggest some major and minor ideas which help you revise the code.

What I just want to ask is to learn how to add a new rule in the plugin.

Which documentation do I follow that? Can you guide me?

I hope you can help me.

I'm waiting for response as early as possible.

godfather1103 commented 11 months ago

Can you debug to see if relevant content has been injected

Rapter1990 commented 11 months ago

@godfather1103 I can run CamelCaseMethodNameRuleTest without any error. After I get zip file from repo as plugin, I test it in Intellij Idea 2018 but it couldn't detect the rule and rule can appeared when I click "Analyze code through Ali Baba Java Guideness Plugin". How can I fix it?

Rapter1990 commented 11 months ago

@godfather1103 I downloaded your repo but I got this issue when I run this command gradle clean :p3c-idea:buildPlugin --warning-mode all

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'idea-plugin'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3.
     Required by:
         project :
      > No matching variant of org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3 was found. The consumer was configured to find a runtime of a library compatible with Ja
va 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5.1' but:
          - Variant 'apiElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3 declares a library, packaged as a jar, and its dependencies declared exter
nally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8  
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
          - Variant 'javadocElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3 declares a runtime of a component, and its dependencies declared exter
nally:
          - Variant 'runtimeElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3 declares a runtime of a library, packaged as a jar, and its dependenci
es declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
          - Variant 'sourcesElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3 declares a runtime of a component, and its dependencies declared exter
nally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
          - Variant 'testFixturesApiElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin-test-fixtures:1.13.3 declares a library, packaged as a jar, and its d
ependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8  
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
          - Variant 'testFixturesRuntimeElements' capability org.jetbrains.intellij.plugins:gradle-intellij-plugin-test-fixtures:1.13.3 declares a runtime of a library, packaged as
 a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

How can I fix it?

godfather1103 commented 11 months ago

use jdk 17

Rapter1990 commented 11 months ago

@godfather1103 I define jdk 17 for project structure, idea-plugin and p3c-pmd under gradle but nothing changed. I still got the same issue. How can I fix it as I'm stuck there. As you can see, Could not resolve org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3.

godfather1103 commented 11 months ago

@Rapter1990 What about the code for this class "demo.CamelCaseMethodNameRule"

Rapter1990 commented 11 months ago

@godfather1103 I forgot to add it. You can see the post above to show all information about custom rule. I have also another problem which I mentioned before. I hope you can help me.

godfather1103 commented 11 months ago

https://plugins.jetbrains.com/docs/intellij/welcome.html

godfather1103 commented 11 months ago

Could not resolve org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.13.3. @Rapter1990

  1. check jdk Version Have you change jdk between these two places? Settings -> Build -> Gradle -> Gradle Jvm Project Structure -> Project -> SDK

2.Modify buildscript.repositories Order

1

Rapter1990 commented 11 months ago

@godfather1103 Here is what I've done after your response.

1. Settings -> Build -> Gradle -> Gradle Jvm -> Oracle Open JDK version 11.0.20 Project Structure -> Project -> SDK -> Oracle Open JDK version 11.0.20

In the file named gradle-wrapper.properties , here is the gradle version shown below

`distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip`

2. Move gradlePluginPortal() to mavenLocal() above.

It works now. Thank you for your guideness. I can reach out you again if I have any problem.

Rapter1990 commented 10 months ago

@godfather1103 How can I find rule example like xpath example such as FinallyStatement, ReturnStatement, ContinueStatement? Can you share its link if you have? Let me know.

godfather1103 commented 10 months ago

https://docs.pmd-code.org/pmd-doc-7.0.0-rc4/pmd_userdocs_extending_your_first_rule.html

Message ID: @.***>

Rapter1990 commented 10 months ago

@godfather1103 I found this link ( https://josephallen.github.io/PMD-New-Site/writing_pmd_rules.html ) I try to find their usage like FinallyStatement//ContinueStatement .

Do you have any idea about it?

godfather1103 commented 10 months ago

I don't know.

Message ID: @.***>

Rapter1990 commented 10 months ago

@godfather1103 Which contributor has any idea about it?

Rapter1990 commented 10 months ago

@godfather1103 I try to find more examples of writing custom rule. Where do I find more them?

Rapter1990 commented 10 months ago

@godfather1103 Hi, I want to ask some questions to you?

1) How can I run the app locally? I try this approach

2) I added the rule but it cannot detect.

Can you help me?

godfather1103 commented 10 months ago

Run in debug mode to confirm if the rules have been injected

godfather1103 commented 10 months ago

Follow the steps in the video I sent and check in sequence