diff options
author | Tor Norbye <tnorbye@google.com> | 2012-05-30 14:54:39 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-05-30 17:25:18 -0700 |
commit | 7e4b8e9d595e45baa9d87cdb8282f02759e73abc (patch) | |
tree | 2da5fccd6c1e00c972c69323e3cf10df2e5ab264 /lint/cli | |
parent | 5ee7da2ae31dcbe781ed81c07f31b31d0a0a7d7f (diff) | |
download | sdk-7e4b8e9d595e45baa9d87cdb8282f02759e73abc.zip sdk-7e4b8e9d595e45baa9d87cdb8282f02759e73abc.tar.gz sdk-7e4b8e9d595e45baa9d87cdb8282f02759e73abc.tar.bz2 |
Fix nullness annotations
Eclipse 4.2 includes analysis support for @Nullable and @NonNull
annotations. However, it requires these annotations to be *repeated*
on every single method implementing or overriding a superclass or
interface method (!).
This changeset basically applies the quickfixes to inline these
annotations. It also changes the retention of our nullness
annotations from source to class, since without this Eclipse believes
that a @NonNull annotation downstream is a redefinition of a @Nullable
annotation.
Finally, the null analysis revealed a dozen or so places where the
nullness annotation was either wrong, or some null checking on
parameters or return values needed to be done.
Change-Id: I43b4e56e2d025a8a4c92a8873f55c13cdbc4c1cb
Diffstat (limited to 'lint/cli')
-rw-r--r-- | lint/cli/src/com/android/tools/lint/LintCliXmlParser.java | 19 | ||||
-rw-r--r-- | lint/cli/src/com/android/tools/lint/LombokParser.java | 13 | ||||
-rw-r--r-- | lint/cli/src/com/android/tools/lint/Main.java | 45 |
3 files changed, 49 insertions, 28 deletions
diff --git a/lint/cli/src/com/android/tools/lint/LintCliXmlParser.java b/lint/cli/src/com/android/tools/lint/LintCliXmlParser.java index e777e99..c13c41d 100644 --- a/lint/cli/src/com/android/tools/lint/LintCliXmlParser.java +++ b/lint/cli/src/com/android/tools/lint/LintCliXmlParser.java @@ -16,6 +16,7 @@ package com.android.tools.lint; +import com.android.annotations.NonNull; import com.android.annotations.Nullable; import com.android.tools.lint.client.api.IDomParser; import com.android.tools.lint.client.api.IssueRegistry; @@ -40,7 +41,7 @@ import java.io.UnsupportedEncodingException; */ public class LintCliXmlParser extends PositionXmlParser implements IDomParser { @Override - public Document parseXml(XmlContext context) { + public Document parseXml(@NonNull XmlContext context) { try { // Do we need to provide an input stream for encoding? String xml = context.getContents(); @@ -70,22 +71,22 @@ public class LintCliXmlParser extends PositionXmlParser implements IDomParser { } @Override - public Location getLocation(XmlContext context, Node node) { + public @NonNull Location getLocation(@NonNull XmlContext context, @NonNull Node node) { OffsetPosition pos = (OffsetPosition) getPosition(node); if (pos != null) { return Location.create(context.file, pos, (OffsetPosition) pos.getEnd()); } - return null; + return Location.create(context.file); } @Override - public Handle createLocationHandle(XmlContext context, Node node) { + public @NonNull Handle createLocationHandle(@NonNull XmlContext context, @NonNull Node node) { return new LocationHandle(context.file, node); } @Override - protected OffsetPosition createPosition(int line, int column, int offset) { + protected @NonNull OffsetPosition createPosition(int line, int column, int offset) { return new OffsetPosition(line, column, offset); } @@ -143,7 +144,7 @@ public class LintCliXmlParser extends PositionXmlParser implements IDomParser { } @Override - public void setEnd(com.android.util.PositionXmlParser.Position end) { + public void setEnd(@NonNull com.android.util.PositionXmlParser.Position end) { mEnd = end; } @@ -155,7 +156,7 @@ public class LintCliXmlParser extends PositionXmlParser implements IDomParser { } @Override - public void dispose(XmlContext context, Document document) { + public void dispose(@NonNull XmlContext context, @NonNull Document document) { } /* Handle for creating DOM positions cheaply and returning full fledged locations later */ @@ -170,13 +171,13 @@ public class LintCliXmlParser extends PositionXmlParser implements IDomParser { } @Override - public Location resolve() { + public @NonNull Location resolve() { OffsetPosition pos = (OffsetPosition) getPosition(mNode); if (pos != null) { return Location.create(mFile, pos, (OffsetPosition) pos.getEnd()); } - return null; + return Location.create(mFile); } @Override diff --git a/lint/cli/src/com/android/tools/lint/LombokParser.java b/lint/cli/src/com/android/tools/lint/LombokParser.java index 15b1073..2c263c6 100644 --- a/lint/cli/src/com/android/tools/lint/LombokParser.java +++ b/lint/cli/src/com/android/tools/lint/LombokParser.java @@ -16,6 +16,7 @@ package com.android.tools.lint; +import com.android.annotations.NonNull; import com.android.annotations.Nullable; import com.android.tools.lint.client.api.IJavaParser; import com.android.tools.lint.detector.api.JavaContext; @@ -39,7 +40,7 @@ import lombok.ast.grammar.Source; public class LombokParser implements IJavaParser { @Override - public Node parseJava(JavaContext context) { + public Node parseJava(@NonNull JavaContext context) { try { Source source = new Source(context.getContents(), context.file.getName()); List<Node> nodes = source.getNodes(); @@ -95,19 +96,21 @@ public class LombokParser implements IJavaParser { } @Override - public Location getLocation(JavaContext context, lombok.ast.Node node) { + public @NonNull Location getLocation( + @NonNull JavaContext context, + @NonNull lombok.ast.Node node) { lombok.ast.Position position = node.getPosition(); return Location.create(context.file, context.getContents(), position.getStart(), position.getEnd()); } @Override - public Handle createLocationHandle(JavaContext context, Node node) { + public @NonNull Handle createLocationHandle(@NonNull JavaContext context, @NonNull Node node) { return new LocationHandle(context.file, node); } @Override - public void dispose(JavaContext context, Node compilationUnit) { + public void dispose(@NonNull JavaContext context, @NonNull Node compilationUnit) { } /* Handle for creating positions cheaply and returning full fledged locations later */ @@ -122,7 +125,7 @@ public class LombokParser implements IJavaParser { } @Override - public Location resolve() { + public @NonNull Location resolve() { Position pos = mNode.getPosition(); return Location.create(mFile, null /*contents*/, pos.getStart(), pos.getEnd()); } diff --git a/lint/cli/src/com/android/tools/lint/Main.java b/lint/cli/src/com/android/tools/lint/Main.java index 6a40cb3..68d68b4 100644 --- a/lint/cli/src/com/android/tools/lint/Main.java +++ b/lint/cli/src/com/android/tools/lint/Main.java @@ -21,6 +21,7 @@ import static com.android.tools.lint.client.api.IssueRegistry.PARSER_ERROR; import static com.android.tools.lint.detector.api.LintConstants.DOT_XML; import static com.android.tools.lint.detector.api.LintUtils.endsWith; +import com.android.annotations.NonNull; import com.android.annotations.Nullable; import com.android.tools.lint.checks.BuiltinIssueRegistry; import com.android.tools.lint.client.api.Configuration; @@ -886,7 +887,11 @@ public class Main extends LintClient { } @Override - public void log(Severity severity, Throwable exception, String format, Object... args) { + public void log( + @NonNull Severity severity, + @Nullable Throwable exception, + @Nullable String format, + @Nullable Object... args) { System.out.flush(); if (!mQuiet) { // Place the error message on a line of its own since we're printing '.' etc @@ -907,7 +912,7 @@ public class Main extends LintClient { } @Override - public Configuration getConfiguration(Project project) { + public Configuration getConfiguration(@NonNull Project project) { return new CliConfiguration(mDefaultConfiguration, project); } @@ -934,8 +939,13 @@ public class Main extends LintClient { } @Override - public void report(Context context, Issue issue, Severity severity, Location location, - String message, Object data) { + public void report( + @NonNull Context context, + @NonNull Issue issue, + @NonNull Severity severity, + @Nullable Location location, + @NonNull String message, + @Nullable Object data) { assert context.isEnabled(issue); if (severity == Severity.IGNORE) { @@ -1041,7 +1051,7 @@ public class Main extends LintClient { } @Override - public String readFile(File file) { + public @NonNull String readFile(@NonNull File file) { try { return LintUtils.getEncodedString(file); } catch (IOException e) { @@ -1067,7 +1077,7 @@ public class Main extends LintClient { } @Override - public Severity getSeverity(Issue issue) { + public @NonNull Severity getSeverity(@NonNull Issue issue) { Severity severity = computeSeverity(issue); if (mAllErrors && severity != Severity.IGNORE) { @@ -1082,7 +1092,7 @@ public class Main extends LintClient { } @Override - protected Severity getDefaultSeverity(Issue issue) { + protected @NonNull Severity getDefaultSeverity(@NonNull Issue issue) { if (mWarnAll) { return issue.getDefaultSeverity(); } @@ -1090,7 +1100,7 @@ public class Main extends LintClient { return super.getDefaultSeverity(issue); } - private Severity computeSeverity(Issue issue) { + private Severity computeSeverity(@NonNull Issue issue) { Severity severity = super.getSeverity(issue); String id = issue.getId(); @@ -1123,25 +1133,32 @@ public class Main extends LintClient { private class ProgressPrinter implements LintListener { @Override - public void update(LintDriver lint, EventType type, Context context) { + public void update( + @NonNull LintDriver lint, + @NonNull EventType type, + @Nullable Context context) { switch (type) { - case SCANNING_PROJECT: + case SCANNING_PROJECT: { + String name = context != null ? context.getProject().getName() : "?"; if (lint.getPhase() > 1) { System.out.print(String.format( "\nScanning %1$s (Phase %2$d): ", - context.getProject().getName(), + name, lint.getPhase())); } else { System.out.print(String.format( "\nScanning %1$s: ", - context.getProject().getName())); + name)); } break; - case SCANNING_LIBRARY_PROJECT: + } + case SCANNING_LIBRARY_PROJECT: { + String name = context != null ? context.getProject().getName() : "?"; System.out.print(String.format( "\n - %1$s: ", - context.getProject().getName())); + name)); break; + } case SCANNING_FILE: System.out.print('.'); break; |