npe() runtime intrinsic method.

This commit is contained in:
Alex Tkachman
2011-11-26 09:30:24 +02:00
parent a6eeb01b64
commit 2cbd072478
6 changed files with 100 additions and 3 deletions
+10
View File
@@ -10,4 +10,14 @@ public class Intrinsics {
public static String stringPlus(String self, Object other) {
return ((self == null) ? "null" : self) + ((other == null) ? "null" : other.toString());
}
public static Object npe(Object self) {
if(self == null)
return throwNpe();
return self;
}
private static Object throwNpe() {
throw new JetNullPointerException();
}
}
@@ -0,0 +1,32 @@
package jet.runtime;
import java.util.ArrayList;
/**
* @author alex.tkachman
*/
class JetNullPointerException extends NullPointerException {
@Override
public synchronized Throwable fillInStackTrace() {
super.fillInStackTrace();
StackTraceElement[] stackTrace = getStackTrace();
ArrayList<StackTraceElement> list = new ArrayList<StackTraceElement>();
boolean skip = true;
for(StackTraceElement ste : stackTrace) {
if(!skip) {
list.add(ste);
}
else {
if("jet.runtime.Intrinsics".equals(ste.getClassName()) && "npe".equals(ste.getMethodName())) {
skip = false;
}
}
}
setStackTrace(list.toArray(new StackTraceElement[list.size()]));
return this;
}
public static void main(String[] args) {
Intrinsics.npe(null);
}
}