JAVA의 naming convention중 class이름의 첫글자는 대문자 여야한다.
이것을 체크해 첫글자가 소문자면 에러를 발생시키는 간단한 플러그인을 만들고 테스트해보는 과정을 진행해보았다.
소스코드 >> TestInspection.java
package com.goodong.namer;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
public class MyPsiVisitor extends JavaElementVisitor {
private final ProblemsHolder holder;
public MyPsiVisitor(ProblemsHolder holder) {
this.holder = holder;
}
@Override
public void visitClass(PsiClass clazz) {
super.visitClass(clazz);
char firstCharacter = clazz.getName().charAt(0);
if(Character.isLowerCase(firstCharacter)){
holder.registerProblem(clazz, "lowercase", ProblemHighlightType.ERROR);
}
}
}
첫글자가 lowercase인경우 registerProblem을통해 ERROR를 발생시킨다.
소스코드 >> MyPsiVisitor.java
package com.goodong.namer;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class TestInspection extends LocalInspectionTool {
@Override
public @Nls(capitalization = Nls.Capitalization.Sentence) @NotNull String getGroupDisplayName() {
return "hello";
}
@Override
public @Nls(capitalization = Nls.Capitalization.Sentence) @NotNull String getDisplayName() {
return "hello";
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new MyPsiVisitor(holder);
}
}
buildVisitor메소드를통해 아까 만들었던 MyPsiVisitor를 반환하게끔한다.
해당 플러그인은 Inspection (소스코드 검사) 유형의 플러그인이기때문에 plugin.xml에 inspection을 따로 등록해야한다.
>> resources/META-INF/plugin.xml
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<!-- Unique identifier of the plugin. It should be FQN. It cannot be changed between the plugin versions. -->
<id>com.goodong.namer</id>
<!-- Public plugin name should be written in Title Case.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-name -->
<name>Namer</name>
<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
<vendor email="kjs990114@naver.com" url="https://www.yourcompany.com">goodong2</vendor>
<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
<description>This plugin performs syntax checks on variable/method/field names in Java source code according to Java naming conventions.</description>
<!-- Product and plugin compatibility requirements.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.java</depends>
<depends>com.intellij.modules.java</depends>
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<localInspection language="JAVA" implementationClass="com.goodong.namer.TestInspection"/>
</extensions>
</idea-plugin>
<localInspection/> 블록으로 아까 만들었던 Inspection을 등록해주어야 플러그인이 정상적으로 등록된다.
----실행결과----
첫글자를 소문자로한경우 에러가 뜬다. 에러 description은 lowercase로 간단하게 등록했다.
하지만 첫글자를 대문자로한 class Abc를 만들경우 에러가 발생하지않는다.
이렇게해서 간단한 syntax check를 하는 플러그인을 만들어보았고,
앞으로의 목표는
https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html
Code Conventions for the Java Programming Language: 9. Naming Conventions
Packages The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified
www.oracle.com
Oracle에서 정한 자바의 변수/메소드의 coding convention을 체크하는 쪽으로 확장시킬것이고.
더 나아가서 사용자가 지정한 rule에 기반한 syntax check를 하는 쪽으로도 확장시킬것이다
https://github.com/kjs990114/namer
GitHub - kjs990114/namer
Contribute to kjs990114/namer development by creating an account on GitHub.
github.com
'Projects > plugin' 카테고리의 다른 글
[Kson] IntellJ 플러그인 - .json to .java (1) | 2024.06.07 |
---|---|
[namer - 1] IntellJ 플러그인 개발일지 - PSI(Program Structure Interface)를 이용한 코드 검사 플러그인 개발 (0) | 2024.02.08 |