From b9e6de627b4ece94fddc3af306793d8682e77d3b Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Sat, 30 Dec 2023 22:47:09 +0800 Subject: [PATCH] todo --- error_templates/java/class_cast_exception.go | 20 ++++++++++++++++ error_templates/java/expected_error.go | 15 ++++++++++++ .../java/illegal_start_of_type_error.go | 17 +++++++++++++ error_templates/java/java.go | 3 +++ .../test_files/class_cast_exception/Main.java | 6 +++++ .../test_files/class_cast_exception/test.txt | 23 ++++++++++++++++++ .../illegal_start_of_type_error/Rand.java | 7 ++++++ .../illegal_start_of_type_error/test.txt | 24 +++++++++++++++++++ 8 files changed, 115 insertions(+) create mode 100644 error_templates/java/class_cast_exception.go create mode 100644 error_templates/java/expected_error.go create mode 100644 error_templates/java/illegal_start_of_type_error.go create mode 100644 error_templates/java/test_files/class_cast_exception/Main.java create mode 100644 error_templates/java/test_files/class_cast_exception/test.txt create mode 100644 error_templates/java/test_files/illegal_start_of_type_error/Rand.java create mode 100644 error_templates/java/test_files/illegal_start_of_type_error/test.txt diff --git a/error_templates/java/class_cast_exception.go b/error_templates/java/class_cast_exception.go new file mode 100644 index 0000000..aac6800 --- /dev/null +++ b/error_templates/java/class_cast_exception.go @@ -0,0 +1,20 @@ +package java + +import lib "github.com/nedpals/errgoengine" + +var ClassCastException = lib.ErrorTemplate{ + Name: "ClassCastException", + Pattern: runtimeErrorPattern("java.lang.ClassCastException", `class (?P\S+) cannot be cast to class (?P\S+)`), + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { + }, + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { + gen.Add("This error occurs when there is an attempt to cast an object to a type that it is not compatible with.") + }, + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { + gen.Add("Use the correct type for casting", func(s *lib.BugFixSuggestion) { + s.AddStep( + "Ensure that the casting is done to the correct type. In this case, you should cast to `%s` instead of `%s`.", + "String", cd.Variables["targetClassName"]) + }) + }, +} diff --git a/error_templates/java/expected_error.go b/error_templates/java/expected_error.go new file mode 100644 index 0000000..ea3ee82 --- /dev/null +++ b/error_templates/java/expected_error.go @@ -0,0 +1,15 @@ +package java + +import lib "github.com/nedpals/errgoengine" + +var ExpectedError = lib.ErrorTemplate{ + Name: "ExpectedError", + Pattern: comptimeErrorPattern(`(?P.+) expected`), + StackTracePattern: comptimeStackTracePattern, + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { + }, + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { + }, + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { + }, +} diff --git a/error_templates/java/illegal_start_of_type_error.go b/error_templates/java/illegal_start_of_type_error.go new file mode 100644 index 0000000..8bf8e99 --- /dev/null +++ b/error_templates/java/illegal_start_of_type_error.go @@ -0,0 +1,17 @@ +package java + +import lib "github.com/nedpals/errgoengine" + +var IllegalStartOfTypeError = lib.ErrorTemplate{ + Name: "IllegalStartOfTypeError", + Pattern: comptimeErrorPattern(`illegal start of type`), + StackTracePattern: comptimeStackTracePattern, + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { + }, + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { + gen.Add("This error occurs when there is an illegal start of a type, typically due to a misplaced return statement.") + }, + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { + gen.Add("Test", func(s *lib.BugFixSuggestion) {}) + }, +} diff --git a/error_templates/java/java.go b/error_templates/java/java.go index 0ed35bd..e83a743 100644 --- a/error_templates/java/java.go +++ b/error_templates/java/java.go @@ -14,6 +14,7 @@ func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates) { errorTemplates.MustAdd(java.Language, ArrayIndexOutOfBoundsException) errorTemplates.MustAdd(java.Language, ArithmeticException) errorTemplates.MustAdd(java.Language, NegativeArraySizeException) + errorTemplates.MustAdd(java.Language, ClassCastException) // Compile time errorTemplates.MustAdd(java.Language, PublicClassFilenameMismatchError) @@ -36,6 +37,8 @@ func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates) { errorTemplates.MustAdd(java.Language, CannotBeAppliedError) errorTemplates.MustAdd(java.Language, BracketMismatchError) errorTemplates.MustAdd(java.Language, InvalidMethodDeclarationError) + errorTemplates.MustAdd(java.Language, IllegalStartOfTypeError) + errorTemplates.MustAdd(java.Language, ExpectedError) } func runtimeErrorPattern(errorName string, pattern string) string { diff --git a/error_templates/java/test_files/class_cast_exception/Main.java b/error_templates/java/test_files/class_cast_exception/Main.java new file mode 100644 index 0000000..46fd7d9 --- /dev/null +++ b/error_templates/java/test_files/class_cast_exception/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + Object obj = "Hello, World!"; + Integer number = (Integer) obj; + } +} diff --git a/error_templates/java/test_files/class_cast_exception/test.txt b/error_templates/java/test_files/class_cast_exception/test.txt new file mode 100644 index 0000000..13f58d6 --- /dev/null +++ b/error_templates/java/test_files/class_cast_exception/test.txt @@ -0,0 +1,23 @@ +template: "Java.ClassCastException" +--- +Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap') + at Main.main(Main.java:5) +=== +template: "Java.ClassCastException" +--- +# ClassCastException +This error occurs when there is an attempt to cast an object to a type that it is not compatible with. +``` + Integer number = (Integer) obj; + } + ^ +} + +``` +## Steps to fix +### Use the correct type for casting +Ensure that the casting is done to the correct type. In this case, you should cast to `String` instead of `Integer`. +```diff +- Integer number = (Integer) obj; ++ String text = (String) obj; // Change the type to String +``` diff --git a/error_templates/java/test_files/illegal_start_of_type_error/Rand.java b/error_templates/java/test_files/illegal_start_of_type_error/Rand.java new file mode 100644 index 0000000..5987597 --- /dev/null +++ b/error_templates/java/test_files/illegal_start_of_type_error/Rand.java @@ -0,0 +1,7 @@ +public class Rand { + public static Rand searchCount() { + Rand countA = new Rand() ; + } + return countA ; + } +} diff --git a/error_templates/java/test_files/illegal_start_of_type_error/test.txt b/error_templates/java/test_files/illegal_start_of_type_error/test.txt new file mode 100644 index 0000000..dd96b9a --- /dev/null +++ b/error_templates/java/test_files/illegal_start_of_type_error/test.txt @@ -0,0 +1,24 @@ +template: "Java.IllegalStartOfTypeError" +--- +Rand.java:5: error: illegal start of type + return countA ; + ^ +Rand.java:5: error: expected + return countA ; + ^ +Rand.java:7: error: class, interface, enum, or record expected +} +^ +3 errors +=== +template: "Java.IllegalStartOfTypeError" +--- +# IllegalStartOfTypeError +``` + } + return countA ; + ^^^^^^^^^^^^^^^ + } +} +``` +This error occurs when there is an illegal start of a type, typically due to a misplaced return statement.