Fix crash when toString returns null value (#8067)

* Fix crash when toString returns null value

* Return a string from NullUnit toString

* Begone redundancy

* Unwrap before null check to ensure non-nullability
This commit is contained in:
buthed010203 2023-01-07 11:28:01 -05:00 committed by GitHub
parent c0b819ec9b
commit a5fd29e56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 18 deletions

View File

@ -882,7 +882,7 @@ public class EntityProcess extends BaseProcessor{
for(Smethod method : methods){
String signature = method.toString();
if(signatures.contains(signature)) continue;
if(!signatures.add(signature)) continue;
Stype compType = interfaceToComp(method.type());
MethodSpec.Builder builder = MethodSpec.overriding(method.e).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
@ -893,25 +893,29 @@ public class EntityProcess extends BaseProcessor{
builder.addAnnotation(OverrideCallSuper.class); //just in case
if(!method.isVoid()){
if(method.name().equals("isNull")){
builder.addStatement("return true");
}else if(method.name().equals("id")){
String methodName = method.name();
switch(methodName){
case "isNull":
builder.addStatement("return true");
break;
case "id":
builder.addStatement("return -1");
}else{
Svar variable = compType == null || method.params().size > 0 ? null : compType.fields().find(v -> v.name().equals(method.name()));
String desc = variable == null ? null : variable.descString();
if(variable == null || !varInitializers.containsKey(desc)){
builder.addStatement("return " + getDefault(method.ret().toString()));
}else{
String init = varInitializers.get(desc);
builder.addStatement("return " + (init.equals("{}") ? "new " + variable.mirror().toString() : "") + init);
}
break;
case "toString":
builder.addStatement("return $S", className);
break;
default:
Svar variable = compType == null || method.params().size > 0 ? null : compType.fields().find(v -> v.name().equals(methodName));
String desc = variable == null ? null : variable.descString();
if(variable == null || !varInitializers.containsKey(desc)){
builder.addStatement("return " + getDefault(method.ret().toString()));
}else{
String init = varInitializers.get(desc);
builder.addStatement("return " + (init.equals("{}") ? "new " + variable.mirror().toString() : "") + init);
}
}
}
nullBuilder.addMethod(builder.build());
signatures.add(signature);
}
nullsBuilder.addField(FieldSpec.builder(type, Strings.camelize(baseName)).initializer("new " + className + "()").addModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.PUBLIC).build());

View File

@ -45,8 +45,10 @@ public class Scripts implements Disposable{
try{
Object o = context.evaluateString(scope, text, "console.js", 1);
if(o instanceof NativeJavaObject n) o = n.unwrap();
if(o instanceof Undefined) o = "undefined";
return String.valueOf(o);
if(o == null) o = "null";
else if(o instanceof Undefined) o = "undefined";
var out = o.toString();
return out == null ? "null" : out;
}catch(Throwable t){
return getError(t, false);
}