Skip to content

Commit 1f02159

Browse files
committed
Make main class name validation much more rigorous (Fixes #226)
1 parent 45efb5c commit 1f02159

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/main/kotlin/com/demonwav/mcdev/creator/MinecraftModuleBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MinecraftModuleBuilder : JavaModuleBuilder() {
5555
creator.root = root
5656
creator.module = modifiableRootModel.module
5757

58-
val r = DumbAwareRunnable { creator.create() }
58+
val r = DumbAwareRunnable(creator::create)
5959

6060
if (project.isDisposed) {
6161
return

src/main/kotlin/com/demonwav/mcdev/creator/MinecraftModuleWizardStep.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package com.demonwav.mcdev.creator
1212

1313
import com.demonwav.mcdev.exception.BadListSetupException
1414
import com.demonwav.mcdev.exception.EmptyInputSetupException
15+
import com.demonwav.mcdev.exception.InvalidMainClassNameException
1516
import com.demonwav.mcdev.exception.SetupException
1617
import com.intellij.ide.util.projectWizard.ModuleWizardStep
1718
import com.intellij.openapi.ui.MessageType
@@ -37,9 +38,26 @@ abstract class MinecraftModuleWizardStep : ModuleWizardStep() {
3738
throw EmptyInputSetupException(pluginVersionField)
3839
}
3940

40-
if (mainClassField.text.trim { it <= ' ' }.isEmpty()) {
41+
if (mainClassField.text.trim { it <= ' ' }.isEmpty()) { // empty
4142
throw EmptyInputSetupException(mainClassField)
4243
}
44+
if (!mainClassField.text.contains('.')) { // default package
45+
throw InvalidMainClassNameException(mainClassField)
46+
}
47+
if (mainClassField.text.contains("\\s+".toRegex())) { // whitespace
48+
throw InvalidMainClassNameException(mainClassField)
49+
}
50+
if (mainClassField.text.first().isJavaIdentifierStart() && // invalid character
51+
mainClassField.text.asSequence().drop(1).all { it.isJavaIdentifierPart() }) {
52+
throw InvalidMainClassNameException(mainClassField)
53+
}
54+
if (mainClassField.text.first() == '.') { // idk why this doesn't fail in the above check, but w/e
55+
throw InvalidMainClassNameException(mainClassField)
56+
}
57+
if (mainClassField.text.split('.').any { keywords.contains(it) }) { // keyword identifier
58+
throw InvalidMainClassNameException(mainClassField)
59+
}
60+
4361
if (!authorsField.text.matches(pattern)) {
4462
throw BadListSetupException(authorsField)
4563
}
@@ -60,5 +78,11 @@ abstract class MinecraftModuleWizardStep : ModuleWizardStep() {
6078

6179
companion object {
6280
val pattern = "(\\s*(\\w+)\\s*(,\\s*\\w+\\s*)*,?|\\[?\\s*(\\w+)\\s*(,\\s*\\w+\\s*)*])?".toRegex()
81+
val keywords = setOf(
82+
"abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", "if",
83+
"private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case",
84+
"enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
85+
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while"
86+
)
6387
}
6488
}

src/main/kotlin/com/demonwav/mcdev/exception/SetupException.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class EmptyInputSetupException(j: JComponent) : SetupException(j) {
3232
get() = "<html>Please fill in all required fields</html>"
3333
}
3434

35+
class InvalidMainClassNameException(j: JComponent) : SetupException(j) {
36+
override val error: String
37+
get() = "<html>Main Class Name must be a valid Java identifier and cannot be the default package</html>"
38+
}
39+
3540
class OtherSetupException(val msg: String, j: JComponent) : SetupException(j) {
3641
override val error: String
3742
get() = "<html>$msg</html>"

0 commit comments

Comments
 (0)