Skip to content

Commit

Permalink
Merge pull request #5 from godfather1103/dev
Browse files Browse the repository at this point in the history
issues/4
  • Loading branch information
godfather1103 authored Aug 23, 2023
2 parents 6f203e9 + 8c81345 commit f9f002f
Show file tree
Hide file tree
Showing 23 changed files with 651 additions and 49 deletions.
8 changes: 8 additions & 0 deletions idea-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
}
Expand Down Expand Up @@ -49,6 +51,12 @@ subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "maven-publish")

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "11"
}
}

java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
Expand Down
2 changes: 1 addition & 1 deletion idea-plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugin_name=Alibaba Java Coding Guidelines(Fix Some Bug)
gradle_jetbrains_version=1.13.3
systemProp.file.encoding=UTF-8
# \u63D2\u4EF6\u7248\u672C
plugin_version=1.2
plugin_version=1.3
# \u4F7F\u7528\u7684p3c-pmd\u7248\u672C
p3c_pmd_version=2.1.1
publishUsername=Jack Chu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package io.github.godfather1103.settings;

import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextArea;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]
* @version 1.0
* @date 创建时间:2023/8/22 09:43
* @since 1.0
*/
public class AppSettingsComponent {

private final JPanel myMainPanel;

private final JBTextArea inData = new JBTextArea(20, 20);

public AppSettingsComponent() {
Border border = BorderFactory.createLineBorder(Color.BLACK);
inData.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
myMainPanel = FormBuilder.createFormBuilder()
.addComponent(new JBLabel("custom namelist.properties"), 1)
.addComponent(inData, 1)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

public JBTextArea getInData() {
return inData;
}

public JPanel getPanel() {
return myMainPanel;
}

public void setInDataText(@NotNull String text) {
inData.setText(text);
}

public String getInDataText() {
return inData.getText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package io.github.godfather1103.settings;

import com.alibaba.p3c.idea.config.P3cConfig;
import com.alibaba.p3c.pmd.lang.java.util.namelist.NameListConfig;
import com.intellij.openapi.options.Configurable;
import io.github.godfather1103.service.BaseNameListServiceExt;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]
* @version 1.0
* @date 创建时间:2023/8/22 09:44
* @since 1.0
*/
public class AppSettingsConfigurable implements Configurable {

private AppSettingsComponent mySettingsComponent;

@Nls(capitalization = Nls.Capitalization.Title)
@Override
public String getDisplayName() {
return "P3C Configure";
}

@Override
public JComponent getPreferredFocusedComponent() {
return mySettingsComponent.getInData();
}

@Nullable
@Override
public JComponent createComponent() {
mySettingsComponent = new AppSettingsComponent();
return mySettingsComponent.getPanel();
}

@Override
public boolean isModified() {
return !P3cConfig.getInstance()
.getCustomNamelistProperties()
.equals(mySettingsComponent.getInDataText());
}

@Override
public void apply() {
var inData = mySettingsComponent.getInDataText();
P3cConfig.getInstance()
.setCustomNamelistProperties(inData);
var service = NameListConfig.NAME_LIST_SERVICE;
if (service instanceof BaseNameListServiceExt) {
BaseNameListServiceExt base = ((BaseNameListServiceExt) service);
if (!base.getOldData().equals(inData)) {
base.resetData(inData, true);
}
}
}

@Override
public void reset() {
mySettingsComponent
.setInDataText(P3cConfig.getInstance().getCustomNamelistProperties());
}

@Override
public void disposeUIResources() {
mySettingsComponent = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.p3c.idea.config

import com.alibaba.smartfox.idea.common.util.getService
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
Expand All @@ -29,6 +30,9 @@ import java.util.*
*/
@State(name = "P3cConfig", storages = [Storage("smartfox_p3c.xml")])
class P3cConfig : PersistentStateComponent<P3cConfig> {

var customNamelistProperties = ""

var astCacheTime = 1000L
var astCacheEnable = true

Expand Down Expand Up @@ -64,5 +68,8 @@ class P3cConfig : PersistentStateComponent<P3cConfig> {
companion object {
val localeEn = Locale.ENGLISH.language!!
val localeZh = Locale.CHINESE.language!!

@JvmStatic
fun getInstance() = P3cConfig::class.java.getService()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,8 @@ import com.intellij.psi.PsiCompiledFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiImportList
import com.intellij.psi.PsiJavaFile
import javassist.CannotCompileException
import javassist.ClassClassPath
import javassist.ClassPool
import javassist.CtField
import javassist.NotFoundException
import net.sourceforge.pmd.Rule
import net.sourceforge.pmd.RuleSet
import net.sourceforge.pmd.RuleSetFactory
import net.sourceforge.pmd.RuleSetNotFoundException
import net.sourceforge.pmd.RuleSets
import javassist.*
import net.sourceforge.pmd.*
import javax.annotation.Generated

/**
Expand Down Expand Up @@ -106,9 +98,9 @@ class AliLocalInspectionToolProvider : InspectionToolProvider {
val virtualFile = file.virtualFile
val index = ProjectRootManager.getInstance(file.project).fileIndex
return index.isInSource(virtualFile)
&& !index.isInTestSourceContent(virtualFile)
&& !index.isInLibraryClasses(virtualFile)
&& !index.isInLibrarySource(virtualFile)
&& !index.isInTestSourceContent(virtualFile)
&& !index.isInLibraryClasses(virtualFile)
&& !index.isInLibrarySource(virtualFile)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.godfather1103.rule

import io.github.godfather1103.service.BaseNameListServiceExt

interface IModifyRuleValue {

fun className(): String {
return javaClass.simpleName
}

fun needModifyOnInit(): Boolean = false

fun modifyValue(base: BaseNameListServiceExt, key: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.godfather1103.rule.ext

import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule
import com.alibaba.p3c.pmd.lang.java.rule.naming.LowerCamelCaseVariableNamingRule
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId
import java.util.regex.Pattern

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]
* @date 创建时间:2023/8/23 13:57
* @version 1.0
* @since 1.0
*/
class LowerCamelCaseVariableNamingRuleExt : AbstractAliRule() {

val rule by lazy { LowerCamelCaseVariableNamingRule() }

override fun visit(node: ASTVariableDeclaratorId, data: Any?): Any? {
if (!(pattern.matcher(node.image)).matches()) {
return rule.visit(node, data)
}
return super.visit(node, data)
}

override fun visit(node: ASTMethodDeclarator, data: Any?): Any? {
if (!(pattern.matcher(node.image)).matches()) {
return rule.visit(node, data)
}
return super.visit(node, data)
}

override fun visit(node: ASTAnnotationTypeDeclaration, data: Any?): Any? {
return null
}

companion object {
var pattern: Pattern =
Pattern.compile("^[a-z][a-z0-9]*([A-Z][a-z0-9]+)*(DO|DTO|VO|DAO|BO|DOList|DTOList|VOList|DAOList|BOList|X|Y|Z|UDF|UDAF|[A-Z])?$")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.godfather1103.rule.impl

import io.github.godfather1103.rule.IModifyRuleValue
import io.github.godfather1103.service.BaseNameListServiceExt

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]<BR>
* 创建时间:2023-08-22 22:58
* @version 1.0
* @since 1.0
*/
class ClassNamingShouldBeCamelRule : IModifyRuleValue {
override fun modifyValue(base: BaseNameListServiceExt, key: String) {
when (key) {
"CLASS_NAMING_WHITE_LIST" ->
com.alibaba.p3c.pmd.lang.java.rule.naming.ClassNamingShouldBeCamelRule::class.java
.declaredFields.find { it.name == key }?.apply {
isAccessible = true
val set = get(null) as MutableList<String>
set.clear()
base.getNameList(className(), key)
.forEach { set.add(it) }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.godfather1103.rule.impl

import io.github.godfather1103.rule.IModifyRuleValue
import io.github.godfather1103.service.BaseNameListServiceExt

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]<BR>
* 创建时间:2023-08-22 22:58
* @version 1.0
* @since 1.0
*/
class CollectionInitShouldAssignCapacityRule : IModifyRuleValue {
override fun modifyValue(base: BaseNameListServiceExt, key: String) {
when (key) {
"COLLECTION_TYPE" ->
com.alibaba.p3c.pmd.lang.java.rule.set.CollectionInitShouldAssignCapacityRule::class.java
.declaredFields.find { it.name == "COLLECTION_LIST" }?.apply {
isAccessible = true
val set = get(null) as MutableList<String>
set.clear()
base.getNameList(className(), key)
.forEach { set.add(it) }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.godfather1103.rule.impl

import io.github.godfather1103.rule.IModifyRuleValue
import io.github.godfather1103.service.BaseNameListServiceExt

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
*
* @author 作者: Jack Chu E-mail: [email protected]
* @date 创建时间:2023/8/22 19:00
* @version 1.0
* @since 1.0
*/
class ConstantFieldShouldBeUpperCaseRule : IModifyRuleValue {
override fun modifyValue(base: BaseNameListServiceExt, key: String) {
when (key) {
"LOG_VARIABLE_TYPE_SET", "WHITE_LIST" ->
com.alibaba.p3c.pmd.lang.java.rule.naming.ConstantFieldShouldBeUpperCaseRule::class.java
.declaredFields.find { it.name == key }?.apply {
isAccessible = true
val set = get(null) as HashSet<String>
set.clear()
base.getNameList(className(), key)
.forEach { set.add(it) }
}
}
}
}
Loading

0 comments on commit f9f002f

Please sign in to comment.