Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullness checker #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ 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.

ADDED ANNOTATIONS
-------------------
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
4 changes: 4 additions & 0 deletions commands.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<version>${gson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>

<build>
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/com/squareup/squash/SquashBacktrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@

package com.squareup.squash;

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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AnnotatedFor({"nullness"})
/** Creates the Squash stacktrace format for serialization by gson. */
public final class SquashBacktrace {

private SquashBacktrace() {
// Should not be instantiated: this is a utility class.
}

public static List<SquashException> getBacktraces(Throwable error) {
//adding the annotation @Nullable as the return type may include a null value
public static @Nullable List<SquashException> getBacktraces(Throwable error) {
if (error == null) {
return null;
}
Expand All @@ -42,15 +46,20 @@ public static List<SquashException> getBacktraces(Throwable error) {
private static List<StackElement> getStacktraceArray(Throwable error) {
List<StackElement> stackElems = new ArrayList<StackElement>();
for (StackTraceElement element : error.getStackTrace()) {
StackElement elementList =
new StackElement(element.getClassName(), 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);
}
return stackElems;
}

public static Map<String, Object> getIvars(Throwable error) {
//adding the annotation @Nullable as the return type may include a null value
public static @Nullable Map<String, Object> getIvars(Throwable error) {
if (error == null) {
return null;
}
Expand All @@ -63,7 +72,12 @@ public static Map<String, Object> getIvars(Throwable error) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object val = field.get(error);
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) {
Expand All @@ -84,9 +98,15 @@ public static void populateNestedExceptions(List<NestedException> nestedExceptio
return;
}
final Throwable cause = error.getCause();
NestedException doc =
@SuppressWarnings("nullness") NestedException doc =
new NestedException(cause.getClass().getName(), cause.getMessage(), getBacktraces(cause),
getIvars(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);
Expand Down