-
Notifications
You must be signed in to change notification settings - Fork 0
new: first implementation #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b92d53d
new: first implementation
sttk df6257a
fix: changed to use serialization-config.json because jdk<23 don't su…
sttk 77aa269
fix: typo
sttk 2520164
fix: modified formats of serialization-config.json
sttk c0cfc47
fix: modified the expected value of printStackTrace in windows
sttk 065b39a
fix: modified the expected value of printStackTrace in windows (2)
sttk ea07f16
fix: modified the expected value of printStackTrace in windows (3)
sttk 2b55e62
fix: modified the expected value of printStackTrace in windows (4)
sttk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| /* | ||
| * Exc class. | ||
| * Copyright (C) 2025 Takayuki Sato. All Rights Reserved. | ||
| */ | ||
| package com.github.sttk.errs; | ||
|
|
||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.io.Serializable; | ||
| import java.io.IOException; | ||
| import java.io.NotSerializableException; | ||
| import java.io.InvalidObjectException; | ||
|
|
||
| /** | ||
| * Is the exception class with a reason. | ||
| * <p> | ||
| * This class has a record field which indicates a reason for this exception. The class name of the reason record | ||
| * represents the type of reason, and the fields of the reason record hold the situation where the exception occurred. | ||
| * <p> | ||
| * Optionally, this exception class can notify its instance creation to pre-registered exception handlers. | ||
| * <p> | ||
| * The example code of creating and throwing an excepton is as follows: | ||
| * | ||
| * <pre>{@code | ||
| * public record FailToDoSomething(String name, int value) { | ||
| * } | ||
| * | ||
| * try { | ||
| * throw new Exc(new FailToDoSomething("abc", 123)); | ||
| * } catch (Err e) { | ||
| * System.out.println(e.getMessage()); // => "FailToDoSomething { name=abc, value=123 }" | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public final class Exc extends Exception { | ||
|
|
||
| /** The serial version UID. */ | ||
| private static final long serialVersionUID = 260427082865587554L; | ||
|
|
||
| /** The reason for this exception. */ | ||
| private transient Record reason; | ||
|
|
||
| /** The stack trace for the location of occurrence. */ | ||
| private StackTraceElement trace; | ||
|
|
||
| /** | ||
| * Is the constructor which takes a {@link Record} object indicating the reason for this exception. | ||
| * | ||
| * @param reason | ||
| * A reason for this exception. | ||
| */ | ||
| public Exc(final Record reason) { | ||
| if (reason == null) { | ||
| throw new IllegalArgumentException("reason is null"); | ||
| } | ||
| this.reason = reason; | ||
|
|
||
| this.trace = getStackTrace()[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Is the constructor which takes a {@link Record} object indicating the reason and {@link Throwable} object | ||
| * indicating the cause for this exception. | ||
| * | ||
| * @param reason | ||
| * A reason for this exception. | ||
| * @param cause | ||
| * A cause for this exception. | ||
| */ | ||
| @SuppressWarnings("this-escape") | ||
| public Exc(final Record reason, final Throwable cause) { | ||
| super(cause); | ||
|
|
||
| if (reason == null) { | ||
| throw new IllegalArgumentException("reason is null"); | ||
| } | ||
| this.reason = reason; | ||
|
|
||
| this.trace = getStackTrace()[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the reason for this exception. The type of the reason. | ||
| * | ||
| * @return The reason for this exception. | ||
| */ | ||
| public Record getReason() { | ||
| return this.reason; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the message of this exception, that is the reason. | ||
| * | ||
| * @return The message of this exception. | ||
| */ | ||
| @Override | ||
| public String getMessage() { | ||
| var rsn = this.reason.toString(); | ||
| var rname = this.reason.getClass().getSimpleName(); | ||
| rsn = rsn.substring(rname.length() + 1, rsn.length() - 1); | ||
|
|
||
| var buf = new StringBuilder(this.reason.getClass().getName()); | ||
| buf.append(" { ").append(rsn).append(" }"); | ||
| return buf.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the detail message of this exception, that contains the reason, source file name, line number, and the | ||
| * cause if provided. | ||
| * | ||
| * @return The message of this exception. | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| var buf = new StringBuilder(getClass().getName()); | ||
| buf.append(" { reason = ").append(getMessage()); | ||
| buf.append(", file = ").append(this.trace.getFileName()); | ||
| buf.append(", line = ").append(this.trace.getLineNumber()); | ||
| if (getCause() != null) { | ||
| buf.append(", cause = ").append(getCause().toString()); | ||
| } | ||
| return buf.append(" }").toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the name of the source file of this exception occurrance. | ||
| * <p> | ||
| * This method can return null if this information is unavailable. | ||
| * | ||
| * @return The name of the source file of this error occurrence. | ||
| */ | ||
| public String getFile() { | ||
| return this.trace.getFileName(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the line number of this exception occurrance in the source file. | ||
| * <p> | ||
| * This method can return a negative number if this information is unavailable. | ||
| * | ||
| * @return The line number of this exception occurrance in the source file. | ||
| */ | ||
| public int getLine() { | ||
| return this.trace.getLineNumber(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a {@link RuntimeException} object for methods that cannot throw a {@link Exc}. | ||
| * | ||
| * @return A {@link RuntimeException} object. | ||
| */ | ||
| public RuntimeException toRuntimeException() { | ||
| return new RuntimeExc(this); | ||
| } | ||
|
|
||
| /** | ||
| * Writes a serial data of this exception to a stream. | ||
| * <p> | ||
| * Since a {@link Record} object is not necessarily serializable, this method will throw a | ||
| * {@link NotSerializableException} if the {@code reason} field does not inherit {@link Serializable}. | ||
| * | ||
| * @param out | ||
| * An {@link ObjectOutputStream} to which data is written. | ||
| * | ||
| * @throws IOException | ||
| * if an I/O error occurs. | ||
| */ | ||
| private void writeObject(ObjectOutputStream out) throws IOException { | ||
| if (!(this.reason instanceof Serializable)) { | ||
| throw new NotSerializableException(this.reason.getClass().getName()); | ||
| } | ||
| out.defaultWriteObject(); | ||
| out.writeObject(this.reason); | ||
| } | ||
|
|
||
| /** | ||
| * Reconstitutes the {@code Exc} instance from a stream and initialize the reason and cause properties when | ||
| * deserializing. If the reason by deserialization is null or invalid, this method throws | ||
| * {@link InvalidObjectException}. | ||
| * | ||
| * @param in | ||
| * An {@link ObjectInputStream} from which data is read. | ||
| * | ||
| * @throws IOException | ||
| * if an I/O error occurs. | ||
| * @throws ClassNotFoundException | ||
| * if a serialized class cannot be loaded. | ||
| */ | ||
| private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { | ||
| in.defaultReadObject(); | ||
| this.reason = Record.class.cast(in.readObject()); | ||
|
|
||
| if (this.reason == null) { | ||
| throw new InvalidObjectException("reason is null or invalid."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final class RuntimeExc extends RuntimeException { | ||
| private static final long serialVersionUID = 4664405757902479929L; | ||
|
|
||
| RuntimeExc(Exc exc) { | ||
| super(exc); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return getCause().getMessage(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getClass().getName() + ": " + getCause().toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public Throwable fillInStackTrace() { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright (C) 2024 Takayuki Sato. All Rights Reserved. | ||
| * | ||
| * This program is free software under MIT License. | ||
| * See the file LICENSE in this distribution for more details. | ||
| */ | ||
|
|
||
| /** | ||
| * Provides classes for handling an exception with a reason. | ||
| * <p> | ||
| * This package contains the {@code Exc} class which has a record field indicates the reason for the exception. | ||
| * | ||
| * @version 0.1 | ||
| */ | ||
| package com.github.sttk.errs; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright (C) 2025 Takayuki Sato. All Rights Reserved. | ||
| * | ||
| * This program is free software under MIT License. | ||
| * See the file LICENSE in this distribution for more details. | ||
| */ | ||
|
|
||
| /** | ||
| * Contains a package which provides the APIs for handling an exception with a reason. | ||
| * | ||
| * @version 0.1 | ||
| */ | ||
| module com.github.sttk.errs { | ||
| exports com.github.sttk.errs; | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/resources/META-INF/native-image/com.github.sttk/errs/serialization-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| { | ||
| "types": [ | ||
| { | ||
| "name": "com.github.sttk.errs.Exc" | ||
| }, | ||
| { | ||
| "name": "java.io.IOException" | ||
| }, | ||
| { | ||
| "name": "java.io.NotSerializableException" | ||
| }, | ||
| { | ||
| "name": "java.io.ObjectStreamException" | ||
| }, | ||
| { | ||
| "name": "java.lang.Exception" | ||
| }, | ||
| { | ||
| "name": "java.lang.RuntimeException" | ||
| }, | ||
| { | ||
| "name": "java.lang.StackTraceElement" | ||
| }, | ||
| { | ||
| "name": "java.lang.StackTraceElement[]" | ||
| }, | ||
| { | ||
| "name": "java.lang.String" | ||
| }, | ||
| { | ||
| "name": "java.lang.Throwable" | ||
| }, | ||
| { | ||
| "name": "java.util.Collections$EmptyList" | ||
| } | ||
| ], | ||
| "lambdaCapturingTypes":[ | ||
| ], | ||
| "proxies":[ | ||
| ] | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.