npe() renamed to sure()

This commit is contained in:
Alex Tkachman
2011-11-26 10:01:57 +02:00
parent 2cbd072478
commit 554aab83be
9 changed files with 58 additions and 71 deletions
+29 -1
View File
@@ -1,5 +1,7 @@
package jet.runtime;
import java.util.ArrayList;
/**
* @author alex.tkachman
*/
@@ -11,7 +13,7 @@ public class Intrinsics {
return ((self == null) ? "null" : self) + ((other == null) ? "null" : other.toString());
}
public static Object npe(Object self) {
public static Object sure(Object self) {
if(self == null)
return throwNpe();
return self;
@@ -20,4 +22,30 @@ public class Intrinsics {
private static Object throwNpe() {
throw new JetNullPointerException();
}
private static Throwable sanitizeStackTrace(Throwable throwable) {
StackTraceElement[] stackTrace = throwable.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()) && "sure".equals(ste.getMethodName())) {
skip = false;
}
}
}
throwable.setStackTrace(list.toArray(new StackTraceElement[list.size()]));
return throwable;
}
private static class JetNullPointerException extends NullPointerException {
@Override
public synchronized Throwable fillInStackTrace() {
super.fillInStackTrace();
return sanitizeStackTrace(this);
}
}
}
@@ -1,32 +0,0 @@
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);
}
}