Skip to content

Commit 1a5c7a6

Browse files
committed
Fix #31
Added some javadocs Some code cleanup Some internal changes
1 parent 4e65fa9 commit 1a5c7a6

15 files changed

+263
-171
lines changed

src/main/java/com/btk5h/skriptmirror/Descriptor.java

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,30 @@
99
import java.util.regex.Matcher;
1010
import java.util.regex.Pattern;
1111

12+
/**
13+
* A Descriptor is all the info that can be provided in a script about a method.
14+
* This consists of:
15+
* <ul>
16+
* <li>An optional class the member (method, field or constructor) is defined in.</li>
17+
* <li>The name of the member</li>
18+
* <li>The parameter types of the member.</li>
19+
* </ul>
20+
*/
1221
public final class Descriptor {
22+
23+
/**
24+
* A regex string for a list of array symbols, e.g. {@code [][][]}.
25+
*/
1326
private static final String PACKAGE_ARRAY = SkriptMirrorUtil.PACKAGE + "(?:\\[])*";
27+
28+
/**
29+
* A regex {@link Pattern} for a single array symbol, matches only {@code []}.
30+
*/
1431
private static final Pattern PACKAGE_ARRAY_SINGLE = Pattern.compile("\\[]");
32+
33+
/**
34+
* A regex {@link Pattern} for a {@link Descriptor}.
35+
*/
1536
private static final Pattern DESCRIPTOR =
1637
Pattern.compile("" +
1738
"(?:\\[(" + SkriptMirrorUtil.PACKAGE + ")])?" +
@@ -61,63 +82,73 @@ public String toString() {
6182
}
6283

6384
public String toString(boolean isStatic) {
64-
return String.format("%s" + (isStatic ? "." : "#") + "%s",
65-
javaClass == null ? "(unspecified)" : SkriptMirrorUtil.getDebugName(javaClass),
66-
name);
85+
return javaClass == null ? "(unspecified)" : SkriptMirrorUtil.getDebugName(javaClass)
86+
+ (isStatic ? "." : "#")
87+
+ name;
6788
}
6889

90+
/**
91+
* Returns a new descriptor with a new {@link #javaClass}.
92+
* If this Descriptors {@link #javaClass} is not null, it will instead return itself.
93+
*/
6994
public Descriptor orDefaultClass(Class<?> cls) {
70-
if (getJavaClass() != null) {
95+
if (javaClass != null) {
7196
return this;
7297
}
7398

74-
return new Descriptor(cls, getName(), getParameterTypes());
99+
return new Descriptor(cls, name, parameterTypes);
75100
}
76101

77-
public static Descriptor parse(String desc, File script) throws ClassNotFoundException {
102+
/**
103+
* Parses the given {@link String} as a {@link Descriptor}. The script parameter is to get the imports.
104+
*/
105+
public static Descriptor parse(String desc, File script) throws ImportNotFoundException {
78106
Matcher m = DESCRIPTOR.matcher(desc);
79107

80108
if (m.matches()) {
81109
String cls = m.group(1);
82-
String name = m.group(2);
83-
String args = m.group(3);
84-
85-
Class<?> javaClass = null;
86-
Class<?>[] parameterTypes = null;
110+
Class<?> javaClass = cls == null ? null : lookupClass(script, cls);
87111

88-
if (cls != null) {
89-
javaClass = lookupClass(script, cls);
90-
}
112+
String name = m.group(2);
91113

92-
if (args != null) {
93-
parameterTypes = parseParams(args, script);
94-
}
114+
String args = m.group(3);
115+
Class<?>[] parameterTypes = args == null ? null : parseParams(args, script);
95116

96117
return new Descriptor(javaClass, name, parameterTypes);
97118
}
98119

99120
return null;
100121
}
101122

102-
private static Class<?>[] parseParams(String args, File script) throws ClassNotFoundException {
123+
/**
124+
* Parses a list of imported names, returning a class array containing the classes in the given string.
125+
*/
126+
private static Class<?>[] parseParams(String args, File script) throws ImportNotFoundException {
103127
String[] rawClasses = args.split(",");
128+
104129
Class<?>[] parsedClasses = new Class<?>[rawClasses.length];
130+
105131
for (int i = 0; i < rawClasses.length; i++) {
106132
String userType = rawClasses[i].trim();
107133

134+
// Calculate array depth with regex
108135
Matcher arrayDepthMatcher = PACKAGE_ARRAY_SINGLE.matcher(userType);
109136
int arrayDepth = 0;
110137
while (arrayDepthMatcher.find()) {
111138
arrayDepth++;
112139
}
113-
userType = userType.substring(0, userType.length() - (2 * arrayDepth));
114140

115-
Class<?> cls = JavaUtil.PRIMITIVE_CLASS_NAMES.get(userType);
141+
// Remove array's square brackets
142+
userType = userType.substring(0, userType.length() - (2 * arrayDepth));
116143

117-
if (cls == null) {
144+
Class<?> cls;
145+
if (JavaUtil.PRIMITIVE_CLASS_NAMES.containsKey(userType)) {
146+
cls = JavaUtil.PRIMITIVE_CLASS_NAMES.get(userType);
147+
} else {
118148
cls = lookupClass(script, userType);
119149
}
120150

151+
// Convert class to array class with calculated depth
121152
cls = JavaUtil.getArrayClass(cls, arrayDepth);
122153

123154
parsedClasses[i] = cls;
@@ -126,13 +157,15 @@ private static Class<?>[] parseParams(String args, File script) throws ClassNotF
126157
return parsedClasses;
127158
}
128159

129-
private static Class<?> lookupClass(File script, String userType) throws ClassNotFoundException {
160+
/**
161+
* Looks up a class from its imported name in the given file.
162+
*/
163+
private static Class<?> lookupClass(File script, String userType) throws ImportNotFoundException {
130164
JavaType customImport = CustomImport.lookup(script, userType);
165+
if (customImport == null)
166+
throw new ImportNotFoundException(userType);
131167

132-
if (customImport != null) {
133-
return customImport.getJavaClass();
134-
} else {
135-
return LibraryLoader.getClassLoader().loadClass(userType);
136-
}
168+
return customImport.getJavaClass();
137169
}
170+
138171
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.btk5h.skriptmirror;
2+
3+
/**
4+
* Exception thrown when the user tries to use a certain class, but that class is not imported.
5+
* Currently only used in the {@link Descriptor} class.
6+
*/
7+
public class ImportNotFoundException extends Exception {
8+
9+
private final String userType;
10+
11+
public ImportNotFoundException(String userType) {
12+
super("Import not found: " + userType);
13+
this.userType = userType;
14+
}
15+
16+
public String getUserType() {
17+
return userType;
18+
}
19+
20+
}

src/main/java/com/btk5h/skriptmirror/ScriptLoaderState.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import org.bukkit.event.Event;
77

88
public class ScriptLoaderState {
9-
private Config currentScript;
10-
private String currentEventName;
11-
private Class<? extends Event>[] currentEvents;
12-
private Kleenean hasDelayBefore;
9+
private final Config currentScript;
10+
private final String currentEventName;
11+
private final Class<? extends Event>[] currentEvents;
12+
private final Kleenean hasDelayBefore;
1313

1414
private ScriptLoaderState(Config currentScript, String currentEventName, Class<? extends Event>[] currentEvents,
1515
Kleenean hasDelayBefore) {
@@ -27,10 +27,10 @@ public void applyToCurrentState() {
2727

2828
public static ScriptLoaderState copyOfCurrentState() {
2929
return new ScriptLoaderState(
30-
ScriptLoader.currentScript,
31-
ScriptLoader.getCurrentEventName(),
32-
ScriptLoader.getCurrentEvents(),
33-
ScriptLoader.hasDelayBefore
30+
ScriptLoader.currentScript,
31+
ScriptLoader.getCurrentEventName(),
32+
ScriptLoader.getCurrentEvents(),
33+
ScriptLoader.hasDelayBefore
3434
);
3535
}
3636
}

src/main/java/com/btk5h/skriptmirror/skript/CondExpressionStatement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.Executors;
2323

2424
public class CondExpressionStatement extends Condition {
25+
2526
static {
2627
Skript.registerCondition(CondExpressionStatement.class, "[(1¦await)] %~javaobject%");
2728
}
@@ -69,15 +70,13 @@ public String toString(Event e, boolean debug) {
6970
return arg.toString(e, debug);
7071
}
7172

72-
@SuppressWarnings("unchecked")
7373
@Override
7474
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
7575
SkriptParser.ParseResult parseResult) {
7676
arg = SkriptUtil.defendExpression(exprs[0]);
7777

78-
if (!(arg instanceof ExprJavaCall)) {
78+
if (!(arg instanceof ExprJavaCall))
7979
return false;
80-
}
8180

8281
isAsynchronous = (parseResult.mark & 1) == 1;
8382
isCondition = SkriptLogger.getNode() instanceof SectionNode;
@@ -86,6 +85,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
8685
Skript.error("Asynchronous java calls may not be used as conditions.");
8786
return false;
8887
}
88+
8989
if (isAsynchronous)
9090
ScriptLoader.hasDelayBefore = Kleenean.TRUE;
9191

src/main/java/com/btk5h/skriptmirror/skript/CondParseLater.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
6464

6565
private Statement getParsedStatement() {
6666
if (parsedStatement == null) {
67+
ScriptLoaderState previousState = ScriptLoaderState.copyOfCurrentState();
6768
scriptLoaderState.applyToCurrentState();
68-
parsedStatement = Statement.parse(statement,
69-
String.format("Could not parse condition/effect at runtime: %s", statement));
69+
parsedStatement = Statement.parse(statement, "Could not parse condition/effect at runtime: "
70+
+ statement);
71+
previousState.applyToCurrentState();
7072

7173
if (parsedStatement == null) {
7274
return null;

src/main/java/com/btk5h/skriptmirror/skript/EffExpressionStatement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ protected void execute(Event e) {
3737
@Override
3838
protected TriggerItem walk(Event e) {
3939
if (isAsynchronous) {
40-
// TODO how to implement async, Bukkit or just other Java (CondExpressionStatement#walk(Event))
4140
Object localVariables = SkriptReflection.getLocals(e);
4241
CompletableFuture.runAsync(() -> {
4342
SkriptReflection.putLocals(localVariables, e);

src/main/java/com/btk5h/skriptmirror/skript/Types.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ protected boolean canBeInstantiated() {
123123
}
124124
}));
125125

126-
Converters.registerConverter(ClassInfo.class, JavaType.class,
127-
((Converter<ClassInfo, JavaType>) c -> new JavaType(c.getC())));
126+
Converters.registerConverter(ClassInfo.class, JavaType.class, c -> new JavaType(c.getC()));
128127

129128
Classes.registerClass(new ClassInfo<>(Null.class, "null")
130129
.parser(new Parser<Null>() {

src/main/java/com/btk5h/skriptmirror/skript/custom/ExprParseMark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public String toString(Event e, boolean debug) {
4040
return "parser mark";
4141
}
4242

43-
@SuppressWarnings("unchecked")
4443
@Override
4544
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
4645
SkriptParser.ParseResult parseResult) {
@@ -52,6 +51,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5251
)) {
5352
Skript.error("The parser mark may only be used in custom syntax.",
5453
ErrorQuality.SEMANTIC_ERROR);
54+
return false;
5555
}
5656
return true;
5757
}

src/main/java/com/btk5h/skriptmirror/skript/custom/ExprParseRegex.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ public String toString(Event e, boolean debug) {
5858
return "parser mark";
5959
}
6060

61-
@SuppressWarnings("unchecked")
6261
@Override
6362
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
6463
SkriptParser.ParseResult parseResult) {
6564
if (!ScriptLoader.isCurrentEvent(
66-
EffectTriggerEvent.class,
67-
ExpressionGetEvent.class,
68-
ExpressionChangeEvent.class,
69-
ConditionCheckEvent.class
70-
)) {
65+
EffectTriggerEvent.class,
66+
ExpressionGetEvent.class,
67+
ExpressionChangeEvent.class,
68+
ConditionCheckEvent.class
69+
)) {
7170
Skript.error("The parsed regular expression may only be used in custom syntax.",
72-
ErrorQuality.SEMANTIC_ERROR);
71+
ErrorQuality.SEMANTIC_ERROR);
72+
return false;
7373
}
7474

7575
index = Utils.parseInt(parseResult.regexes.get(0).group(0));

src/main/java/com/btk5h/skriptmirror/skript/custom/PreloadListener.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public void onPreScriptLoad(PreScriptLoadEvent preScriptLoadEvent) {
6464
}
6565
}
6666

67-
// TODO docs for all changes
68-
69-
// TODO error for non-preloadable syntax when attempted to be used on preload
70-
// TODO and have a concrete boolean for whether a custom syntax section is preloadable
71-
72-
// TODO usable in section (imports)
73-
7467
public static void handleEventNode(SectionNode sectionNode) {
7568
String key = sectionNode.getKey();
7669
assert key != null;

0 commit comments

Comments
 (0)