Creating Anonymous Class with Set Name

When creating an expression for the scripting language Skript (https://github.com/SkriptLang/Skript) you would do:
public class ExprWhatever extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprWhatever.class, String.class, ExpressionType.COMBINED, "pattern");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
return true;
}

@Override
protected String[] get(Event e) {
return null;
}

.........
}
public class ExprWhatever extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprWhatever.class, String.class, ExpressionType.COMBINED, "pattern");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
return true;
}

@Override
protected String[] get(Event e) {
return null;
}

.........
}
I want to add a QoL class for simple expressions like
var expr = SimpleExpression.create(String.class, "length of %string%", String::length, ...);
Skript.registerExpression(expr);
var expr = SimpleExpression.create(String.class, "length of %string%", String::length, ...);
Skript.registerExpression(expr);
so that you don't need to spam classes for simple stuff like this. The problem is the class name, I could return an anonymous class with the methods overriden but it will appear as Syntaxes$1 $2 $3... which would make it very hard to debug. Is there a way I can change that without ASM or anything bytecode related?
7 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @eren.! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
Where do these class names appear that make it hard to debug? What about using the same class with a custom toString?
eren.
eren.OP3w ago
stack trace
dan1st
dan1st3w ago
If you are using JDK 22 or later, you can use the class file API instead of ASM: https://openjdk.org/jeps/466 While this is bytecode-related, I think you could create one template class and "just" replace the class name Note that this is a preview feature in JDK 22 and 23 but it will be final in JDK 24 (will be released in March)
eren.
eren.OP3w ago
damn java releases so often this probably wont be used in main project but i can make a separate library i guess thank you
JavaBot
JavaBot3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot3w ago
Post Closed
This post has been closed by <@587604022349791274>.

Did you find this page helpful?