Skip to content

Commit

Permalink
fix npe for bigint/double/long function
Browse files Browse the repository at this point in the history
fix null pointer execepiton for bigint double and long function
  • Loading branch information
shuailung committed Feb 28, 2024
1 parent bd20856 commit 32b61cb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AviatorObject call(final Map<String, Object> env, final AviatorObject arg
} else if (obj instanceof Character) {
return AviatorBigInt.valueOf(new BigInteger(String.valueOf(obj)));
} else {
throw new ClassCastException("Could not cast " + obj.getClass().getName() + " to bigint");
throw new ClassCastException("Could not cast " + (obj != null ? obj.getClass().getName() : "null") + " to bigint, AviatorObject is " + arg1);
}
case String:
return AviatorBigInt.valueOf(new BigInteger((String) arg1.getValue(env)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
} else if (obj instanceof Character) {
return new AviatorDouble(Double.parseDouble(String.valueOf(obj)));
} else {
throw new ClassCastException("Could not cast " + obj.getClass().getName() + " to double");
throw new ClassCastException("Could not cast " + (obj != null ? obj.getClass().getName() : "null") + " to double, AviatorObject is" + arg1 );
}
case String:
return new AviatorDouble(Double.parseDouble((String) arg1.getValue(env)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
} else if (obj instanceof Character) {
return AviatorLong.valueOf(Long.valueOf(String.valueOf(obj)));
} else {
throw new ClassCastException("Could not cast " + obj.getClass().getName() + " to long");
throw new ClassCastException("Could not cast " + (obj != null ? obj.getClass().getName() : "null") + " to long, AviatorObject is " + arg1);
}
case String:
return AviatorLong.valueOf(Long.valueOf((String) arg1.getValue(env)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.aviator.runtime.function.system;

import com.googlecode.aviator.runtime.type.AviatorJavaType;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;


public class BigIntFunctionUnitTest {
@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNullArgument() {
BigIntFunction bigIntFunction = new BigIntFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
bigIntFunction.call(null, aviatorJavaType);
}

@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNotSupportArgument() {
BigIntFunction bigIntFunction = new BigIntFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
Map<String, Object> env = new HashMap<>();
env.put("var", true);

bigIntFunction.call(env, aviatorJavaType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.aviator.runtime.function.system;

import com.googlecode.aviator.runtime.type.AviatorJavaType;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;


public class DoubleFunctionUnitTest {
@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNullArgument() {
DoubleFunction doubleFunction = new DoubleFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
doubleFunction.call(null, aviatorJavaType);
}

@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNotSupportArgument() {
DoubleFunction doubleFunction = new DoubleFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
Map<String, Object> env = new HashMap<>();
env.put("var", true);

doubleFunction.call(env, aviatorJavaType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.aviator.runtime.function.system;

import com.googlecode.aviator.runtime.type.AviatorJavaType;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;


public class LongFunctionUnitTest {
@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNullArgument() {
LongFunction longFunction = new LongFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
longFunction.call(null, aviatorJavaType);
}

@Test(expected = ClassCastException.class)
public void testCall_WithJavaTypeNotSupportArgument() {
LongFunction longFunction = new LongFunction();
AviatorJavaType aviatorJavaType = new AviatorJavaType("var");
Map<String, Object> env = new HashMap<>();
env.put("var", true);

longFunction.call(env, aviatorJavaType);
}
}

0 comments on commit 32b61cb

Please sign in to comment.