Bug 570738: [R-Source] Add nullable annotations to RAsts

Change-Id: I07f73259d02dd85698d8d14a70c60b1cc09d27b9
diff --git a/r/org.eclipse.statet.r.core/src/org/eclipse/statet/r/core/rsource/ast/RAsts.java b/r/org.eclipse.statet.r.core/src/org/eclipse/statet/r/core/rsource/ast/RAsts.java
index 5806c46..a1ef0be 100644
--- a/r/org.eclipse.statet.r.core/src/org/eclipse/statet/r/core/rsource/ast/RAsts.java
+++ b/r/org.eclipse.statet.r.core/src/org/eclipse/statet/r/core/rsource/ast/RAsts.java
@@ -23,6 +23,8 @@
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.jface.text.IRegion;
 
+import org.eclipse.statet.jcommons.collections.ImCollections;
+import org.eclipse.statet.jcommons.collections.ImList;
 import org.eclipse.statet.jcommons.collections.IntArrayList;
 import org.eclipse.statet.jcommons.collections.IntList;
 import org.eclipse.statet.jcommons.lang.NonNull;
@@ -42,6 +44,7 @@
 /**
  * 
  */
+@NonNullByDefault
 public class RAsts extends Asts {
 	
 	
@@ -50,7 +53,7 @@
 		private final int startOffset;
 		private final int stopOffset;
 		private boolean inAssignment;
-		private RAstNode assignment;
+		private @Nullable RAstNode assignment;
 		
 		
 		public LowestFDefAssignmentSearchVisitor(final int offset) {
@@ -101,7 +104,8 @@
 				RAstNode candidate= node.getRParent();
 				// TODO: use analyzed ElementAccess if possible
 				AssignExpr assign= null;
-				while ((assign= checkAssign(candidate)) != null && assign.valueNode == take) {
+				while (candidate != null && (assign= checkAssign(candidate)) != null
+						&& assign.valueNode == take) {
 					take= assign.assignNode;
 					candidate= take.getRParent();
 				}
@@ -113,7 +117,7 @@
 	}
 	
 	
-	public static RAstNode findLowestFDefAssignment(final AstNode root, final int offset) {
+	public static @Nullable RAstNode findLowestFDefAssignment(final AstNode root, final int offset) {
 		final LowestFDefAssignmentSearchVisitor visitor= new LowestFDefAssignmentSearchVisitor(offset);
 		try {
 			root.accept(visitor);
@@ -123,11 +127,12 @@
 		return visitor.assignment;
 	}
 	
+	
 	private static class DeepestCommandsSearchVisitor extends GenericVisitor implements AstVisitor {
 		
 		private final int startOffset;
 		private final int stopOffset;
-		private RAstNode container;
+		private @Nullable RAstNode container;
 		private final List<RAstNode> commands= new ArrayList<>();
 		
 		
@@ -200,8 +205,8 @@
 	private static class NextCommandsSearchVisitor extends GenericVisitor implements AstVisitor {
 		
 		private final int offset;
-		private RAstNode container;
-		private RAstNode next;
+		private @Nullable RAstNode container;
+		private @Nullable RAstNode next;
 		
 		
 		public NextCommandsSearchVisitor(final int offset) {
@@ -274,17 +279,17 @@
 		
 	}
 	
-	public static RAstNode[] findDeepestCommands(final AstNode root, final int startOffset, final int endOffset) {
+	public static ImList<RAstNode> findDeepestCommands(final AstNode root, final int startOffset, final int endOffset) {
 		final DeepestCommandsSearchVisitor visitor= new DeepestCommandsSearchVisitor(startOffset, endOffset);
 		try {
 			root.accept(visitor);
 		}
 		catch (final InvocationTargetException e) {
 		}
-		return visitor.commands.toArray(new RAstNode[visitor.commands.size()]);
+		return ImCollections.toList(visitor.commands);
 	}
 	
-	public static RAstNode findNextCommands(final AstNode root, final int offset) {
+	public static @Nullable RAstNode findNextCommands(final AstNode root, final int offset) {
 		final NextCommandsSearchVisitor visitor= new NextCommandsSearchVisitor(offset);
 		try {
 			root.accept(visitor);
@@ -294,6 +299,7 @@
 		return visitor.next;
 	}
 	
+	
 	public static class AssignExpr {
 		
 		public static final Object GLOBAL= new Object();
@@ -313,7 +319,7 @@
 		
 	}
 	
-	public static AssignExpr checkAssign(final RAstNode node) {
+	public static @Nullable AssignExpr checkAssign(final RAstNode node) {
 		switch (node.getNodeType()) {
 		case A_LEFT:
 		case A_RIGHT:
@@ -342,27 +348,29 @@
 			return checkAssign(node.getRParent());
 		case F_CALL_ARG:
 			return checkAssign(node.getRParent().getRParent());
+		default:
+			break;
 		}
 		return null;
 	}
 	
-	@NonNullByDefault
+	
 	public static final class FCallArgMatch {
 		
 		public final ArgsDefinition argsDef;
+		
 		public final FCall.Args argsNode;
+		
 		public final FCall. @Nullable Arg[] allocatedArgs;
 		public final FCall.Arg[] ellipsisArgs;
 		public final FCall.Arg[] otherArgs;
 		public final int[] argsNode2argsDef;
 		
 		
-		private FCallArgMatch(
-				final ArgsDefinition argsDef, 
-				final FCall.Args argsNode, 
-				final FCall. @Nullable Arg[] allocatedArgs, 
-				final FCall.Arg[] ellipsisArgs, 
-				final FCall.Arg[] otherArgs,
+		private FCallArgMatch(final ArgsDefinition argsDef,
+				final FCall.Args argsNode,
+				final FCall. @Nullable Arg[] allocatedArgs,
+				final FCall.Arg[] ellipsisArgs, final FCall.Arg[] otherArgs,
 				final int[] argsNode2argsDef) {
 			this.argsDef= argsDef;
 			this.argsNode= argsNode;
@@ -433,7 +441,7 @@
 		
 	}
 	
-	public static final FCall.Arg @NonNull [] NO_ARGS= new FCall.Arg[0];
+	public static final FCall.Arg[] NO_ARGS= new FCall.Arg[0];
 	
 	private static final int FAIL= -1;
 	private static final int ELLIPSIS= -2;
@@ -472,7 +480,6 @@
 	 * @param argsDef the arguments definition
 	 * @return
 	 */
-	@NonNullByDefault
 	public static FCallArgMatch matchArgs(final FCall.Args argsNode, final ArgsDefinition argsDef) {
 		final int nodeArgsCount= argsNode.getChildCount();
 		final int defArgsCount= argsDef.size();
@@ -609,7 +616,6 @@
 	/**
 	 * @return position of the element name, if possible (symbol or strings), otherwise <code>null</code>.
 	 */
-	@NonNullByDefault
 	public static @Nullable TextRegion getElementNameRegion(final RAstNode node) {
 		switch (node.getNodeType()) {
 		case SYMBOL:
@@ -627,7 +633,7 @@
 				) != 0 );
 	}
 	
-	public static int[] computeRExpressionIndex(RAstNode node, final RAstNode baseNode) {
+	public static int @Nullable [] computeRExpressionIndex(RAstNode node, final RAstNode baseNode) {
 		final IntList topdown= new IntArrayList();
 		while (node != baseNode) {
 			final RAstNode parent= node.getRParent();
@@ -716,7 +722,7 @@
 		return path;
 	}
 	
-	public static List<RAstNode> computeRExpressionNodes(RAstNode node, final RAstNode baseNode) {
+	public static @Nullable List<RAstNode> computeRExpressionNodes(RAstNode node, final RAstNode baseNode) {
 		final List<RAstNode> nodes= new ArrayList<>();
 		while (node != baseNode) {
 			switch (node.getNodeType()) {
@@ -789,7 +795,7 @@
 		return nodes;
 	}
 	
-	public static RAstNode getRRootNode(RAstNode node, final IRegion region) {
+	public static RAstNode getRRootNode(RAstNode node, final @Nullable IRegion region) {
 		if (region == null) {
 			return node.getRRoot();
 		}
@@ -812,6 +818,7 @@
 		return node;
 	}
 	
+	@SuppressWarnings("null")
 	public static boolean isParentChild(final RAstNode parent, RAstNode child) {
 		while ((child= child.getRParent()) != null) {
 			if (child == parent) {
@@ -972,7 +979,7 @@
 				case NUM_NUM: {
 						final Double num= parseNum(node.getText());
 						if (num != null) {
-							return num.doubleValue();
+							return num;
 						}
 					}
 					break;
@@ -1019,10 +1026,10 @@
 				}
 				if (text.startsWith("0x")) { //$NON-NLS-1$
 					text= text.substring(2);
-					return Integer.parseInt(text, 16);
+					return Integer.valueOf(text, 16);
 				}
 				else {
-					return Integer.parseInt(text);
+					return Integer.valueOf(text);
 				}
 			}
 			catch (final NumberFormatException e) {}
diff --git a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandAndGotoNextCommandHandler.java b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandAndGotoNextCommandHandler.java
index bd51da4..dae8a17 100644
--- a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandAndGotoNextCommandHandler.java
+++ b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandAndGotoNextCommandHandler.java
@@ -20,6 +20,8 @@
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.ui.statushandlers.StatusManager;
 
+import org.eclipse.statet.jcommons.collections.ImList;
+
 import org.eclipse.statet.ecommons.ui.util.UIAccess;
 
 import org.eclipse.statet.r.core.rsource.ast.RAstNode;
@@ -43,8 +45,8 @@
 	@Override
 	protected void postLaunch(final Data data) {
 		try {
-			final RAstNode[] nodes = data.nodes;
-			final int offset = getNextOffset(nodes[nodes.length-1], data.document);
+			final ImList<RAstNode> nodes= data.nodes;
+			final int offset= getNextOffset(nodes.get(nodes.size() - 1), data.document);
 			UIAccess.getDisplay().asyncExec(new Runnable() {
 				@Override
 				public void run() {
diff --git a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandHandler.java b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandHandler.java
index e53b4f7..4df5b9a 100644
--- a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandHandler.java
+++ b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitEntireCommandHandler.java
@@ -37,6 +37,8 @@
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.progress.IProgressService;
 
+import org.eclipse.statet.jcommons.collections.ImList;
+
 import org.eclipse.statet.ecommons.ui.util.UIAccess;
 
 import org.eclipse.statet.internal.r.debug.ui.RLaunchingMessages;
@@ -71,7 +73,7 @@
 		AbstractDocument document;
 		RSourceUnitModelInfo model;
 		AstInfo ast;
-		RAstNode[] nodes;
+		ImList<RAstNode> nodes;
 		RSourceUnit su;
 		
 		List<SourceRegion> regions;
@@ -87,7 +89,7 @@
 	}
 	
 	protected SubmitEntireCommandHandler(final boolean gotoConsole) {
-		fGotoConsole = gotoConsole;
+		this.fGotoConsole = gotoConsole;
 	}
 	
 	
@@ -189,9 +191,9 @@
 	
 	protected IStatus getRegions(final Data data)
 			throws CoreException {
-		final RAstNode[] nodes = RAsts.findDeepestCommands(data.ast.getRoot(),
+		final ImList<RAstNode> nodes= RAsts.findDeepestCommands(data.ast.getRoot(),
 				data.selection.getOffset(), data.selection.getOffset()+data.selection.getLength() );
-		if (nodes == null || nodes.length == 0) {
+		if (nodes.isEmpty()) {
 			final RAstNode next = RAsts.findNextCommands(data.ast.getRoot(),
 					data.selection.getOffset()+data.selection.getLength() );
 			if (next != null) {
@@ -206,18 +208,18 @@
 		}
 		try {
 			data.nodes = nodes;
-			final List<SourceRegion> list= new ArrayList<>(nodes.length);
-			for (int i = 0; i < nodes.length; i++) {
-				if (RAsts.hasErrors(nodes[i])) {
+			final List<SourceRegion> list= new ArrayList<>(nodes.size());
+			for (final RAstNode node : nodes) {
+				if (RAsts.hasErrors(node)) {
 					return new Status(IStatus.ERROR, RUI.BUNDLE_ID,
 							RLaunchingMessages.SubmitCode_info_SyntaxError_message );
 				}
 				
 				final SourceRegion region = new SourceRegion(data.su, data.document);
-				region.setBegin(checkStart(data.document, nodes[i].getStartOffset()));
-				region.setEnd(nodes[i].getEndOffset());
+				region.setBegin(checkStart(data.document, node.getStartOffset()));
+				region.setEnd(node.getEndOffset());
 				region.setCode(data.document.get(region.getOffset(), region.getLength()));
-				region.setNode(nodes[i]);
+				region.setNode(node);
 				list.add(region);
 			}
 			data.regions = list;
diff --git a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitFunctionDefHandler.java b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitFunctionDefHandler.java
index 4e73f8c..7cce71b 100644
--- a/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitFunctionDefHandler.java
+++ b/r/org.eclipse.statet.r.ui/srcDebug/org/eclipse/statet/internal/r/debug/ui/launcher/SubmitFunctionDefHandler.java
@@ -22,6 +22,8 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.text.BadLocationException;
 
+import org.eclipse.statet.jcommons.collections.ImCollections;
+
 import org.eclipse.statet.internal.r.debug.ui.RLaunchingMessages;
 import org.eclipse.statet.r.core.rsource.ast.RAstNode;
 import org.eclipse.statet.r.core.rsource.ast.RAsts;
@@ -75,7 +77,7 @@
 						RLaunchingMessages.SubmitCode_info_SyntaxError_message );
 			}
 			
-			data.nodes= new RAstNode[] { node };
+			data.nodes= ImCollections.newList(node);
 			final List<SourceRegion> list= new ArrayList<>(1);
 			{	final SourceRegion region= new SourceRegion(data.su, data.document);
 				region.setBegin(checkStart(data.document, node.getStartOffset()));