From d42fdb51c246ee7ffdaec20ade52efb6ca34013b Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Mon, 5 Mar 2018 17:05:58 +0530 Subject: [PATCH 1/8] with Nullness Checker's Annotations --- .../com/squareup/squash/SquashBacktrace.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/squareup/squash/SquashBacktrace.java b/src/main/java/com/squareup/squash/SquashBacktrace.java index a5edc53..ba10e85 100644 --- a/src/main/java/com/squareup/squash/SquashBacktrace.java +++ b/src/main/java/com/squareup/squash/SquashBacktrace.java @@ -14,6 +14,8 @@ package com.squareup.squash; +import org.checkerframework.checker.nullness.qual.*; +import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -27,8 +29,8 @@ public final class SquashBacktrace { private SquashBacktrace() { // Should not be instantiated: this is a utility class. } - - public static List getBacktraces(Throwable error) { + //adding the annotation @Nullable as the return type may include a null value + public static @Nullable List getBacktraces(Throwable error) { if (error == null) { return null; } @@ -42,15 +44,15 @@ public static List getBacktraces(Throwable error) { private static List getStacktraceArray(Throwable error) { List stackElems = new ArrayList(); for (StackTraceElement element : error.getStackTrace()) { - StackElement elementList = - new StackElement(element.getClassName(), element.getFileName(), element.getLineNumber(), + StackElement elementList = + new StackElement(element.getClassName(), castNonNull(element.getFileName()), element.getLineNumber(), element.getMethodName()); stackElems.add(elementList); - } + }//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. return stackElems; } - - public static Map getIvars(Throwable error) { + //adding the annotation @Nullable as the return type may include a null value + public static @Nullable Map getIvars(Throwable error) { if (error == null) { return null; } @@ -63,7 +65,7 @@ public static Map getIvars(Throwable error) { if (!field.isAccessible()) { field.setAccessible(true); } - Object val = field.get(error); + Object val = castNonNull(field.get(error));//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. ivars.put(field.getName(), val); } } catch (IllegalAccessException e) { @@ -85,8 +87,8 @@ public static void populateNestedExceptions(List nestedExceptio } final Throwable cause = error.getCause(); NestedException doc = - new NestedException(cause.getClass().getName(), cause.getMessage(), getBacktraces(cause), - getIvars(cause)); + new NestedException(cause.getClass().getName(), castNonNull(cause.getMessage()), castNonNull(getBacktraces(cause)), + castNonNull(getIvars(cause)));//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. nestedExceptions.add(doc); // Exceptions all the way down! populateNestedExceptions(nestedExceptions, cause); From f8f3c0f8122e5c367beca90979f32d08e4305d5b Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Mon, 5 Mar 2018 17:12:26 +0530 Subject: [PATCH 2/8] Added Annotations --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 82803d1..e615df1 100644 --- a/README.md +++ b/README.md @@ -113,3 +113,4 @@ Even if you are not using code obfuscation, you can still use this gem to map Java class names to their original file paths, as Java stack traces do not include the full path to source files, which Squash needs to perform its Git-blame magic. +//The code of the SquashBacktrace.java are now with Nullness Annoatations which ensures that the program will never throw a null pointer exception. From c80d40ebd831b7c977d07baf9141e336b3d57673 Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Tue, 6 Mar 2018 23:23:38 +0530 Subject: [PATCH 3/8] Modified with Annotations --- .../com/squareup/squash/SquashBacktrace.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/squareup/squash/SquashBacktrace.java b/src/main/java/com/squareup/squash/SquashBacktrace.java index ba10e85..cc51693 100644 --- a/src/main/java/com/squareup/squash/SquashBacktrace.java +++ b/src/main/java/com/squareup/squash/SquashBacktrace.java @@ -30,7 +30,7 @@ private SquashBacktrace() { // Should not be instantiated: this is a utility class. } //adding the annotation @Nullable as the return type may include a null value - public static @Nullable List getBacktraces(Throwable error) { + public static @Nullable List getBacktraces(Throwable error) { if (error == null) { return null; } @@ -44,11 +44,16 @@ private SquashBacktrace() { private static List getStacktraceArray(Throwable error) { List stackElems = new ArrayList(); for (StackTraceElement element : error.getStackTrace()) { - StackElement elementList = - new StackElement(element.getClassName(), castNonNull(element.getFileName()), element.getLineNumber(), - element.getMethodName()); + @SuppressWarnings("nullness") StackElement elementList = + new StackElement(element.getClassName(),element.getFileName(), element.getLineNumber(), + element.getMethodName());/*The constructor of the StackElement class requires non-null + type arguments,but the method call element.getFileName() might + return an null value,it can be resolved by modifying parameter of the + constructor by adding the annotattion @Nullable, but for now the annotation @SuppressWarnings("nullness") + can be added assuming it doesn't return a null value and suppressing it + if it does,without crashing the program. */ stackElems.add(elementList); - }//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. + } return stackElems; } //adding the annotation @Nullable as the return type may include a null value @@ -65,7 +70,12 @@ private static List getStacktraceArray(Throwable error) { if (!field.isAccessible()) { field.setAccessible(true); } - Object val = castNonNull(field.get(error));//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. + Object val = castNonNull(field.get(error));/*Ivars is a HashMap object, we can't allow a nullable + value at the place of a value for a specific key in the + put() method of the HashMap.The castNonNull() method of + NullnessUtil class can be used as this method takes a + possibly null reference unsafely casts it to have the @NonNull + type qualifier.As it's an expression statement @SuppressWarnings("nullness") can't be used.*/ ivars.put(field.getName(), val); } } catch (IllegalAccessException e) { @@ -86,9 +96,15 @@ public static void populateNestedExceptions(List nestedExceptio return; } final Throwable cause = error.getCause(); - NestedException doc = - new NestedException(cause.getClass().getName(), castNonNull(cause.getMessage()), castNonNull(getBacktraces(cause)), - castNonNull(getIvars(cause)));//used the method castNonNull because can't use the annotation @SuppressWarnings("nullness") for the expression, castNonNull is a method that suppresses warnings from the Nullness Checker. + @SuppressWarnings("nullness") NestedException doc = + new NestedException(cause.getClass().getName(), cause.getMessage(), getBacktraces(cause), + getIvars(cause));/*The contructor of the NestedException class can't take a null value,but the three method calls + 1. cause.getMessage() , 2. getBacktrace(cause), 3.getIvars(cause) may return null values, + resolved it using @SuppressWarnings("nullness") reasons: + 1. cause.getMessage() : the constructor itself can be modified with the annotation @Nullable + 2.getBacktrace(cause) : have a return type annotated with @Nullable(see line no. 33) + 3.getIvars(cause) : have a return type annotated with @Nullable(see line no. 61), + the method castNonNull() of the NullnessUtil class can't be used as there are chances of getting a null value.*/ nestedExceptions.add(doc); // Exceptions all the way down! populateNestedExceptions(nestedExceptions, cause); From 03ff8af45b35a7d21056b2bba5c3f618fdc80ca4 Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Tue, 6 Mar 2018 23:27:45 +0530 Subject: [PATCH 4/8] Create commands.txt --- commands.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 commands.txt diff --git a/commands.txt b/commands.txt new file mode 100644 index 0000000..7c0faea --- /dev/null +++ b/commands.txt @@ -0,0 +1,4 @@ +/*commands for SquashBacktrace.java*/ +(compiling): +"java -jar "C:\Program Files\checker-framework-2.3.2\checker-framework-2.3.2\checker\dist\checker.jar" -processor nullness SquashBacktrace.java" +Hope there's is no compilation error From 32d66c6dedbd0ecd529b23f8d2f3c61197aaf27c Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Tue, 6 Mar 2018 23:31:11 +0530 Subject: [PATCH 5/8] Added dependency for checker framework --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 9ce4fe3..67bb068 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,11 @@ ${gson.version} test + + org.checkerframework + checker + 2.2.1 + From d53d0d7a87e842590e840ead0d8956ad10b5e120 Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Tue, 6 Mar 2018 23:42:07 +0530 Subject: [PATCH 6/8] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e615df1..e786141 100644 --- a/README.md +++ b/README.md @@ -113,4 +113,7 @@ Even if you are not using code obfuscation, you can still use this gem to map Java class names to their original file paths, as Java stack traces do not include the full path to source files, which Squash needs to perform its Git-blame magic. -//The code of the SquashBacktrace.java are now with Nullness Annoatations which ensures that the program will never throw a null pointer exception. + +ADDED ANNOTATIONS +------------------- +The code of the SquashBacktrace.java are now with Nullness Annoatations which ensures that the program will never throw a null pointer exception. From bd8bd3cacaad16aeecdc8a41396c75847d2918e6 Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Tue, 6 Mar 2018 23:51:07 +0530 Subject: [PATCH 7/8] Added Annotations --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e786141..132a067 100644 --- a/README.md +++ b/README.md @@ -116,4 +116,7 @@ Git-blame magic. ADDED ANNOTATIONS ------------------- -The code of the SquashBacktrace.java are now with Nullness Annoatations which ensures that the program will never throw a null pointer exception. +This branch contains the code of the SquashBacktrace.java that are now with Nullness Annoatations which ensures that the program will never throw a null pointer exception. +Files Updated are : +1.SquashBacktrace.java +2.pom.xml From a38d1f7880509d7c8f7ec454f5bdf56d46bac0c9 Mon Sep 17 00:00:00 2001 From: Arica Chakraborty Date: Thu, 8 Mar 2018 00:13:14 +0530 Subject: [PATCH 8/8] Update SquashBacktrace.java --- src/main/java/com/squareup/squash/SquashBacktrace.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/squareup/squash/SquashBacktrace.java b/src/main/java/com/squareup/squash/SquashBacktrace.java index cc51693..99f5619 100644 --- a/src/main/java/com/squareup/squash/SquashBacktrace.java +++ b/src/main/java/com/squareup/squash/SquashBacktrace.java @@ -16,6 +16,7 @@ import org.checkerframework.checker.nullness.qual.*; import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull; +import org.checkerframework.framework.qual.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -23,6 +24,7 @@ import java.util.List; import java.util.Map; +@AnnotatedFor({"nullness"}) /** Creates the Squash stacktrace format for serialization by gson. */ public final class SquashBacktrace {